gpt4 book ai didi

json - 如何在 Elasticsearch 中进行部分匹配?

转载 作者:行者123 更新时间:2023-11-29 02:43:40 25 4
gpt4 key购买 nike

我有一个类似 http://drive.google.com 的链接我想匹配链接中的“google”。

我有:

query: {
bool : {
must: {
match: { text: 'google'}
}
}
}

但这仅在整个文本为“google”时匹配(不区分大小写,因此它也匹配 Google 或 GooGlE 等)。如何匹配另一个字符串中的“google”?

最佳答案

重点是您使用的 ElasticSearch 正则表达式 requires a full string match :

Lucene’s patterns are always anchored. The pattern provided must match the entire string.

因此,要匹配任何字符(但换行符除外),您可以使用 .* 模式:

match: { text: '.*google.*'}
^^ ^^

在 ES6+ 中,使用 regexp 代替 match:

"query": {
"regexp": { "text": ".*google.*"}
}

另一种变体适用于您的字符串可以包含换行符的情况:match: { text: '(.|\n)*google(.|\n)*'}。这个可怕的 (.|\n)* 在 ElasticSearch 中是必须的,因为这种正则表达式风格不允许任何 [\s\S] 变通方法,也不允许任何 DOTALL/Singleline 标志. "The Lucene regular expression engine is not Perl-compatible but supports a smaller range of operators."

但是,如果您不打算匹配任何复杂的模式并且不需要单词边界检查,则仅通过通配符搜索可以更好地执行正则表达式搜索纯子字符串:

{
"query": {
"wildcard": {
"text": {
"value": "*google*",
"boost": 1.0,
"rewrite": "constant_score"
}
}
}
}

参见 Wildcard search了解更多详情。

注意:通配符模式也需要匹配整个输入字符串,因此

  • google* 查找所有开头的字符串 google
  • *google* 查找所有包含 google
  • 的字符串
  • *google 查找所有结尾的字符串 google

此外,请记住通配符模式中唯一的一对特殊字符:

?, which matches any single character
*, which can match zero or more characters, including an empty one

关于json - 如何在 Elasticsearch 中进行部分匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37709100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com