【问题标题】:ElasticSearch Bool Filter with a Phrase (instead of a single word/tag)带有短语的 ElasticSearch Bool 过滤器(而不是单个单词/标签)
【发布时间】:2013-02-09 23:45:57
【问题描述】:

在弹性搜索中,这个过滤器

{
  "bool": {
    "must": {
      "term": {
        "article.title": "google"
      }
    }
  }
}

正确返回标题中带有“google”的文章。

然而,

{
  "bool": {
    "must": {
      "term": {
        "article.title": "google earth"
      }
    }
  }
}

不返回任何结果,尽管有些文章的标题中包含确切的词“google earth”。我希望它这样做。

完整的查询:

{
  "size": 200,
  "filter": {
    "bool": {
      "must": {
        "term": {
          "article.title": "google maps"
        }
      }
    }
  },
  {
    "range": {
      "created_date": {
        "from": "2013-01-11T02:14:03.352Z"
      }
    }
  }]
}
}

如您所见,我没有“查询”——只有过滤器、大小和范围。所以我认为 ElasticSearch 正在使用默认分析器...?

我误会了什么?


编辑:对于那些正在寻找解决方案的人,这是我的过滤器:

{
  "query": {
    "bool": {
      "must": {
        "must_match": {
          "article.title": "google earth"
        }
      }
    }
  }
}

节点(1)我们用“query”包装了布尔过滤器,(2)“term”更改为“must_match”,这导致整个短语被匹配(而不是“match”,它将搜索article.title 与谷歌地球上的标准分析器)。

完整的查询如下所示:

{
  "size": 200,
  "filter": {
    "query": {
      "bool": {
        "must": {
          "must_match": {
            "article.title": "google earth"
          }
        }
      }
    }
  }
}

FWIW,我在“过滤器”字段(而不是使用标准查询)中有这个条件的原因是有时我想使用“must_not”而不是“must_not”,有时我还添加其他查询的元素。

【问题讨论】:

  • 您的分析器对于 article.title 的外观如何?如果您使用“关键字”以外的任何内容分析该字段,则该字段将被标记为 [google] 和 [earth],这将使其无法匹配。
  • 嗯,查询中的文章是否被转移到过滤器?我更新了我的问题以说明我的意思。

标签: elasticsearch


【解决方案1】:

Elasticsearch 根本没有使用分析器,因为您使用了 term query,它会查找准确的术语。

您的title 字段已被分析(除非您另有指定),因此"google earth" 将被索引为两个术语["google","earth"]。这就是为什么 term 查询 "google" 有效,但 term 查询 "google earth" 无效 - 确切的术语不存在。

如果您改用match query,那么您的查询词将在搜索前进行分析。

【讨论】:

    【解决方案2】:

    对于最近遇到这个问题的人,请注意使用更简洁的表示方式

    {"query":{"bool":{"must":{"must_match":{"article.title":"google earth"}}}}}
    

    {"query":{"match_phrase":{"article.title":"google earth"}}}
    

    【讨论】:

      【解决方案3】:

      我通过分解传递的短语来解决这个问题,所以只是改变。

      {"bool":{"must":{"term":{"article.title":"google earth"}}}}
      

      {"bool":{"must":{"term":{"article.title":["google", "earth"]}}}}
      

      它不漂亮,如果你有很多查询,它可能会太慢,但它可以工作。

      注意,我刚刚发现这也会返回带有“google”或“earth”的任何结果。

      【讨论】:

        【解决方案4】:

        使用 Elasticsearch 5.4.2.,我的解决方案演变为以下解决方案:

        {"query": {
             "bool": {
                 "must": {
                     "match_phrase": {
                         "article.title": "google earth"}}}}}
        

        希望这对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-12
          • 2016-05-05
          相关资源
          最近更新 更多