elasticsearch - 如何使用ElasticSearch处理聚合?

标签 elasticsearch aggregate

查询:

GET test5/_search
{
   "size": 1,
   "aggregations": {
      "Location": {
         "terms": {
            "field": "Location"
         }
      }
   }
}

输出:
   {
           "took": 5,
           "timed_out": false,
           "_shards": {
              "total": 5,
              "successful": 5,
              "failed": 0
           },
           "hits": {
              "total": 19,
              "max_score": 1,
              "hits": [
                 {
                    "_index": "test5",
                    "_type": "xxxxxx",
                    "_id": "AVLOBXbvFe2VQdUY7tZu",
                    "_score": 1,
                    "_source": {
                        fired",
                       "Host": "N/A",

                       "Mac": "N/A",
                       "Destination IP": "xxx.xxx.xxx.xxx",
                       "Location": "United States",
                       "Events/Flows": "107",
                       "Asset Name": "N/A",
                       "Custom Rule XXXX :": "xxxxxxfp",
                       "User": "N/A"
                    }
                 }
              ]
           },
           "aggregations": {
              "Location": {
                 "doc_count_error_upper_bound": 0,
                 "sum_other_doc_count": 0,
                 "buckets": [
                    {
                       "key": "other",
                       "doc_count": 11
                    },
                    {
                       "key": "states",
                       "doc_count": 2
                    },
                    {
                       "key": "united",
                       "doc_count": 2
                    }
                 ]
              }
           }
        }

您会注意到,美国已被当作两个键。如何获得美国作为单键?

最佳答案

您需要确保Location字段为not_analyzed,否则其值将被ES标记和分析,以便United States成为您看到的两个标记:unitedstates
creating your mapping时,您需要确保您的Location字段如下所示:

curl -XPUT localhost:9200/test5/_mapping/xxxxxx -d '{
  "properties": {
    ... your other fields...
    "Location": {
      "type": "string",
      "index": "not_analyzed"           <--- make sure to add this
    }
  }
}

关于elasticsearch - 如何使用ElasticSearch处理聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35442651/

相关文章:

python - 如何在pandas数据框中使用groupby获取列列表的平均值?

c++ - 列出聚合的初始化 : when can it invoke copy constructor?

java - GroupBy 框架

mongodb - 如何使用 MongoDB 在 Go 中聚合过去一小时内创建的数据?

c# - Elasticsearch 在使用 IndexMany 时推断动态类型的 _id

elasticsearch - 在Kibana中获取带有新架构的 “Objects in arrays are not well supported” Kibana

authentication - 将Elasticsearch 2.1.1升级到2.2.0-缺少身份验证 token ?

javascript - 在不知道属性名称的情况下从对象数组中提取最小/最大值

node.js - Elasticsearch 更新不返回任何 node.js 的结果

python - ElasticSearch:查找具有数组中字段值的文档