我不認爲這是可能的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
}
}
]
}
}
}
然後你可以遍歷結果並在客戶端進行總結。
你可以發佈你的代碼,並在哪裏失敗? –
我是elasticsearch的新手,對於如何爲這個問題編寫彈性搜索查詢沒有太多想法。 – asurana2