2015-04-15 30 views
0

我写的elasticsearch查询下面的情形:Elasticsearch:过滤头文件为每一个独特的ID

- field1 field2 
- 2015  20 
- 2015  14 
- 2014  39 
- 2013  76 
- 2013  2 
- 2013  55 

我想找到的field2总和为每一个独特field1这样field2对于field1最大。 例如在这种情况下,我想要value = 20 + 39 + 76

什么是将返回此值的elasticsearch查询?

+0

你可以发布你的代码,并在哪里失败? –

+0

我是elasticsearch的新手,对于如何为这个问题编写弹性搜索查询没有太多想法。 – asurana2

回答

0

我不认为这是可能的elasticsearch 1.x与一个单一的查询。 在2.0中,我们可能会有这样一个功能,如减速器(请参阅:https://github.com/elastic/elasticsearch/issues/8110)。

你可以让你的任务(由FIELD1分组的场2最大值)这样的第一部分:

DELETE /test_index 

PUT /test_index 
{ 
    "settings": { 
     "number_of_shards": 1 
    } 
} 

POST /test_index/_bulk 
{"index":{"_index":"test_index","_type":"doc","_id":1}} 
{"field1":2015,"field2":20} 
{"index":{"_index":"test_index","_type":"doc","_id":2}} 
{"field1":2015,"field2":14} 
{"index":{"_index":"test_index","_type":"doc","_id":3}} 
{"field1":2014,"field2":39} 
{"index":{"_index":"test_index","_type":"doc","_id":4}} 
{"field1":2013,"field2":76} 
{"index":{"_index":"test_index","_type":"doc","_id":5}} 
{"field1":2013,"field2":2} 
{"index":{"_index":"test_index","_type":"doc","_id":6}} 
{"field1":2013,"field2":55} 

POST /test_index/_search 
{ 
    "size": 0, 
    "aggs": { 
    "field1_group": { 
     "terms": { 
     "field": "field1", 
     "size": 0, 
     "order": { 
      "maksior": "asc" 
     } 
     }, 
     "aggs": { 
     "maksior": { 
      "max": { 
      "field": "field2" 
      } 
     } 
     } 
    } 
    } 
} 

,这将给你:

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 6, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "field1_group": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
       "key": 2015, 
       "doc_count": 2, 
       "maksior": { 
        "value": 20 
       } 
      }, 
      { 
       "key": 2014, 
       "doc_count": 1, 
       "maksior": { 
        "value": 39 
       } 
      }, 
      { 
       "key": 2013, 
       "doc_count": 3, 
       "maksior": { 
        "value": 76 
       } 
      } 
     ] 
     } 
    } 
} 

然后你可以遍历结果并在客户端进行总结。

+0

实际上,使用1.4你可以尝试脚本化的度量聚合http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html –

+0

感谢gruby的回应。我得到了类似的查询,但每个field1的field2数量很大,所以不想总结应用程序端。我使用ES 1.4.2所以脚本聚合我可以尝试,但链接不工作。 – asurana2

+0

@ asurana2与建议的查询,你会得到尽可能多的结果,因为field1有不同的值(不管有多少field2),所以我没有在这里看到问题。你能解释一下吗? –