2016-04-09 25 views
0

不確定如何在Elasticsearch中執行SQL如聯合。我試過bool查詢,但它不符合我的要求。例如,文檔結構是Elasticsearch單個請求做聯合查詢返回頂端N

{ 
    "id": "123", 
    "authorId": 28, 
    "title": "Five Ways to Tap into...", 
    "byLine": "ashd jsabbdjs international", 
    "category": "Cat1" 
} 

我需要找到前5名匹配「稱號」,在每個「類別」當用戶鍵入的東西。這可以通過對Elasticsearch的多個查詢來完成,但我想知道是否有其他方法可以在一個請求中執行。

回答

1

使用與top_hits子聚集的聚集:

{ 
    "size": 0, 
    "query": {"match_all": {}}, 
    "aggs": { 
    "categories": { 
     "terms": { 
     "field": "category", 
     "size": 10 
     }, 
     "aggs": { 
     "top_5": { 
      "top_hits": { 
      "size": 5 
      } 
     } 
     } 
    } 
    } 
} 
+0

非常感謝安德烈·多桶。有用! – Jay

0

下面是查詢返回基於「類別」

{ 
    "size": 0, 
    "query": { 
     "bool": { 
     "must": [ 
      { 
       "terms": { 
        "authorId": [ 
        1, 
        28 
        ] 
       } 
      } 
     ], 
     "should": [ 
      { 
       "query_string": { 
        "query": "*int*", 
        "fields": [ 
        "title^2", 
        "byLine^1" 
        ] 
       } 
      } 
     ] 
     } 
    }, 
    "aggs": { 
     "categories": { 
     "terms": { 
      "field": "category", 
      "size": 10 
     }, 
     "aggs": { 
      "top_5": { 
       "top_hits": { 
        "size": 5 
       } 
      } 
     } 
     } 
    } 
}