带短语的 ElasticSearch Bool 过滤器(而不是单个单词/标签)

标签 elasticsearch

在 Elasticsearch 中,这个过滤器

{
  "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”包裹了 bool 过滤器,(2) “term”变成了“must_match”,这导致整个短语被匹配(而不是“match”,它会搜索article.title 与谷歌地球上的标准分析器)。

完整的查询如下所示:

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

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

最佳答案

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

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

如果您使用 match query相反,您的查询字词将在搜索之前进行分析。

关于带短语的 ElasticSearch Bool 过滤器(而不是单个单词/标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14792942/

相关文章:

elasticsearch - Elasticsearch 协调节点

elasticsearch - Elasticsearch 中用于通配符搜索的 ngram

elasticsearch - 自定义char_filter用于 Elasticsearch 中的模式

docker - Docker的elasticsearch 5.5.3 Java客户端API NoNodeAvailableException

elasticsearch - 在Elasticsearch中使用GET Request查询时删除停用词

elasticsearch - 没有得到所有结果

nginx - 限制直接访问端口,但允许在 Nginx 中进行端口转发

elasticsearch - 在Elasticsearch索引中为每个字段建立索引都有不利之处吗?

javascript - 一般 Node.js/javascript 问题和事件循环

elasticsearch - 没有注册 [match] 的查询