2017-06-29 101 views
0

我有索引名爲字典,其中包含像關鍵字,映射關鍵字和類別過濾器字段。多個子查詢裏面一個查詢elasticsearch

Keyword Mapped Keyowrd  Category 
------- --------------  -------- 
apple  apple iphone   smartphones 
apple  apple watch   smart watches 
apple  apple ipad   tablets 

因此,如果用戶搜索蘋果,內部查詢將搜索帶有各自類別的映射關鍵字,如下查詢。

SELECT * FROM products where (title= "*apple*" AND title="*iphone*" and category="smartphones") OR (title= "*apple*" AND title="*ipad*" and category="tablets") OR (title= "*apple*" AND title="*watch*" and category="smart watches") 

下面是我寫的相應的彈性搜索查詢。

{ 
    "query": { 
     "bool": { 
     "should": [ 
      { 
       "bool": { 
        "must": [ 
        { 
         "match" : { 
          "title" : { 
           "query" : "apple iphone", 
           "operator" : "and" 
          } 
         } 
        }, 
        { 
         "term": { 
          "category.raw": "smartphones" 
         } 
        } 
        ] 
       } 
      }, 
      { 
       "bool": { 
        "must": [ 
        { 
         "match" : { 
          "title" : { 
           "query" : "apple watch", 
           "operator" : "and" 
          } 
         } 
        }, 
        { 
         "term": { 
          "category.raw": "smartwatch" 
         } 
        } 
        ] 
       } 
      }, 
      { 
       "bool": { 
        "must": [ 
        { 
         "match" : { 
          "title" : { 
           "query" : "apple ipad", 
           "operator" : "and" 
          } 
         } 
        }, 
        { 
         "term": { 
          "category.raw": "tablets" 
         } 
        } 
        ] 
       } 
      } 
     ], 
     "minimum_should_match": 1 
     } 
    } 
} 
  • 是上面的查詢嗎?上述查詢中需要的任何更改?
  • 有沒有辦法通過在這個查詢中添加一些參數來獲得elasticsearch中每個子查詢的前10個結果?

回答

0

是的,您的查詢看起來很好,據我所知。 "minimum_should_match": 1並不是真的必要,那是默認行爲。

您可能會使用function_score查詢(可能與script_score)強加這種邏輯,但我認爲更好的方法是隻執行三個不同的查詢並獲取每個查詢的結果。如果您想要在一個請求中執行這些多個查詢,可以使用Multi Search API來完成。