【问题标题】:'must' and 'should' combined in bool query not working“必须”和“应该”组合在布尔查询中不起作用
【发布时间】:2016-11-02 12:36:13
【问题描述】:

在 elasticsearch 中搜索 galaxy mobiles 时。 它会创建一个如下所示的查询,因为应在 mobiles 类别中搜索 galaxy

{
  "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 条件无效。 上面的查询有什么问题吗?

【问题讨论】:

  • 不工作是什么意思?你在期待什么?
  • 我想要移动类别的产品,标题或其他字段中包含银河。但它只给了我手机类别

标签: php search elasticsearch lucene


【解决方案1】:

如果应该和必须在同一级别上,那么应该没有任何影响。所以这可能就是你想要的:

{
  "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"
            }
        }
    }
  }
}

【讨论】:

    【解决方案2】:

    我在下面得到了这样的解决方案。它工作正常。

    {
      "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"
                        }
                    }]
                }
            }
        }
     }
    }
    

    【讨论】:

      【解决方案3】:

      如果您查看bool 弹性搜索文档,您将看到以下内容:

      应该

      子句(查询)应该出现在匹配的文档中。在一个 没有必须或过滤子句的布尔查询,一个或多个应该 子句必须与文档匹配。 should 子句的最小数量 可以使用 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

      【讨论】:

        猜你喜欢
        • 2017-10-30
        • 1970-01-01
        • 2016-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        相关资源
        最近更新 更多