【问题标题】:Elastic Search: simple query not returning expected results弹性搜索:简单查询未返回预期结果
【发布时间】:2016-08-05 03:55:54
【问题描述】:

我使用的是 Elasticsearch 2.2.0,但这也发生在 1.x 版本中。

其中一个文本字段包含单词 google.com。当我尝试搜索谷歌时,ElasticSearch 不返回任何内容。但如果我搜索 google.com,它会返回包含它的文档。

我的查询很简单,如下所示:

query: {
  filtered: {
    query: {
      simple_query_string: {
        query: "google"
      }
    }
  }
}

我应该怎么做才能让 Elastic search 在我搜索 google 时返回文档?

【问题讨论】:

  • 您是否尝试过使用通配符查询?
  • @AlainIb 谷歌*?是的,在这种情况下,它返回我想要的文档 + 很多与查询不完全相关的其他文档。

标签: elasticsearch


【解决方案1】:

我用“通配符”查询做这种查询

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

{
  "query": {
    "bool": {
      "must": {
        "wildcard": {
          "fieldname": "google*" // replace the fieldname her
        }
      }
    }
  }
}

【讨论】:

  • 我将 simple_query 暴露给我的用户,因为他们有时会使用“term1 -term2”之类的东西。是不是可以通过只搜索google来调整simple_query以返回结果?看起来很简单,因为带有 LIKE 的 SQL 查询会返回这个结果(这很明显)。
【解决方案2】:

我建议阅读the documentation 了解_all 字段,因为simple_query_string 在未指定任何字段时使用此字段,这是您的情况。

_all 默认使用standard 分析器进行索引,这意味着google.com 不会在. 处拆分,而是将索引为google.com。这意味着搜索google.com 将在您的索引中查找该术语,而在您的文本中您有google

在 Elasticsearch 中搜索不仅与查询有关,还与您索引数据的方式有关。它可能不像 使用 LIKE 的 SQL 查询那么简单,但同时,ES 在 insert 时为您提供比 SQL 更强大的功能。

根据您对simple_query 的要求,您有多种选择:

  • 更改_all字段的分析器:
{
  "settings": {
    "analysis": {
      "analyzer": {
        "letter": {
          "type": "custom",
          "tokenizer": "letter",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  }, 
  "mappings": {
    "test": {
      "_all": {
        "analyzer": "letter"
      }

和查询:

  "query": {
    "simple_query_string": {
      "query": "google"
    }
  }
  • 更改field 的分析器并**使用query_string_query 中的该字段:
{
  "settings": {
    "analysis": {
      "analyzer": {
        "letter": {
          "type": "custom",
          "tokenizer": "letter",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  }, 
  "mappings": {
    "test": {
      "properties": {
        "text": {
          "type": "string",
          "analyzer": "letter"
        }
      }

还有查询:

  "query": {
    "simple_query_string": {
      "query": "google",
      "fields": ["text"]
    }
  }

【讨论】:

    猜你喜欢
    • 2021-07-12
    • 2021-02-10
    • 2021-01-18
    • 2019-07-20
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    相关资源
    最近更新 更多