我们以一个查询的示例开始,我们在student这个type中存储了一些学生的基本信息,我们分别使用match和match_phrase进行查询。

首先,使用match进行检索,关键字是“He is”:

GET /test/student/_search
{
"query": {
"match": {
"description": "He is"
}
}
}

执行这条查询,得到的结果如下:

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.2169777,
"hits": [
{
"_index": "test",
"_type": "student",
"_id": "",
"_score": 0.2169777,
"_source": {
"name": "februus",
"sex": "male",
"age": ,
"description": "He is passionate.",
"interests": "reading, programing"
}
},
{
"_index": "test",
"_type": "student",
"_id": "",
"_score": 0.16273327,
"_source": {
"name": "leotse",
"sex": "male",
"age": ,
"description": "He is a big data engineer.",
"interests": "reading, swiming, hiking"
}
},
{
"_index": "test",
"_type": "student",
"_id": "",
"_score": 0.01989093,
"_source": {
"name": "pascal",
"sex": "male",
"age": ,
"description": "He works very hard because he wanna go to Canada.",
"interests": "programing, reading"
}
},
{
"_index": "test",
"_type": "student",
"_id": "",
"_score": 0.016878016,
"_source": {
"name": "yolovon",
"sex": "female",
"age": ,
"description": "She is so charming and beautiful.",
"interests": "reading, shopping"
}
}
]
}
}

而当你执行match_phrase时:

GET /test/student/_search
{
"query": {
"match_phrase": {
"description": "He is"
}
}
}

结果如下:

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.30685282,
"hits": [
{
"_index": "test",
"_type": "student",
"_id": "",
"_score": 0.30685282,
"_source": {
"name": "februus",
"sex": "male",
"age": ,
"description": "He is passionate.",
"interests": "reading, programing"
}
},
{
"_index": "test",
"_type": "student",
"_id": "",
"_score": 0.23013961,
"_source": {
"name": "leotse",
"sex": "male",
"age": ,
"description": "He is a big data engineer.",
"interests": "reading, swiming, hiking"
}
}
]
}
}

占的篇幅有点长,但是如果能基于此看清这两者之间的区别,那也是值得的。

我们分析一下这两者结果的差别:

1.非常直观的一点,对于同一个数据集,两者检索出来的结果集数量不一样;
2.对于match的结果,我们可以可以看到,结果的Document中description这个field可以包含“He is”,“He”或者“is”;
3.match_phrase的结果中的description字段,必须包含“He is”这一个词组;
4.所有的检索结果都有一个_score字段,看起来是当前这个document在当前搜索条件下的评分,而检索结果也是按照这个得分从高到低进行排序。
       我们要想弄清楚match和match_phrase的区别,要先回到他们的用途:match是全文搜索,也就是说这里的搜索条件是针对这个字段的全文,只要发现和搜索条件相关的Document,都会出现在最终的结果集中,事实上,ES会根据结果相关性评分来对结果集进行排序,这个相关性评分也就是我们看到的_score字段;总体上看,description中出现了“He is”的Document的相关性评分高于只出现“He”或“is”的Document。(至于怎么给每一个Document评分,我们会在以后介绍)。
相关性(relevance)的概念在Elasticsearch中非常重要,而这个概念在传统关系型数据库中是不可想象的,因为传统数据库对记录的查询只有匹配或者不匹配。

那么,如果我们不想将我们的查询条件拆分,应该怎么办呢?这时候我们就可以使用match_phrase:
match_phrase是短语搜索,亦即它会将给定的短语(phrase)当成一个完整的查询条件。当使用match_phrase进行搜索的时候,你的结果集中,所有的Document都必须包含你指定的查询词组,在这里是“He is”。这看起来有点像关系型数据库的like查询操作。

ES查询-match VS match_phrase的更多相关文章

  1. ES查询-term VS match (转)

    原文地址:https://blog.csdn.net/sxf_123456/article/details/78845437 elasticsearch 中term与match区别 term是精确查询 ...

  2. ES查询之刨根问底

    昨天有一个需求,就是想要根据某个网关url做过滤,获取其下面所有的上下文nginx日志:如果直接"query":"https://XXX/YYY/ZZZ"发现有 ...

  3. Es查询工具使用

    Kibana按照索引过滤数据 1.创建索引模式 2.查询索引中的数据 Es查询不返回数据 创建索引的时候指定mapping mappings={ "mappings": { &qu ...

  4. ElasticSearch 学习记录之ES查询添加排序字段和使用missing或existing字段查询

    ES添加排序 在默认的情况下,ES 是根据文档的得分score来进行文档额排序的.但是自己可以根据自己的针对一些字段进行排序.就像下面的查询脚本一样.下面的这个查询是根据productid这个值进行排 ...

  5. .NetCore下ES查询驱动 PlainElastic .Net 升级官方驱动 Elasticsearch .Net

    1.背景 由于历史原因,笔者所在的公司原有的ES查询驱动采用的是 PlainElastic.Net, 经过询问原来是之前PlainElastic.Net在园子里文档较多,上手比较容易,所以最初作者选用 ...

  6. ES查询语句

    记录常用的es 查询 聚合 GET _cat / indices GET / p_ext_develop / _mapping / g GET / p_ext_develop / _analyze { ...

  7. es 查询更新操作

    # es 查询更新操作# _*_ coding: utf-8 _*_ import time import datetime import pymysql from elasticsearch imp ...

  8. es查询--请求body

    查询的JSON结构 普通查询 { "query": { # 查询条件 "match_all": {} //匹配所有文档, 所有 _score 为1.0 # &q ...

  9. Es查询结果集默认是10000,更新设置

    Es查询结果集默认是10000,结果集大小是int,最大为21亿左右 PUT _all/_settings?preserve_existing=true { "index.max_resul ...

随机推荐

  1. Oracle 生成Guid;Oracle 生成多个Guid;Oracle 生成带''-"的Guid

    Oracle 生成Guid select sys_guid() from dual Oracle 生成多个Guid Oracle 生成带''-"的Guid , ) , ) || '-' || ...

  2. jquery 防止当前页面被Iframe嵌套,防止登录页面Iframe被嵌套

    <script type="text/javascript"> if (top.location != location) { top.location.href = ...

  3. 7.3.5 Tomcat堆溢出分析(1)

    实战Java虚拟机:JVM故障诊断与性能优化>第7章分析Java堆,本章主要介绍了Java堆的分析方法.首先,介绍了几种常见的Java内存溢出现象及解决思路.其次,探讨了java.lang.St ...

  4. sql 查询语句的练习

    --1.使用基本查询语句. --(1)查询DEPT表显示所有部门名称. select * from dept; --(2)查询EMP表显示所有雇员名及其全年收入(月收入=工资+补助),处理NULL行, ...

  5. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  6. mongodb插入数据获取本次插入的mongodb id

    最近接了一个别人的项目做二次开发,使用php进行mongodb的数据操作时,需要插入数据后得到相应的mongodb 中的id,简单代码如下 $data = array('test' => 'aa ...

  7. VirtualBox安装Ubuntu16.04过程

    1. 软件版本 Windows: Win7/Win10 VirtualBox: VirtualBox-6.0.24-108355-Win Ubuntu: ubuntu-16.04-desktop-am ...

  8. [Docker] 容器开发环境最佳实践理论

      保持 image 小       选择合适的 base image.       使用 multi-stage 构建. https://docs.docker.com/develop/develo ...

  9. whereis命令详解

    1.简介: whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis ...

  10. python 图片识别灰度

    # -*- coding: cp936 -*- from skimage import io,transform,color import numpy as np def convert_gray(f ...