Elasticsearch包括聚合结果集中的字段

时间:2015-11-12 19:02:43

标签: elasticsearch

如何将字符串类型的字段包含在聚合的结果集中? 例如,给出以下映射:

skip=0

示例数据:

{
 "sport": {
  "mappings": {
     "runners": {
        "properties": {
           "name": {
              "type": "string"
           },
           "city": {
              "type": "string"
           },
           "region": {
              "type": "string"
           },
           "sport": {
              "type": "string"
                }
            }
          }
        }
      }
    }

如何将字段curl -XPOST "http://localhost:9200/sport/_bulk" -d' {"index":{"_index":"sport","_type":"runner"}} {"name":"Gary", "city":"New York","region":"A","sport":"Soccer"} {"index":{"_index":"sport","_type":"runner"}} {"name":"Bob", "city":"New York","region":"A","sport":"Tennis"} {"index":{"_index":"sport","_type":"runner"}} {"name":"Mike", "city":"Atlanta","region":"B","sport":"Soccer"} ' 包含在聚合的结果集中:

name

1 个答案:

答案 0 :(得分:0)

如果我理解正确的话,这似乎就是你想要的:

POST /sport/_search
{
   "size": 0,
   "aggregations": {
      "city_terms": {
         "terms": {
            "field": "city"
         },
         "aggs": {
            "name_terms": {
               "terms": {
                  "field": "name"
               }
            }
         }
      }
   }
}

根据您提供的数据,它会返回:

{
   "took": 43,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "city_terms": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "new",
               "doc_count": 2,
               "name_terms": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "bob",
                        "doc_count": 1
                     },
                     {
                        "key": "gary",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "york",
               "doc_count": 2,
               "name_terms": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "bob",
                        "doc_count": 1
                     },
                     {
                        "key": "gary",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "atlanta",
               "doc_count": 1,
               "name_terms": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "mike",
                        "doc_count": 1
                     }
                  ]
               }
            }
         ]
      }
   }
}

(如果这些结果不符合您的预期,您可能需要将"index":"not_analyzed"添加到地图中的一个或两个字段。)

以下是我用来测试它的代码:

http://sense.qbox.io/gist/07735aadc082c1c60409931c279f3fd85a340dbb