2016-08-11 118 views
0

我的目的是結合2個多重匹配查詢,如下所示。我希望MPN或SKU字段必須匹配每個關鍵字。因此,關鍵字「hp」和「301」都必須存在於MPN或SKU中。 或Name或Name.raw應該具有關鍵字「hp」或「301」中的一個。 完全匹配是MPN/SKU應該有更高的分數。 我寫了如下查詢,但它不返回任何結果,雖然有文件有這些條件。 我的問題是,如何結合多個匹配查詢?

1) Does must query require keyword to be found in both SKU and MPN 
for the multi_match query? 

2) Combination of must and should is AND or OR? it looks like AND 
for me as it doesn't return any result. if it is AND, how to make it 
as OR? I cant find any operator on bool. I tried to wrap in another 
should query but it didnt help. 

我也很欣賞我的目的的任何查詢建議。

"from": 0, 
     "size": 10, 
     "explain": true, 
     "query": { 
      "bool": { 
      "must": [ 
       { 
        "multi_match": { 
         "type": "best_fields", 
         "query": "hp 301", 
         "fields": [      
         "MPN^9", 
         "SKU^8"    
         ], 
       "operator": "and" 
        } 
       } 
      ], 
      "should": [ 
       {"multi_match": { 
         "type": "best_fields", 
        "query": "hp 301", 
        "fields": [ 
         "Name.raw2^7.5", 
         "Name^7" 
         ] 
       }} 
      ] 
      } 
     } 

回答

0

我假設這個問題與這個問題有關。 How do && and || work constructing queries in NEST?

如果我理解正確的話,一定的組合,並應與作爲必應作爲一個助推器,他只要說下面

bool 
>  must 
>   term1 
>  should 
>   term2 
>   term3 
>   term4 

這是因爲一個布爾查詢有一個必須條款應該開始充當推動因素。所以在

以前你能拿回來,只有包含字詞1這是 顯然不是你輸入的嚴格意義上的布爾想要的結果。

因此,我將我的查詢改爲2查詢,其中一個查詢與「AND」運算符。所以這個行爲就像MUST一樣,因爲它會強制所有的搜索關鍵字在字段中找到。

"from": 0, 
     "size": 10, 
     "explain": true, 
     "query": { 
      "bool": { 
      "should": [ 
       { 
        "multi_match": { 
         "type": "best_fields", 
         "query": "hp 301", 
         "fields": [      
         "MPN^9", 
         "SKU^8"    
         ], 
       "operator": "and" 
        } 
       }    , 
       { 
       "multi_match": { 
         "type": "best_fields", 
        "query": "hp 301", 
        "fields": [ 
         "Name.raw2^7.5", 
         "Name^7" 
         ] 
       } 
       } 
      ] 

      } 
     }