2017-03-29 97 views
0

我想通過字段進行搜索了一些成果和其他領域(「myScore的」),對它們進行排序獲得不同的記錄,這裏是ES(5.2.2)查詢:Elasticsearch:由某一特定領域

{ 
    "sort": [ 
    {"myscore": {"order" :"desc"}} 
    ], 
    "query": { 
    "query_string" : { 
     "query" : "(field1:foo) AND (field2:bar)" 
    } 
    } 
} 

那麼,我能得到這個:

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 3, 
     "max_score": null, 
     "hits": [ 
      { 
       "_index": "fooindex", 
       "_type": "footype", 
       "_id": "1", 
       "_score": null, 
       "_source": { 
        "field1": "foo", 
        "field2": "bar", 
        "x_id": "x001", 
        "myscore": 0.9 
       }, 
       "sort": [ 
        0.9 
       ] 
      }, 
      { 
       "_index": "fooindex", 
       "_type": "footype", 
       "_id": "2", 
       "_score": null, 
       "_source": { 
        "field1": "foo", 
        "field2": "bar", 
        "x_id": "x001", 
        "myscore": 0.8 
       }, 
       "sort": [ 
        0.8 
       ] 
      }, 
      { 
       "_index": "fooindex", 
       "_type": "footype", 
       "_id": "3", 
       "_score": null, 
       "_source": { 
        "field1": "foo", 
        "field2": "bar", 
        "x_id": "x002", 
        "myscore": 0.7 
       }, 
       "sort": [ 
        0.7 
       ] 
      } 
     ] 
    } 
} 

但是,我想根據現場得到不同的結果 「X_ID」,像這樣:

{ 
    "_index": "fooindex", 
    "_type": "footype", 
    "_id": "1", 
    "_score": null, 
    "_source": { 
     "field1": "foo", 
     "field2": "bar", 
     "x_id": "x001", 
     "myscore": 0.9 
    }, 
    "sort": [ 
     0.9 
    ] 
}, 
{ 
    "_index": "fooindex", 
    "_type": "footype", 
    "_id": "3", 
    "_score": null, 
    "_source": { 
     "field1": "foo", 
     "field2": "bar", 
     "x_id": "x002", 
     "myscore": 0.7 
    }, 
    "sort": [ 
     0.7 
    ] 
} 

類似的SQL將是「從腳步羣組中選擇* x_id」;

我試過聚集:

"aggs": { 
    "unique_xid": { 
    "terms": { 
     "field": "x_id" 
    } 
    } 
}, 

其結果將是:

"aggregations": { 
    "unique_ids": { 
     "buckets": [ 
     { 
      "key": "x001", 
      "doc_count": 2 
     }, 
     { 
      "key": "x002", 
      "doc_count": 1 
     } 
     ] 
    } 
} 

的問題是,聚集的結果泄漏現場的信息和他們排序「算」不「將myScore 」。有什麼方法可以通過指定字段獲得不同的結果嗎?

+0

在你的' x001'桶你意識到有兩個文件,對吧?那麼應該將哪個「myscore」考慮在內以對桶進行分類?最大的一個? – Val

+0

@Val是的,最大的一個。 – user7783308

回答

0

由於您的桶可能包含多個文件,並要使用的這些文件中myscore最大值,以你的水桶進行排序,那麼你可以做這樣的:

"aggs": { 
    "unique_xid": { 
     "terms": { 
     "field": "x_id", 
     "order": { 
      "score": "desc" 
     } 
     }, 
     "aggs": { 
     "score": { 
      "max": { 
       "field": "myscore" 
      } 
     } 
     } 
    } 
}, 
+0

無需添加大小:10,因爲這是默認值;-) – Val

+0

很高興幫助! – Val

+0

是的,在我的情況下,大小已達1000,只是讓人們知道如何增加聚合大小。 – user7783308