Elasticsearch实战 | 怎么通过Elasticsearch实现模糊查询?

1、问题分析

首先这里所说的模糊查询是指类似mysql的like关键字左右模糊的查询过滤。
举个栗子:搜索 社保登记 ,能匹配出 我要社保登记查询 ,不能匹配出 社保缴纳登记”
然后啰嗦下Elasticsearch是一个全文检索引擎,我们通常应用它来进行文本的分词匹配过滤。也就是说通常我们不会用Elasticsearch来做这种模糊查询,这种比起全文检索属于更精准的查询。
当然,Elasticsearch也是支持模糊查询的。它有两种思路来实现,一种是使用wildcard查询,一种是使用短语查询(match_phrase,match_phrase_prefix)。然而wildcard查询官方建议是尽量避免使用,因为有性能问题。
所以我们下面描述使用短语查询的实现方式,顺便比较下match_phrase 和match_phrase_prefix的异同。

2、match_phrase

查询规则描述
目标文本包含检索短语的所有能分出的分词
目标文本和检索短语里的分词出现顺序完全一致,且中间不夹杂其他分词
举个栗子:
检索短语:电信业务经营
目标文本1:电信业务经营发生场所
目标文本2:电信业务经营者

假设目标字段为title,其定义为

"title" : {
   "type" : "text",
   "analyzer" : "ik_smart"
}

使用match_phrase进行如下查询发现能成功匹配到目标文本1(下面的语句省略了索引名),看起来已经实现了模糊查询

GET /_search
{
  "query": {"match_phrase": {
    "title": "电信经营业务"
  }}
}

但是我们会惊讶的发现它没有匹配到目标文本2,这显然不符合我们一般的预期。为什么呢?下面我们来分析下原因
使用ik_smart分词器对检索短语分词

GET _analyze
{
  "analyzer": "ik_smart",
  "text": ["电信业务经营"]
}

分词结果为:

{
  "tokens" : [
    {
      "token" : "电信业务",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "经营",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}

接着使用ik_smart分词器分别对目标文本1和目标文本2进行分析
目标文本1分词结果:

{
  "tokens" : [
    {
      "token" : "电信业务",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "经营",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "发生",
      "start_offset" : 6,
      "end_offset" : 8,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "场所",
      "start_offset" : 8,
      "end_offset" : 10,
      "type" : "CN_WORD",
      "position" : 3
    }
  ]
}

目标文本2分词结果为:

{
  "tokens" : [
    {
      "token" : "电信业务",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "经营者",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}

我们比对检索短语和目标文本的分词结果。发现目标文本1分出的词完全包含检索短语的词,且顺序一致,中间没有其他词。但文本2没有分出经营,而是有一个分词经营者,这里显然是不符合match_phrase的过滤规则,没有包含“经营“这个分词。所以目标文本2没有被匹配出来。
那是不是因为这里的分词器是ik_smart分的太粗了呢?我们试下使用ik_max_word分词看下分词结果。
检索短语ik_max_word分词结果:

{
  "tokens" : [
    {
      "token" : "电信业务",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "电信业",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "电信",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "业务",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "经营",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 4
    }
  ]
}

目标文本2的ik_max_word分词结果为:

{
  "tokens" : [
    {
      "token" : "电信业务",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "电信业",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "电信",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "业务",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "经营者",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "经营",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 5
    }
  ]
}

如上所见,这下目标文本2分出的词已经完成包含检索短语分出的词了。但是查询结果仍然是匹配不到的,问题出在中间夹杂了其他词。

我们再近距离对比下:
电信业务营业:电信业务 电信业 电信 业务 经营
电信业务营业者:电信业务 电信业 电信 业务 经营者 经营

是的,业务和经营中间出现了一个“经营者”。这还是不符合match_phrase的过滤规则。那如果我们就是想要匹配到目标文本2应该怎么办呢?那就可以试试使用match_phrase_prefix

3、match_phrase_prefix

直观来看,match_phrase_prefix多了个prefix。这个prefix是指可以用最后一个分词作为前缀去匹配,其他的规则和match_phrase一样。还是用上面的例子,检索短语“电信业务营业”使用ik_smart分词的最后一个分词是"营业"。对最后一个分词作为前缀匹配,那就是形如“经营*”的匹配方式,那其实就能匹配到目标文本2中多出来的那个“经营者”。
可以使用如下查询验证(下面的语句省略了索引名):

GET /_search
{
  "query": {
  "match_phrase_prefix": {
    "title": {
      "query": "电信业务营业",
      "max_expansions": 50
    }
  }}
}

max_expansions是指能模糊匹配的位数,数值设置的越大能模糊匹配到的位数越大。官方建议是50。到这里,我们就已经实现了模糊匹配了。

4、小结

要实现类似mysql的like查询,可以使用match_phrase_prefix实现。
match_phrase相较match_phrase_prefix更严格,可能出现有些我们人眼看觉得应该匹配的匹配不到。