首页 > 解决方案 > Elasticsearch 通配符使用

问题描述

问题

在弹性搜索上使用通配符运行查询时,不会返回预期结果。

要复制问题:

版本 5.5.1

创建索引

放置/示例测试/

{
  "mappings": {
    "Change": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "searchProperties": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "value": {
              "type": "text",
              "analyzer": "standard"
            }
          }
        }
      }
    }
  }
}

将文档添加到索引

发布 /example-test/Change/01_MEP_01

{
  "id": "01_MEP_01",  
  "searchProperties": [
    {
      "key": "a.x",
      "value": "You take the red pill you stay in Wonderland, and I show you how deep the rabbit hole goes"
    }
  ]
}

观察索引中的文档

获取 /example-test/Change/_search

运行查询

POST /example-test/Change/_search

{      
  "query": {
    "bool": {
      "must": [        
        {
          "query_string": {
            "query": "you ta",
            "default_operator": "and",
            "allow_leading_wildcard": true,
            "analyze_wildcard": true,
            "fields": [
              "_all"
            ]
          }
        }
      ]
    }
  }
}

从这里开始,我将只指定查询的“查询”部分。我已经为几个例子运行了这个:

+-------------------+-------+
|       Query       | Found |
+-------------------+-------+
| you ta            | false |
| you ta*           | true  |
| you take*         | true  |
| land              | false |
| *land*            | true  |
| *land,*           | false |
| *land,            | false |
| wonderland        | true  |
| wonderland,       | true  |
| wonderlan*,       | false |
| \\*wonderland,\\* | true  |
+-------------------+-------+

注意 - 在最后一个示例中,通过阅读此处的文档https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string添加了通配符之前的反斜杠(可能是错误的) -query.html ) 声明的地方:

另一种选择是在查询字符串本身中提供通配符字段搜索(正确转义 * 符号)

这只是在查询中指定字段名称时吗?

问题

如果我想让我的用户搜索“land”并返回文档(因为它包含“wonderland”一词),我需要如何使用通配符来返回我的文档?

更多信息

看来逗号在这里引起了问题。这似乎很奇怪。查看运行生成的token:

发布 /example-test/_analyze

{
  "analyzer": "standard",
  "text": "You take the red pill you stay in Wonderland, and I show you how deep the rabbit hole goes"
}

我们可以看到“Wonderland”的结果是令牌“wonderland”。我的理解是查询将通过相同的分析器(https://www.elastic.co/guide/en/elasticsearch/reference/current/search-analyzer.html),所以应该产生相同的标记?看来不是这样的?

标签: elasticsearchwildcard

解决方案


推荐阅读