Elasticsearch的wildcard空格问题

因业务需要,之前的个别后台搜索功能采用了wildcard通配搜索,虽然性能很差,但效果最好。

但近日接到反馈,说是带有空格的搜索词无法通过wildcard匹配到结果。

应用wildcard的字段是keyword类型,当搜索是:”*abc def*”的时候无法找到结果,但是仅搜”*abc*”或者”*def*”则可以搜到。

有一篇stackoverflow:https://stackoverflow.com/questions/29377705/elastic-wild-card-search-not-working-with-space ,说到wildcard只能支持单个term的通配,中间带有空格是不行的:

The wildcard query works only with a single token. What you are trying to do is to find one token followed by another token with the second token being specified by a prefix. This can be achieved by using match phrase prefix query. Your query would look like this:

QueryBuilders.matchPhrasePrefixQuery(ElasticSearchUtil.FIELDNAME, searchValue)

Please note that the searchValue shouldn’t have “*” at the end.

我又查到了一个2011年的博客,提到了底层lucene对空格等特殊字符的处理问题:https://dismantledtech.wordpress.com/2011/05/15/handling-spaces-in-lucene-wild-card-searches/

This caused a problem, because Lucene’s query parser interprets the space in the search term as a break in the actual query, and doesn’t include it in the search.

查lucene官方手册,可以看到对于lucene来说,它的语法包含了一些特殊字符,如果我们希望让lucene把wildcard query的”*abc def*”视作一个整体,那么需要转义空格”*abc\ def*”,这样lucene就不会认为空格是拆分2个term的意思了。

然而我经过实践发现,在ES5.X中即便转义也没有达到效果,我的PHP代码如下:

所以,也许我们需要看一下ES在处理wildcard query的时候做了什么事情,这个留作TODO。

另外,在之前的博客我也说过,利用match phrase短语匹配并不能完美解决精确通配的问题,因为短语匹配也依赖分词,所以像”abc de”这样的搜索条件是无法通过match phrase召回数据的。

如果文章帮助您解决了工作难题,您可以帮我点击屏幕上的任意广告,或者赞助少量费用来支持我的持续创作,谢谢~