如何在elastic.js(elasticsearch)中计算具有相同值的字段?

我有一个社区的名单。 我需要创build聚合查询,它将统计所有具有相同标题的数据。

[ { "_id": "56161cb3cbdad2e3b437fdc3", "_type": "Comunity", "name": "public", "data": [ { "title": "sonder", "creationDate": "2015-08-22T03:43:28 -03:00", "quantity": 0 }, { "title": "vule", "creationDate": "2014-05-17T12:35:01 -03:00", "quantity": 0 }, { "title": "omer", "creationDate": "2015-01-31T04:54:19 -02:00", "quantity": 3 }, { "title": "sonder", "creationDate": "2014-05-22T05:09:36 -03:00", "quantity": 3 } ] }, { "_id": "56161cb3dae30517fc133cd9", "_type": "Comunity", "name": "static", "data": [ { "title": "vule", "creationDate": "2014-07-01T06:32:06 -03:00", "quantity": 5 }, { "title": "vule", "creationDate": "2014-01-10T12:40:28 -02:00", "quantity": 1 }, { "title": "vule", "creationDate": "2014-01-09T09:33:11 -02:00", "quantity": 3 } ] }, { "_id": "56161cb32f62b522355ca3c8", "_type": "Comunity", "name": "public", "data": [ { "title": "vule", "creationDate": "2014-02-03T09:55:28 -02:00", "quantity": 2 }, { "title": "vule", "creationDate": "2015-01-23T09:14:22 -02:00", "quantity": 0 } ] } ] 

所以欲望的结果应该是

 [ { title: vule, total: 6 }, { title: omer, total: 1 }, { title: sonder, total: 1 } ] 

我写了一些聚合查询,但它仍然无法正常工作。 我怎样才能得到欲望的结果?

PS:我试图创build嵌套聚合

 ejs.Request().size(0).agg( ejs.NestedAggregation('comunities') .path('data') .agg( ejs.FilterAggregation('sonder') .filter( ejs.TermsFilter('data.title', 'sonder') ).agg( ejs.ValueCountAggregation('counts') .field('data.title') ) ) ); 

你需要使用术语聚合。

现在取决于你的映射,可以有两种方法来做到这一点:

1.您的数据字段存储为一个子文档

你需要运行一个简单的术语聚合,这在RAW json中是这样的:

 POST /test/test/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "Grouping": { "terms": { "field": "data.title", "size": 0 } } } } 

2.您的数据字段存储为嵌套文档

在进行术语聚合之前,您必须添加一个嵌套的subaggregation。

 POST /test/test/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "Nest": { "nested": { "path": "data" }, "aggs": { "Grouping": { "terms": { "field": "data.title", "size": 0 } } } } } } 

两者都会输出这个:

 { "took": 125, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": 0, "hits": [] }, "aggregations": { "Nest": { "doc_count": 9, "Grouping": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "vule", "doc_count": 6 -- The Total count you're looking for }, { "key": "sonder", "doc_count": 2 }, { "key": "omer", "doc_count": 1 } ] } } } } 

不幸的是,这只是一个原始的查询,但我想它可以很容易地转换成elastic.js 。

最重要的是。 如果你要做聚合,不要忘记设置你的领域,你正在做聚合,因为它将开始计算单个的令牌,如文档

我自己会将这些文档存储为嵌套文档。

例:

映射:

 PUT /test { "mappings": { "test": { "properties": { "name": { "type": "string" }, "data": { "type": "nested", "properties": { "title": { "type": "string", "index": "not_analyzed", "fields": { "stemmed": { "type": "string", "analyzed": "standard" } } }, "creationDate": { "type": "date", "format": "dateOptionalTime" }, "quantity": { "type": "integer" } } } } } } } 

testing数据:

 PUT /test/test/56161cb3cbdad2e3b437fdc3 { "name": "public", "data": [ { "title": "sonder", "creationDate": "2015-08-22T03:43:28", "quantity": 0 }, { "title": "vule", "creationDate": "2014-05-17T12:35:01", "quantity": 0 }, { "title": "omer", "creationDate": "2015-01-31T04:54:19", "quantity": 3 }, { "title": "sonder", "creationDate": "2014-05-22T05:09:36", "quantity": 3 } ] } PUT /test/test/56161cb3dae30517fc133cd9 { "name": "static", "data": [ { "title": "vule", "creationDate": "2014-07-01T06:32:06", "quantity": 5 }, { "title": "vule", "creationDate": "2014-01-10T12:40:28", "quantity": 1 }, { "title": "vule", "creationDate": "2014-01-09T09:33:11", "quantity": 3 } ] } PUT /test/test/56161cb32f62b522355ca3c8 { "name": "public", "data": [ { "title": "vule", "creationDate": "2014-02-03T09:55:28", "quantity": 2 }, { "title": "vule", "creationDate": "2015-01-23T09:14:22", "quantity": 0 } ] } 

实际查询:

 POST /test/test/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "Nest": { "nested": { "path": "data" }, "aggs": { "Grouping": { "terms": { "field": "data.title", "size": 0 } } } } } } 

PS "size":0意味着我让Elasticsearch输出所有可能的术语,而不是像文档中所描述的那样限制它的输出。

可以设置size参数来定义从总体术语表中返回多less个术语桶。 默认情况下,协调search过程的节点将请求每个分片提供自己的最大size词桶,一旦所有分片响应,它将把结果减less到最终列表,然后返回给客户端。 这意味着如果唯一条目的数量大于size ,返回的列表会稍微偏离并且不准确(可能是因为术语计数稍微偏离,甚至可能是一个本来应该处于最大尺寸的术语桶没有被退回)。 如果设置为0 ,则size将设置为Integer.MAX_VALUE