在bool查询中,'must'和'should'结合起来不起作用

时间:2016-06-30 12:09:34

标签: php search elasticsearch lucene

在弹性搜索中搜索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条件不起作用。 上述查询中是否有任何问题。?

3 个答案:

答案 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"
            }
        }
    }
  }
}

答案 1 :(得分: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"
                    }
                }]
            }
        }
    }
 }
}

答案 2 :(得分: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