elasticsearch 模糊查询不分词,实现 mysql like
一、分词我们可以先来看下啥叫分词,在es中,分词会对关键词进行一个或者几个分隔,比如输入了 隔壁小白,则会查询到 隔壁、小、白、隔、小壁等关键词有关的文档数据。而使用过mysql的模糊查询的童鞋们,应该都知道,输入了 隔壁小白,则只会出现 隔壁小白123、321淘宝了压抑122、22445淘宝了压抑等相关的数据。它不会对其中的关键词进行分割二、不分词使用es对于不分词,其实主要是 match_ph
·
一、分词
我们可以先来看下啥叫分词,在es中,分词会对关键词进行一个或者几个分隔,比如输入了 隔壁小白,则会查询到 隔壁、小、白、隔、小壁等关键词有关的文档数据。
而使用过mysql的模糊查询的童鞋们,应该都知道,输入了 隔壁小白,则只会出现 隔壁小白123、321淘宝了压抑122、22445淘宝了压抑等相关的数据。它不会对其中的关键词进行分割
二、不分词使用
es对于不分词,其实主要是 match_phrase的使用。我们平时比较多是使用match,这个是会进行分词的
三、先看下使用match 查询结果
[6] => Array
(
[_index] => testindex
[_type] => testtype
[_id] => P3QqTHcBSzBd54DbdZMl
[_score] => 3.9861763
[_source] => Array
(
[product_name] => 1016隔壁小白287
[rand] => 5082
)
)
[7] => Array
(
[_index] => testindex
[_type] => testtype
[_id] => Q3QqTHcBSzBd54DbepOG
[_score] => 3.9861763
[_source] => Array
(
[product_name] => 504隔壁小白1030
[rand] => 5275
)
)
[8] => Array
(
[_index] => testindex
[_type] => testtype
[_id] => s3QrTHcBSzBd54DbDZPS
[_score] => 3.9861763
[_source] => Array
(
[product_name] => 801隔壁小白15
[rand] => 4342
)
)
[9] => Array
(
[_index] => testindex
[_type] => testtype
[_id] => enQqTHcBSzBd54Dbs5PF
[_score] => 3.807864
[_source] => Array
(
[product_name] => 162隔1壁2小3白4157
[rand] => 5552
)
)
由此可见搜索 “隔壁小白” 结果出现 “162隔1壁2小3白4157” “504隔壁小白1030”
四、使用 match_phrase 进行检索结果如下
(
[0] => Array
(
[_index] => testindex
[_type] => testtype
[_id] => PnQqTHcBSzBd54Dbc5PD
[_score] => 7.4503546
[_source] => Array
(
[product_name] => 579隔壁小白891
[rand] => 6165
)
)
[1] => Array
(
[_index] => testindex
[_type] => testtype
[_id] => QnQqTHcBSzBd54DbeZOu
[_score] => 7.4503546
[_source] => Array
(
[product_name] => 928隔壁小白637
[rand] => 9975
)
)
[2] => Array
(
[_index] => testindex
[_type] => testtype
[_id] => PHQqTHcBSzBd54DbbZNd
[_score] => 4.6551437
[_source] => Array
(
[product_name] => 77隔壁小白1106
[rand] => 2949
)
)
)
更多推荐
已为社区贡献2条内容
所有评论(0)