ES match match_phrase term willcard的查询原理
比如:要求实现SQL中like “%xxxx%”的匹配效果。
wildcard通配
这种效果在ES中最匹配的做法是用wildcard query通配,这种情况不会对query分词,而是直接遍历倒排索引逐个匹配计算,性能是无法想象的,大家慎用。
match全文匹配
效果最差的做法是用match全文检索,这种情况只要query分词的任何一个term出现在倒排中,就会召回文档,所以很容易搜出一些八竿子打不着的文档。
term匹配
如果你的搜索词本身不需要分词,只是一个term的话,那么直接走term query是最方便的。
match_phrase短语匹配
推荐一个折衷性能与准确度的做法就是用match_phrase短语匹配。
match_phrase的原理是对query分词,要求所有的term都出现在倒排中,并且连续且顺序一致的排列,下面一起看个例子。
我们采用ik_smart中文分词器,对”青岛上合蓝”分词:
|
1
2
3
4
5
6
7
|
[
'index' => 'article',
'body' => [
'analyzer' => 'ik_smart',
'text' => '青岛上合蓝',
]
]
|
得到结果:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
{
"tokens": [{
"token": "青岛",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
}, {
"token": "上合",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 1
}, {
"token": "蓝",
"start_offset": 4,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
}]
}
|
大家看到,每个term都有一个position字段标识了term的位置,这将直接影响match_phrase是否可以召回。
接着我们进行搜索,query搜索词是:”上合蓝”,分词结果如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
{
"tokens": [{
"token": "上合",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
}, {
"token": "蓝",
"start_offset": 2,
"end_offset": 3,
"type": "CN_WORD",
"position": 1
}]
}
|
“上合”与”蓝”的position紧密排列,与之前”青岛上合蓝”中的”上合”与”蓝”顺序一致且连续,所以match_phrase搜索”上合蓝”可以召回上述的”青岛上合蓝”。
相反,如果你query搜索”青岛蓝”,那么”青岛”与”蓝”中间少了一个”上合”,所以无法召回:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
{
"tokens": [{
"token": "青岛",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
}, {
"token": "蓝",
"start_offset": 2,
"end_offset": 3,
"type": "CN_WORD",
"position": 1
}]
}
|
所以,match_phrase的确可以解决我们的这个场景。
因为match_phrase需要分词,所以如果分词效果不好(词库不足),query就会产生不同于doc的term,如果term都不同就肯定无法匹配了。
但是大家要注意,match_phrase与ik_max_word分词器是无法一起工作的,因为ik_max_word分词的term具有重叠问题,下面举个栗子:
先用ik_max_word分词:
|
1
2
3
4
5
6
7
|
[
'index' => 'article',
'body' => [
'analyzer' => 'ik_max_word',
'text' => '青岛上合蓝',
]
]
|
得到:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
{
"tokens": [{
"token": "青岛",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
}, {
"token": "岛上",
"start_offset": 1,
"end_offset": 3,
"type": "CN_WORD",
"position": 1
}, {
"token": "岛",
"start_offset": 1,
"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": 5,
"type": "CN_WORD",
"position": 4
}]
}
|
你从”岛上”,”岛”就能看出,它的term之间具有重叠情况,这与ik_smart是完全不同的,因为ik_max_word的目标是尽可能产生更多的term组合,一般用于全文检索提高召回率。
接着我们搜索下面的query:
|
1
2
3
4
5
6
7
|
[
'index' => 'article',
'body' => [
'analyzer' => 'ik_max_word',
'text' => '青岛',
]
]
|
分词结果:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
{
"tokens": [{
"token": "青岛",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
}, {
"token": "岛",
"start_offset": 1,
"end_offset": 2,
"type": "CN_WORD",
"position": 1
}]
}
|
“青岛”与”岛”之间差着一个”岛上”,结果就是match_phrase不匹配。
最后给大家一个结论:
如果大家用match_phrase的话,需要注意2个方面:1)分词器不准会影响召回;2)只能用ik_smart。
其他对于ES 默认分词等其他分词同样适用
原文链接:https://yuerblog.cc/2018/09/13/ik-with-match_phrase
ES match match_phrase term willcard的查询原理的更多相关文章
- ElasticSearch match, match_phrase, term区别
1.term结构化字段查询,匹配一个值,且输入的值不会被分词器分词. 比如查询条件是: { "query":{ "term":{ "foo" ...
- ES 入门记录之 match和term查询的区别
ElasticSearch 系列文章 1 ES 入门之一 安装ElasticSearcha 2 ES 记录之如何创建一个索引映射 3 ElasticSearch 学习记录之Text keyword 两 ...
- es match、match_phrase、query_string和term的区别
(一)text字段和keyword字段的区别 以下给出一个例子: 首先建立一个索引和类型,引入一个keywork的字段: PUT my_index { "mappings": { ...
- elasticsearch 查询(match和term)
elasticsearch 查询(match和term) es中的查询请求有两种方式,一种是简易版的查询,另外一种是使用JSON完整的请求体,叫做结构化查询(DSL). 由于DSL查询更为直观也更为简 ...
- (转载)elasticsearch 查询(match和term)
原文地址:https://www.cnblogs.com/yjf512/p/4897294.html elasticsearch 查询(match和term) es中的查询请求有两种方式,一种是简易版 ...
- es创建普通索引以及各种查询
创建索引 创建普通索引: PUT /my_index { "settings": { "index": { "number_of_shards&quo ...
- 基于Lucene查询原理分析Elasticsearch的性能
前言 Elasticsearch是一个很火的分布式搜索系统,提供了非常强大而且易用的查询和分析能力,包括全文索引.模糊查询.多条件组合查询.地理位置查询等等,而且具有一定的分析聚合能力.因为其查询场景 ...
- ES 入门 - 基于词项的查询
准备 首先先声明下,我这里使用的 ES 版本 5.2.0. 为了便于理解,这里以如下 index 为格式,该格式是通过 PMACCT 抓取的 netflow 流量信息, 文中所涉及的到的例子,全基于此 ...
- Lucene 查询原理 传统二级索引方案 倒排链合并 倒排索引 跳表 位图
提问: 1.倒排索引与传统数据库的索引相比优势? 2.在lucene中如果想做范围查找,根据上面的FST模型可以看出来,需要遍历FST找到包含这个range的一个点然后进入对应的倒排链,然后进行求并集 ...
随机推荐
- mongodb url
https://blog.csdn.net/jianlong727/article/details/53484440
- 数字色彩的艺术 | The Art Of Digital Color(修订)
翻译一篇来自2011年的文章,原链地址:https://www.fxguide.com/featured/the-art-of-digital-color/ 在这个时期,DPX日渐式微,ACES方兴未 ...
- 转发 Delphi中线程类TThread 实现多线程编程
Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对TThread类的几个成员作一简单介绍,再说明一下Execute的实现和Synchr ...
- (转)SQLServer_十步优化SQL Server中的数据访问四
原文地址:http://tech.it168.com/a2009/1125/814/000000814758_all.shtml 第八步:使用SQL事件探查器和性能监控工具有效地诊断性能问题 在SQL ...
- 棒槌的工作第八天-----------------------早会-------三次握手四次挥手(转载https://www.cnblogs.com/zmlctt/p/3690998.html)尚待了解,下班补充
TCP三次握手 所谓三次握手(Three-way Handshake),是指建立一个TCP连接时,需要客户端和服务器总共发送3个包. 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步连接双方 ...
- 秘制牛肉Alpha阶段项目展示
秘制牛肉Alpha阶段项目展示 1.团队成员和个人博客 · 左顺:"我是左顺,秘制牛肉队开发人员". · 王尖兵:"C,java,html5都会一点的菜鸡,没做过团队项目 ...
- 整理Xen理论知识
XEN 简介 XEN 是一个基于X86架构.发展最快.性能最稳定.占用资源最少的开源虚拟化技术.Xen可以在一套物理硬件上安全的执行多个虚拟机,与 Linux 是一个完美的开源组合,Novell SU ...
- java中几种常用的设计模式
参考https://blog.csdn.net/jiyang_1/article/details/50110931 参考https://blog.csdn.net/dean_hu/article/de ...
- Python unittest discover()方法与执行顺序补充
一.discover更多测试用例 可以根据不同的功能创建不同的测试文件,甚至是不同的测试目录,测试文件中还可以将不同的小功能划分为不同的测试类,在类下编写测试用例,让整体结构更加清晰 但通过addTe ...
- RobotFramework - AppiumLibrary 之关键字Open Application使用