【问题标题】:Elasticsearch nested query with multiple and and or conditions具有多个 and 和 or 条件的 Elasticsearch 嵌套查询
【发布时间】:2021-06-16 13:18:33
【问题描述】:

我正在尝试在 elasticsearch 中生成嵌套的 mustshould 查询以模拟以下条件:

(  (brandsvisited ilike '%adidas%' or   brandsvisited ilike '%skechers%' ) or( placecategory ilike '%Pizza Restaurant Visitors%' or   placecategory ilike '%Grocery Store Visitors%'  ) and( gender='Male'  )  ) and polygonid = '1465'

我在 elasticsearch 中创建了以下查询。

"query": {
      "bool": {
          "must": [
              {"term": {"polygonid": "1465"}},
              {"term": {"gender": "Male"}},
              {
                  "bool": {
                      "should": [
                          {"term": {"brandsvisited": "adidas"}},
                          {"term": {"brandsvisited": "skechers"}}
                      ],"minimum_should_match": 1
                  }
              },
              {
                  "bool": {
                      "should": [
                          {"term": {"placecategory": "Pizza Restaurant Visitors"}},
                          {"term": {"placecategory": "Grocery Store Visitors"}}
                      ],"minimum_should_match": 1
                  }
              }
          ]
      }
  }

上述查询返回零个结果。但是,如果我删除最后一个布尔值应该查询,它会返回结果。不知道我哪里出错了。

【问题讨论】:

  • 如果您必须搜索“%Pizza Restaurant visitor%”,为什么还要使用术语查询?
  • @TusharShahi 我也可以使用 match...我对 elasticsearch 完全陌生,不确定哪个是正确的。
  • 检查这个stackoverflow.com/questions/26001002/…。查看 ESCoder 提到的匹配短语。
  • @TusharShahi 谢谢你的问题...我试过match,它开始返回数据

标签: elasticsearch elasticsearch-query


【解决方案1】:

假设placecategory 字段是文本类型。您需要将其替换为 placecategory.keyword 字段(如果您尚未定义任何显式索引映射)

Term query 返回在提供的字段中包含准确术语的文档。如果您尚未定义任何显式索引映射,则需要将 .keyword 添加到该字段。这使用关键字分析器而不是标准分析器。

如下所示修改您的查询

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "polygonid": "1465"
          }
        },
        {
          "term": {
            "gender": "Male"
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "brandsvisited": "adidas"
                }
              },
              {
                "term": {
                  "brandsvisited": "skechers"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "placecategory.keyword": "Pizza Restaurant Visitors"
                }
              },
              {
                "term": {
                  "placecategory.keyword": "Grocery Store Visitors"
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  }
}

如果您想在placecategory 中搜索确切的术语,那么您可以使用带有should 子句或terms query 的术语查询,否则,如果您想匹配placecategory 中的短语字段,则可以使用match_phrase查询

【讨论】:

  • @Apricot match 查询不会给您正确的答案,因为它会匹配所有在placecategory 字段中具有PizzaRestaurantVisitors 的文档。但是根据你的 SQL 查询match_phrase 会给你正确的答案
  • 非常感谢,我用关键字、match 和 match_phrase 尝试了我的用例术语的所有组合......您对match_phrase 的建议非常有效......感谢您一直以来的支持和详细说明。
  • @Apricot 很高兴我能帮助你:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
相关资源
最近更新 更多