【问题标题】:Does elasticsearch ignore dot in match_phrase?elasticsearch 是否忽略 match_phrase 中的点?
【发布时间】:2020-01-17 15:00:07
【问题描述】:

我正在使用match_phrase 进行如下搜索:

"match_phrase": {
                            "name": "XX .S"
                          }

它会找到名称为"name" : "XX S&#","name": "XX S Ltd" 的结果。它似乎在搜索中忽略了.。经过一番调查,dot 在索引期间可能会在对字符串中的单词进行标记时出现条纹。如果这是真的,我怎样才能使搜索将dot 视为常规字符? 如果不是真的,是什么原因造成的,我该如何解决?

我期望通过查询XX .S得到以下回复

A XX .S B
XX .S
XX .S11
XX .Sa

到目前为止,我发现的关于match_phrase 的唯一问题是它无法逃脱.。它适用于其他情况。

【问题讨论】:

  • 像我的回答一样使用通配符查询。不能通过 cmets 粘贴查询,将按网站过滤。

标签: elasticsearch


【解决方案1】:

您需要在创建索引时将“名称”定义为关键字类型。默认为文本类型,将由 Elastic Search 引擎标记。

name": {
    "type": "keyword"
}

使用通配符:

{
    "from": 0,
    "size": 200,
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "wildcard": {
                                    "name": {
                                        "wildcard": "*XX .S*",
                                        "boost": 1
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 1
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "_source": {
        "includes": [
            "name"
        ],
        "excludes": []
    }
}

【讨论】:

  • 感谢您的回答。如果我将其更改为keyword,则无法通过查询XX .S 来搜索XX .SAB XX .S A。我需要的是使. 成为常​​规字符。
  • 试试这个查询:{“from”:0,“size”:200,“query”:{“bool”:{“filter”:[{“bool”:{“must”: [ { "wildcard": { "name": { "wildcard": "Level1 .", "boost": 1 } } } ], "adjust_pure_negative": true, "boost": 1 } } ], "adjust_pure_negative": true, "boost": 1 } }, "_source": { "includes": [ "name" ], "excludes": [] } }
  • 我改了答案。
  • 为什么要使用嵌套的bool 查询?如果我删除前两个键 boolfilter 会有什么影响?
  • 也可以。嵌套是因为如果还有其他条件。
【解决方案2】:

您可以通过引入 /(不替换) 更多类似这样的 keyword 字段来保留旧的搜索逻辑。

{
  "foo": {
    "type" "text",
    "fields": {
      "raw": { 
        "type":  "keyword"
      }
    }
  }
}

您可以使用foo.raw 字段执行全文搜索。

这是您需要查询才能获得结果的内容

{
    "query": {
        "wildcard": {
            "foo.raw": "*XX .S*"
        }
    }
}

https://www.elastic.co/guide/en/elasticsearch/reference/7.5/multi-fields.html查看更多详情

最好的,

【讨论】:

  • 如果我这样做,搜索.XXX 将响应一些不包含. 的结果。因为它匹配旧的搜索逻辑。如果没有名称包含.XXX,我想回复一个空结果。
  • 您应该只搜索字段 foo.raw 并且它只匹配 .XXX 值。您可能会从foo 的实现中删除旧逻辑。这是因为 type=keyword 的字段不会被 ES 标记
  • 使用type=keyword,不支持对字段进行部分查询。我在帖子中添加了一些关键字查询不支持的示例。
  • 请使用wildcard匹配foo.raw查看更新后的查询
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多