ES操作还是应该看官网api指南,这里写了几个自己用到的,作为总结记录。

https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-aggregations-metrics-top-hits-aggregation.html

1

聚合索引中字段stu_num的数量(groupby),min_doc_count代表每一项聚合输出的项要求最少300个doc, size代表最多输出11个聚合项,排序按照每一项的doc数量排序。

{
	"aggs": {
		"all_stu_num": {
			"terms": {
				"field": "stu_num",
				"min_doc_count": 300,
				"size": 11,
				"order": {
					"_count": "desc"
				}
			}
		}
	}
}

显示结果如下:

elasticsearch查询总结

1.1

每个聚合的buckets里面的操作,也可以显示top n个最相关的doc

Top hits Aggregationedit

A top_hits metric aggregator keeps track of the most relevant document being aggregated. This aggregator is intended to be used as a sub aggregator, so that the top matching documents can be aggregated per bucket.

The top_hits aggregator can effectively be used to group result sets by certain fields via a bucket aggregator. One or more bucket aggregators determines by which properties a result set get sliced into.

{
  "aggs": {
    "zcl_all_stu_num": {
      "terms": {
        "field": "stu_num",
        "min_doc_count": 300,
        "size": 11,
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "zcl_top_duplications": {
          "top_hits": {
            "size": 3
          }
        }
      }
    }
  }
}

结果如下,每个buckets显示top3

elasticsearch查询总结

相关文章: