【问题标题】:Elasticsearch with grouped query_string带有分组查询字符串的 Elasticsearch
【发布时间】:2020-06-01 05:21:48
【问题描述】:
{
   "query":
   {
      "query_string" :
      {
         "query" : "((name:the_search_phrase) OR (keywords:the_search_phrase)) AND (city:Sydney, Australia)"
      }
   }
}

弹性搜索新手。根据此处的文档构建 JSON:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

但是,查询运行时,除Sydney, Australia 以外的城市也会返回结果。为什么 AND 部分不起作用?

我希望搜索词组与名称、关键字中的一个或两个匹配,但城市应该严格来说是悉尼。

【问题讨论】:

    标签: elasticsearch elasticsearch-php


    【解决方案1】:

    你正在做的是一个全文查询。 city:Sydney, Australia 似乎是一个过滤查询。就像 SQL 中的 WHERE 子句。你最好使用过滤查询。

    boolean query为例,

    可能是这样的,

        {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "the_search_term",
                "fields": [
                  "name",
                  "keywords"
                ]
              }
            }
          ],
          "filter": [
            {
              "match": {
                "city": "Sydney, Australia"
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • { "query": { "bool" : { "should": [ {"match" : {"name" : "The search Phrase"}}, {"match" : {"keywords" : "The search Phrase"}} ], "filter" : { "term" : { "city" : "Melbourne, Australia" } } } } } 我改写成这样,但是对于任何 搜索短语city 都没有结果。
    • 我的理解有什么错误吗?
    • 更新了一个例子。
    • 感谢您的示例。但是,这也适用于澳大利亚悉尼以外的城市。
    • 你在尝试http://your-ES-host:es-port/you-index/_search?q=city:"Sydney, Australia"时看到了什么你也看到其他城市的记录吗?您的“城市”字段中是否有多个值用于相同的记录,例如数组?
    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2018-06-16
    • 2014-12-09
    • 2015-04-09
    • 2011-05-03
    • 1970-01-01
    相关资源
    最近更新 更多