【问题标题】:Search in multiple fields in ElasticSearch returns more results than expected在 ElasticSearch 中搜索多个字段会返回比预期更多的结果
【发布时间】:2021-05-25 02:25:32
【问题描述】:

我正在尝试使用 ElasticSearch 按国家或城市或两者进行搜索。

当我使用 USA 作为搜索词按国家/地区进行搜索时,我得到了这些正确的结果。

email country city
mike@example.com USA Portland
You Can Also USA Chicago

查询如下所示:

{
  "body": {
    "query": {
      "bool": {
        "must": [
          {
            "bool": {
              "should": [
                {
                  "match": {
                    "country": {
                      "query": "USA",
                      "operator": "and"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

问题是,如果我还想使用术语 Portland 按城市搜索,我预计只会得到一个结果

email country city
mike@example.com USA Portland

但我又得到了两个结果,就像我只按国家搜索时一样。

这两个字段的查询如下所示:

{
  "body": {
    "query": {
      "bool": {
        "must": [
          {
            "bool": {
              "should": [
                {
                  "match": {
                    "country": {
                      "query": "USA",
                      "operator": "and"
                    }
                  }
                }
              ]
            }
          },
          {
            "bool": {
              "should": [
                {
                  "match": {
                    "country": {
                      "query": "USA",
                      "operator": "and"
                    }
                  }
                },
                {
                  "match": {
                    "city": {
                      "query": "Portland",
                      "operator": "and"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

我做错了什么?

【问题讨论】:

    标签: elasticsearch match


    【解决方案1】:

    在您的情况下,无需使用多个 bool/should 子句和运算符(带有匹配查询)。

    添加一个工作示例

    按国家搜索

    {
      "query": {
        "bool": {
          "must": {
            "match": {
              "country": {
                "query": "USA"
              }
            }
          }
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "67676851",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.18232156,
            "_source": {
              "email": "mike@example.com",
              "country": "USA",
              "city": "Portland"
            }
          },
          {
            "_index": "67676851",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.18232156,
            "_source": {
              "email": "You Can Also",
              "country": "USA",
              "city": "Chicago"
            }
          }
        ]
    

    按城市搜索:

    {
      "query": {
        "bool": {
          "must": {
            "match": {
              "city": {
                "query": "Portland"
              }
            }
          }
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "67676851",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.6931471,
            "_source": {
              "email": "mike@example.com",
              "country": "USA",
              "city": "Portland"
            }
          }
        ]
    

    按城市和国家搜索:

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "city": "Portland"
              }
            },
            {
              "match": {
                "country": "USA"
              }
            }
          ]
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "67676851",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.8754687,
            "_source": {
              "email": "mike@example.com",
              "country": "USA",
              "city": "Portland"
            }
          }
        ]
    

    【讨论】:

    • 感谢您的回答。我使用多个 bool/should 的原因是因为除了国家和城市之外,我还有多个其他条件/字段要搜索。
    • 哦,还有一件事。我忘了提到,例如,选择多个城市并且只选择一个国家总是可能的情况。在这种情况下,must 不起作用,应该使用 should
    • @Cosmin 在第二个查询中,在一个 bool/should 子句中,您正在搜索具有 country: USA 的文档,在第二个 bool/should 子句中,您正在搜索同时具有 country:USA 和 @ 的文档987654335@。自从。两个文件都包含美国,所以很自然两个文件都会返回。
    • @Cosmin 您能解释一下您的具体要求吗?否则,我在上面的回答中给出的查询适用于您的 3 个案例
    • 简单来说,我想在过滤器之间建立AND 关系,并在单个过滤器的选项中建立OR 关系。
    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 2017-10-05
    • 2021-01-29
    • 1970-01-01
    • 2018-07-31
    • 2021-08-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多