• 对分词字段检索使用的通常是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搜索的一些坑的更多相关文章

  1. elasticsearch的rest搜索--- 查询

    目录: 一.针对这次装B 的解释 二.下载,安装插件elasticsearch-1.7.0   三.索引的mapping 四. 查询 五.对于相关度的大牛的文档 四. 查询 1. 查询的官网的文档   ...

  2. elasticsearch实现网站搜索

    使用elasticsearch 实现网站搜索,可以支持商品搜索,筛选项过滤搜索 ,价格排序, 打分 筛选项聚合,还有其他综合排序 后续推出搜索人工干预排序,根据销量,好评率,售卖率 进行全方位的搜索实 ...

  3. Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...

  4. CentOS 7.4 下搭建 Elasticsearch 6.3 搜索群集

    上个月 13 号,Elasticsearch 6.3 如约而至,该版本和以往版本相比,新增了很多新功能,其中最令人瞩目的莫过于集成了 X-Pack 模块.而在最新的 X-Pack 中 Elastics ...

  5. 笔记13:Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 1 ES基本介绍 概念介绍 Elasticsearch是一个基于Lucene库的搜索引擎.它提供了一个分布式.支持多租户的全文搜索引擎,它可 ...

  6. elasticsearch联想加搜索实例

    //搜索框具体的ajax如下: <form class="form-wrapper cf"> <img src="__PUBLIC__/Home/img ...

  7. elasticsearch 单节点搭建与爬坑记录

    elasticsearch 单节点搭建与爬坑记录   prepare   虚拟机或者云服务器(这里用的是阿里云ECS) linux---centos7 安装完毕的jdk 相应的安装包(在https:/ ...

  8. 畅购商城(五):Elasticsearch实现商品搜索

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 畅购商城(一):环境搭建 畅购商 ...

  9. Elasticsearch(2) 数据搜索

    本文介绍如何在Elasticsearch中对数据进行搜索. 1.简述 在Elasticsearch中的搜索中,有两类搜索: queries aggregations 区别在于:query可以进行全文搜 ...

随机推荐

  1. int*p[ ]与int(*p)[ ]的不同

    举例说明: 1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型. 可以这样来用: #include <iostream> using ...

  2. Android反编译获取资源文件-android学习之旅(69)

    有时候你看到一些很好看的布局,会考虑别人怎么实现的,回想参考一下,那么这时候反编译一下是很必要的. 要用到的工具apktool.bat和aapt.exe和apktool.jar(要最新版本) 下载前两 ...

  3. struts2 令牌 实现源代码 JSP

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  4. (NO.00001)iOS游戏SpeedBoy Lite成形记(二十八):增加排行榜功能

    游戏大体上基本也就完成了,还差一个排行榜.否则如何激励各位选手创造新纪录呢? 排行榜功能也没什么难的,不过需要一点点排序的算法上的考虑. 这里我们把排行榜记录数据和排序都放在GameState类中,在 ...

  5. 学习OpenCV,GPU模块

    如何使用opencv的gpu库呢?我这两天一直在搞这个事情,环境的配置见上文(转载),这里我先举个简单的例子,实现这样的功能:host读入一幅图像,加载到GPU上,在GPU上复制一份然后下传到host ...

  6. redis学习资料

    http://www.it168.com/redian/redis/ http://down.51cto.com/data/836008 http://www.redis.cn/

  7. Leetcode_128_Longest Consecutive Sequence

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43854597 Given an unsorted arra ...

  8. Linux引导流程(第二版)

    Linux引导流程 柱面,0磁头,1扇区 如图:] 进入这一步骤的目的是[通常,PID是随机分配的,但是init特殊,可以通过命令:ps -el | more 查看],Linux系统中init名义上可 ...

  9. eclipse中Debug简单记忆

    最左边:代码一步一步的走,进入函数也是一步一步的走: 最中间:在断点开始一步一步的走,遇到函数不会进入函数,而是直接跳过函数(但是把函数中的代码整体走完的): 最右边:断点开始一部迅速返回上一级函数调 ...

  10. LeetCode之“字符串”:Valid Palindrome

    题目链接 题目要求: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...