Elasticsearch - 获取指定字段的术语聚合

时间:2016-09-30 08:52:13

标签: elasticsearch

我正在使用术语聚合来获取每个城市的所有用户

$oSetting = PersonalSetting::find($id);
                $oSetting->Login = utf8_decode($login);
                $oSetting->Sign = utf8_decode($sign);
                $oSetting->save();

这是结果。但我总是希望得到一些特定城市的聚合结果,无论他们是否排在前10名。我是否需要分别对每个城市使用过滤器聚合来获得结果?

1 个答案:

答案 0 :(得分:0)

您有三种解决方案:

一个。您可以在查询中指定过滤器:

{
  "query": {
    "terms": {
      "city.name": [ "city1", "city2", "city3" ]
    }
  },
  "aggs": {
    "cities": {
      "terms": {
        "field": "city.name"
      }
    }
  }
}

B中。您可以在聚合中指定过滤器:

{
  "aggs": {
    "city_filter": {
      "filter": {
        "terms": {
          "city.name": [
            "city1",
            "city2",
            "city3"
          ]
        }
      },
      "aggs": {
        "cities": {
          "terms": {
            "field": "city.name"
          }
        }
      }
    }
  }
}

℃。您可以在聚合:

这两个词中filter values
{
  "aggs": {
    "cities": {
      "terms": {
        "field": "city.name",
        "include": "city1*",
        "exclude": "city2*"
      }
    }
  }
}