重点是您正在使用的ElasticSearch正则表达式requires a full string match:
Lucene’s patterns are always anchored. The pattern provided must match the entire string.
因此,要匹配任何字符(但换行符除外),可以使用.*
模式:
match: { text: '.*google.*'}
^^ ^^
In ES6+, use regexp
insted of match
:
"query": {
"regexp": { "text": ".*google.*"}
}
还有一个变化是针对字符串可以有换行符的情况:match: { text: '(.|\n)*google(.|\n)*'}
.在ElasticSearch中,这个糟糕的(.|\n)*
是必须的,因为这个regex风格不允许任何[\s\S]
解决方案,也不允许任何DOTALL/Singleline标志."The Lucene regular expression engine is not Perl-compatible but supports a smaller range of operators."
However,如果您不打算匹配任何复杂的模式,也不需要进行单词边界判断,那么使用wildcard search就可以更好地执行regex搜索一个简单的子字符串:
{
"query": {
"wildcard": {
"text": {
"value": "*google*",
"boost": 1.0,
"rewrite": "constant_score"
}
}
}
}
See Wildcard search for more details.
NOTE:通配符模式还需要匹配整个输入字符串,因此
google*
finds all strings starting with google
*google*
查找所有字符串containing google
*google
查找所有字符串ending with google
Also, bear in mind the only pair of special characters in wildcard patterns:
?, which matches any single character
*, which can match zero or more characters, including an empty one