关于Elasticsearch 使用 MatchPhrase搜索的一些坑
- 对分词字段检索使用的通常是match查询,对于短语查询使用的是matchphrase查询,但是并不是matchphrase可以直接对分词字段进行不分词检索(也就是业务经常说的精确匹配),下面有个例子,使用Es的请注意。
- 某个Index下面存有如下内容
{
"id": "1",
"fulltext": "亚马逊卓越有限公司诉讼某某公司"
}
其中fulltext使用ik分词器进行分词存储,使用ik分词结果如下
"tokens": [
{
"token": "亚马逊",
"start_offset": 0,
"end_offset": 3,
"type": "CN_WORD",
"position": 0
},
{
"token": "亚",
"start_offset": 0,
"end_offset": 1,
"type": "CN_WORD",
"position": 1
},
{
"token": "马",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 2
},
{
"token": "逊",
"start_offset": 2,
"end_offset": 3,
"type": "CN_WORD",
"position": 3
},
{
"token": "卓越",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 4
},
{
"token": "卓",
"start_offset": 3,
"end_offset": 4,
"type": "CN_WORD",
"position": 5
},
{
"token": "越有",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 6
},
{
"token": "有限公司",
"start_offset": 5,
"end_offset": 9,
"type": "CN_WORD",
"position": 7
},
{
"token": "有限",
"start_offset": 5,
"end_offset": 7,
"type": "CN_WORD",
"position": 8
},
{
"token": "公司",
"start_offset": 7,
"end_offset": 9,
"type": "CN_WORD",
"position": 9
},
{
"token": "诉讼",
"start_offset": 9,
"end_offset": 11,
"type": "CN_WORD",
"position": 10
},
{
"token": "讼",
"start_offset": 10,
"end_offset": 11,
"type": "CN_WORD",
"position": 11
},
{
"token": "某某",
"start_offset": 11,
"end_offset": 13,
"type": "CN_WORD",
"position": 12
},
{
"token": "某公司",
"start_offset": 12,
"end_offset": 15,
"type": "CN_WORD",
"position": 13
},
{
"token": "公司",
"start_offset": 13,
"end_offset": 15,
"type": "CN_WORD",
"position": 14
}
]
对于如上结果,如果进行matchphrase查询 “亚马逊卓越”,无法匹配出任何结果
因为对 “亚马逊卓越” 进行分词后的结果为:
{
"tokens": [
{
"token": "亚马逊",
"start_offset": 0,
"end_offset": 3,
"type": "CN_WORD",
"position": 0
},
{
"token": "亚",
"start_offset": 0,
"end_offset": 1,
"type": "CN_WORD",
"position": 1
},
{
"token": "马",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 2
},
{
"token": "逊",
"start_offset": 2,
"end_offset": 3,
"type": "CN_WORD",
"position": 3
},
{
"token": "卓越",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 4
},
{
"token": "卓",
"start_offset": 3,
"end_offset": 4,
"type": "CN_WORD",
"position": 5
},
{
"token": "越",
"start_offset": 4,
"end_offset": 5,
"type": "CN_CHAR",
"position": 6
}
]
}
和存储的内容对比发现 原文存储中包含词语 “越有”,而查询语句中并不包含“越有”,包含的是“越”,因此使用matchphrase短语匹配失败,也就导致了无法检索出内容。
还是这个例子,换个词语进行检索,使用“亚马逊卓越有”,会发现竟然检索出来了,对“亚马逊卓越有”进行分词得到如下结果:
{
"tokens": [
{
"token": "亚马逊",
"start_offset": 0,
"end_offset": 3,
"type": "CN_WORD",
"position": 0
},
{
"token": "亚",
"start_offset": 0,
"end_offset": 1,
"type": "CN_WORD",
"position": 1
},
{
"token": "马",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 2
},
{
"token": "逊",
"start_offset": 2,
"end_offset": 3,
"type": "CN_WORD",
"position": 3
},
{
"token": "卓越",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 4
},
{
"token": "卓",
"start_offset": 3,
"end_offset": 4,
"type": "CN_WORD",
"position": 5
},
{
"token": "越有",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 6
}
]
}
注意到了吗?这里出现了越有这个词,这也就是说现在的分词结果和之前的全文分词结果完全一致了,所以matchphrash也就找到了结果。
再换一个极端点的例子,使用“越有限公司”去进行检索,你会惊讶的发现,竟然还能检索出来,对“越有限公司”进行分词,结果如下:
{
"tokens": [
{
"token": "越有",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
},
{
"token": "有限公司",
"start_offset": 1,
"end_offset": 5,
"type": "CN_WORD",
"position": 1
},
{
"token": "有限",
"start_offset": 1,
"end_offset": 3,
"type": "CN_WORD",
"position": 2
},
{
"token": "公司",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 3
}
]
}
这个结果和原文中的结果又是完全一致(从越有之后的内容一致),所以匹配出来了结果,注意点这里有个词语“有限公司”,检索词语如果我换成了“越有限”,就会发现没有查询到内容,因为“越有限”分词结果为:
{
"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
}
]
}
“越有”这个词是包含的,”有限”这个词语也是包含的,但是中间隔了一个“有限公司”,所以没有完全一致,也就匹配不到结果了。这时候如果我检索条件设置matchphrase的slop=1,使用“越有限”就能匹配到结果了,现在可以明白了,其实position的位置差就是slop的值,而matchphrase并不是所谓的词语拼接进行匹配,还是需要进行分词,以及position匹配的。
关于Elasticsearch 使用 MatchPhrase搜索的一些坑的更多相关文章
- elasticsearch的rest搜索--- 查询
目录: 一.针对这次装B 的解释 二.下载,安装插件elasticsearch-1.7.0 三.索引的mapping 四. 查询 五.对于相关度的大牛的文档 四. 查询 1. 查询的官网的文档 ...
- elasticsearch实现网站搜索
使用elasticsearch 实现网站搜索,可以支持商品搜索,筛选项过滤搜索 ,价格排序, 打分 筛选项聚合,还有其他综合排序 后续推出搜索人工干预排序,根据销量,好评率,售卖率 进行全方位的搜索实 ...
- Python 和 Elasticsearch 构建简易搜索
Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...
- CentOS 7.4 下搭建 Elasticsearch 6.3 搜索群集
上个月 13 号,Elasticsearch 6.3 如约而至,该版本和以往版本相比,新增了很多新功能,其中最令人瞩目的莫过于集成了 X-Pack 模块.而在最新的 X-Pack 中 Elastics ...
- 笔记13:Python 和 Elasticsearch 构建简易搜索
Python 和 Elasticsearch 构建简易搜索 1 ES基本介绍 概念介绍 Elasticsearch是一个基于Lucene库的搜索引擎.它提供了一个分布式.支持多租户的全文搜索引擎,它可 ...
- elasticsearch联想加搜索实例
//搜索框具体的ajax如下: <form class="form-wrapper cf"> <img src="__PUBLIC__/Home/img ...
- elasticsearch 单节点搭建与爬坑记录
elasticsearch 单节点搭建与爬坑记录 prepare 虚拟机或者云服务器(这里用的是阿里云ECS) linux---centos7 安装完毕的jdk 相应的安装包(在https:/ ...
- 畅购商城(五):Elasticsearch实现商品搜索
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 畅购商城(一):环境搭建 畅购商 ...
- Elasticsearch(2) 数据搜索
本文介绍如何在Elasticsearch中对数据进行搜索. 1.简述 在Elasticsearch中的搜索中,有两类搜索: queries aggregations 区别在于:query可以进行全文搜 ...
随机推荐
- 【翻译】在Ext JS应用程序中构建可维护的控制器
原文:Building Maintainable Controllers in Ext JS Apps 你好You Had Me 你是Tearing Me Apart 模板We Dont Need t ...
- 关于Eclipse创建Android项目时,会多出一个appcompat_v7的问题
问题描述: 使用eclipse创建一个Android项目时,发现project列表中会多创建出一个appcompat_v7项目,再创建一个Android项目时,又会再多出一个appcompat_ ...
- 【一天一道LeetCode】#65. Valid Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- Java实现栈之计算器
Java实现栈来做一个将中缀表达式转化为后缀表达式的程序,中缀表达式更符合我们的主观感受,后缀表达式更适合计算机的运算,下面直接上代码吧: package Character1; import jav ...
- Property属性, KVC键值编码OC…
1.属性:帮你自动生成setter 和 getter 方法 属性的声明:(写在.h中) 格式: @property 数据类型 属性名 属性的实现:(写在.m中) ...
- java实现http的post和get
前话说一句:conn.setDefaultRequestProperty(key, value);这个函数是设置属性的,其实可以没有! 自己写了一个简单的get,容易控制 public stati ...
- (NO.00001)iOS游戏SpeedBoy Lite成形记(二十七)
切换回Xcode,在GameScene.m中添加新的实例变量:_winLayer. 接下来在第一个选手到达终点时,我们可以完成选手胜利的动画特效了. 首先,在GameScene.m中添加一个新方法pl ...
- 14_Android中Service的使用,关于广播接收者的说明
服务:长期后台运行的没有界面的组件 android应用:什么地方需要用到服务? 天气预报:后台的连接服务器的逻辑,每隔一段时间获取最新的天气信息 股票显示:后台的连接服务器的逻辑,每隔一段时间获 ...
- Linux管理日记(一)
作者: 铁锚 日期: 2013年12月23日 1. 安装 webmin # 安装 webmin cd /usr/local/ieternal/ mkdir download cd download w ...
- 【翻译】Ext JS 5的委托事件和手势
原文:Delegated Events and Gestures in Ext JS 5 简介 Ext JS在5之前的版本,被设计为专用于传统鼠标输入的桌面设备使用.而从5开始,添加了对触屏输入的支持 ...