2016-06-30 42 views
0

而在elasticsearch搜索galaxy mobiles。 它會創建一個類似於下面的查詢,因爲galaxy應該在mobiles類別中進行搜索。'必須'和'應該'結合在布爾查詢不起作用

{ 
    "query": { 
    "bool": { 
     "should": [{ 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "operator": "and" 
      } 
     }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase_prefix" 
      } 
     }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase" 
      } 
     }], 
     "must": [{ 
      "term": { 
       "category": "mobiles" 
      } 
     }] 
    } 
    } 
} 

只有must條件的工作,should條件不工作。 上述查詢中是否有問題?

+0

你說的不工作呢?你在期待什麼? – femtoRgon

+0

我想要在標題或其他字段中使用銀河移動類別的產品。但它只給我手機類別 –

回答

0

如果應該和必須在同一水平上,應該不會有什麼影響。所以可能這是你想要的:

{ 
    "query": { 
    "bool": { 
     "must": { 
     "bool": { 
      "should": [{ 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "operator": "and" 
      } 
      }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase_prefix" 
      } 
      }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase" 
      } 
      }] 
     }}, 
     "must": { 
      "term": { 
       "category": "mobiles" 
      } 
     } 
    } 
    } 
} 
0

我得到了如下解決方案。它工作正常。

{ 
    "query": { 
    "filtered": { 
     "query": { 
      "bool": { 
       "should": [{ 
        "multi_match": { 
         "query": "galaxy", 
         "fields": ["title", "product_specs", "category", "brand", "os"], 
         "operator": "and" 
        } 
       }, { 
        "multi_match": { 
         "query": "galaxy", 
         "fields": ["title", "product_specs", "category", "brand", "os"], 
         "type": "phrase_prefix" 
        } 
       }, { 
        "multi_match": { 
         "query": "galaxy", 
         "fields": ["title", "product_specs", "category", "brand", "os"], 
         "type": "phrase" 
        } 
       }] 
      } 
     }, 
     "filter": { 
      "bool": { 
       "must": [{ 
        "term": { 
         "category": "mobiles" 
        } 
       }] 
      } 
     } 
    } 
} 
} 
0

如果你看一下bool彈性搜索文件,你會看到以下內容:

應該

子句(查詢)應該會出現匹配的文件內。在 布爾查詢中沒有must或filter子句,一個或多個應該 子句必須匹配一個文檔。可以使用minimum_should_match參數設置匹配 匹配的最小條數。

所以正確的查詢是:

{ 
    "query": { 
    "bool": { 
     "should": [{ 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "operator": "and" 
      } 
     }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase_prefix" 
      } 
     }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase" 
      } 
     }], 
     "minimum_should_match":1, 
     "must": [{ 
      "term": { 
       "category": "mobiles" 
      } 
     }] 
    } 
    } 
} 

請注意,我說的 「minimum_should_match」:1