短语中的弹性搜索通配符

问题描述 投票:0回答:1

[目前,我们有一个搜索可进行文字全字搜索。例如在文档中

你好世界你好,世界再次你好美丽的世界世界问好

q="Hell" returns nothing
q="Hello" returns all 4
q="Hello w" returns nothing
q="hello world" returns the first 2

我们现在需要进行通配符匹配。我们可以使用*并从查询周围删除引号来实现此目的。但是,删除引号也会使查找不再是短语查找。例如

q=Hell* returns all 4 (not a problem)
q=Hello w* returns all 4 (problem; we wanted only the first two)

有人知道防止“ hello”和“ w *”分裂为两个单独的搜索的方法吗?

elasticsearch
1个回答
0
投票

q类型更改为关键字是一种解决方案,然后创建一个小写的规范化器。

{
  "mappings": {
    "doc": {
      "properties": {
        "q": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",    
              "normalizer": "my_custom_normalizer"
            }
          },
          "fielddata": true
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "normalizer": {
        "my_custom_normalizer": {
          "type": "custom",
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  }
}

搜索查询:

GET index/_search
{
  "query": {
    "wildcard": {
      "q.keyword": {
        "value": "hello w*"
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.