http://blog.csdn.net/yangwenbo214/article/details/54142786

一、基本情况

前言:term query和match query牵扯的东西比较多,例如分词器、mapping、倒排索引等。我结合官方文档中的一个实例,谈谈自己对此处的理解

string类型在es5.*分为text和keyword。text是要被分词的,整个字符串根据一定规则分解成一个个小写的term,keyword类似es2.3中not_analyzed的情况。
string数据put到elasticsearch中,默认是text。 NOTE:默认分词器为standard analyzer。”Quick Brown Fox!”会被分解成[quick,brown,fox]写入倒排索引 term query会去倒排索引中寻找确切的term,它并不知道分词器的存在。这种查询适合keyword 、numeric、date
match query知道分词器的存在。并且理解是如何被分词的
总的来说有如下:
- term query 查询的是倒排索引中确切的term
- match query 会对filed进行分词操作,然后在查询 二、测试(1) 准备数据:
POST /termtest/termtype/1
{
"content":"Name"
} POST /termtest/termtype/2
{
"content":"name city"
} 查看数据是否导入
GET /termtest/_search
{
"query":
{
"match_all": {}
}
} 结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "termtest",
"_type": "termtype",
"_id": "",
"_score": 1,
"_source": {
"content": "name city"
}
},
{
"_index": "termtest",
"_type": "termtype",
"_id": "",
"_score": 1,
"_source": {
"content": "Name"
}
}
]
}
} 如上说明,数据已经被导入。该处字符串类型是text,也就是默认被分词了 做如下查询:
POST /termtest/_search
{
"query":{
"term":{
"content":"Name"
}
}
} 结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
} 分析结果:因为是默认被standard analyzer分词器分词,大写字母全部转为了小写字母,并存入了倒排索引以供搜索。term是确切查询,
必须要匹配到大写的Name。所以返回结果为空 POST /termtest/_search
{
"query":{
"match":{
"content":"Name"
}
}
} 结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "termtest",
"_type": "termtype",
"_id": "",
"_score": 0.2876821,
"_source": {
"content": "Name"
}
},
{
"_index": "termtest",
"_type": "termtype",
"_id": "",
"_score": 0.25811607,
"_source": {
"content": "name city"
}
}
]
}
} 分析结果: 原因(1):默认被standard analyzer分词器分词,大写字母全部转为了小写字母,并存入了倒排索引以供搜索,
原因(2):match query先对filed进行分词,分词为”name”,再去匹配倒排索引中的term 三、测试(2) 下面是官网实例官网实例
1. 导入数据 PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"full_text": {
"type": "text"
},
"exact_value": {
"type": "keyword"
}
}
}
}
} PUT my_index/my_type/1
{
"full_text": "Quick Foxes!",
"exact_value": "Quick Foxes!"
} 先指定类型,再导入数据 full_text: 指定类型为text,是会被分词
exact_value: 指定类型为keyword,不会被分词
full_text: 会被standard analyzer分词为如下terms [quick,foxes],存入倒排索引
exact_value: 只有[Quick Foxes!]这一个term会被存入倒排索引 做如下查询
GET my_index/my_type/_search
{
"query": {
"term": {
"exact_value": "Quick Foxes!"
}
}
} 结果: {
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "",
"_score": 0.2876821,
"_source": {
"full_text": "Quick Foxes!",
"exact_value": "Quick Foxes!"
}
}
]
}
} exact_value包含了确切的Quick Foxes!,因此被查询到 GET my_index/my_type/_search
{
"query": {
"term": {
"full_text": "Quick Foxes!"
}
}
}
结果: {
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
} full_text被分词了,倒排索引中只有quick和foxes。没有Quick Foxes! GET my_index/my_type/_search
{
"query": {
"term": {
"full_text": "foxes"
}
}
} 结果: {
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.25811607,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "",
"_score": 0.25811607,
"_source": {
"full_text": "Quick Foxes!",
"exact_value": "Quick Foxes!"
}
}
]
}
} full_text被分词,倒排索引中只有quick和foxes,因此查询foxes能成功 GET my_index/my_type/_search
{
"query": {
"match": {
"full_text": "Quick Foxes!"
}
}
} 结果: {
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.51623213,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "",
"_score": 0.51623213,
"_source": {
"full_text": "Quick Foxes!",
"exact_value": "Quick Foxes!"
}
}
]
}
} match query会先对自己的query string进行分词。也就是”Quick Foxes!”先分词为quick和foxes。然后在去倒排索引中查询,此处full_text是text类型,被分词为quick和foxes
因此能匹配上。
参考文献:http://blog.csdn.net/yangwenbo214/article/details/54142786

Elasticsearch 5.x 关于term query和match query的认识的更多相关文章

  1. Elasticsearch 5.0 中term 查询和match 查询的认识

    Elasticsearch 5.0 关于term query和match query的认识 一.基本情况 前言:term query和match query牵扯的东西比较多,例如分词器.mapping ...

  2. Elasticsearch Query DSL 整理总结(二)—— 要搞懂 Match Query,看这篇就够了

    目录 引言 构建示例 match operator 参数 analyzer lenient 参数 Fuzziness fuzzniess 参数 什么是模糊搜索? Levenshtein Edit Di ...

  3. Elasticsearch Query DSL 整理总结(四)—— Multi Match Query

    目录 引言 概要 fields 字段 通配符 提升字段权重 multi_match查询的类型 best_fields 类型 dis_max 分离最大化查询 best_fields 维权使者 tie_b ...

  4. Elasticsearch Query DSL 整理总结(三)—— Match Phrase Query 和 Match Phrase Prefix Query

    目录 引言 Match Phase Query slop 参数 analyzer 参数 zero terms query Match Phrase 前缀查询 max_expansions 小结 参考文 ...

  5. Elasticsearch.Net 异常:[match] query doesn't support multiple fields, found [field] and [query]

    用Elasticsearch.Net检索数据,报异常: )); ElasticLowLevelClient client = new ElasticLowLevelClient(settings); ...

  6. elasticsearch 中的Multi Match Query

    在Elasticsearch全文检索中,我们用的比较多的就是Multi Match Query,其支持对多个字段进行匹配.Elasticsearch支持5种类型的Multi Match,我们一起来深入 ...

  7. [Elasticsearch] 全文搜索 (一) 基础概念和match查询

    全文搜索(Full Text Search) 现在我们已经讨论了搜索结构化数据的一些简单用例,是时候开始探索全文搜索了 - 如何在全文字段中搜索来找到最相关的文档. 对于全文搜索而言,最重要的两个方面 ...

  8. Hibernate : Query.list()、Query.iterator()的区别

    Query上有list()与iterator()方法,两者的差别在于list()方法在读取数据时,并不会利用到快取,而是直接再向数据库查询,而iterator()则将读取到的数据写到快取,并于读取时再 ...

  9. query.setFirstResult(0),query.setMaxResults(4)

    query.setFirstResult(0),query.setMaxResults(1);相当于MySQL中的limit 0, 1; String hql = "FROM Forum f ...

随机推荐

  1. HBase Rowkey的散列与预分区设计

    转自:http://www.cnblogs.com/bdifn/p/3801737.html 问题导读:1.如何防止热点?2.如何预分区?扩展:为什么会产生热点存储? HBase中,表会被划分为1.. ...

  2. Selenium常用操作汇总二——如何把一个元素拖放到另一个元素里面(转)

    Q群里有时候会有人问,selenium  webdriver怎么实现把一个元素拖放到另一个元素里面.这一节总一下元素的拖放. 下面这个页面是一个演示拖放元素的页面,你可以把左右页面中的条目拖放到右边的 ...

  3. shell编程小结

    因为项目中要用到shell脚本,所以系统的看了一下.以前只是泛泛的了解. 变量:环境变量.预定义变量.位置变量.自定义变量. 环境变量这个好说,通过set或者env命令都能看到相应的列表,然后可以通过 ...

  4. Wings 3D

    Wings 3D 编辑 Wings 3D 是一个开源的三维计算机图形软件.使用翼边数据库.注重于多边形建模,构思取与 Izware 的 Nendo 和 Mirai.支持多种操作系统,包括 Linux. ...

  5. [原创]Allegro 导入DXF文件,保留布好的线路信息

    最近智能钥匙产品开发过程中,由于结构装配尺寸的偏差,需要对电路PCB外框OUTLINE进行缩小调整,并且USB插座定位孔改变. Allegro软件在线性绘制方面是有严重缺陷的,想绘制一个异形的板框比较 ...

  6. css 阻止元素中的文本。双击选中

    //firefox -moz-user-select: none; //chrome.safari -webkit-user-select: none; //ie -ms-user-select: n ...

  7. (个人)Linux基本指令收集

    1. 删除文件 其中 -r为向下递归删除    -f为强行删除,不做提示 rm -rf name 1   1 rm -rf name 2. 目录跳转指令 cd .. --跳转到上一级 cd ../ - ...

  8. Redis 缓存 + Spring 的集成示例(转载)

    1. 依赖包安装 pom.xml 加入: <dependency> <groupId>org.springframework.data</groupId> < ...

  9. Android带进度条的文件上传,使用AsyncTask异步任务

    最近项目中要做一个带进度条的上传文件的功能,学习了AsyncTask,使用起来比较方便,将几个方法实现就行,另外做了一个很简单的demo,希望能对大家有帮助,在程序中设好文件路径和服务器IP即可. A ...

  10. 用Fiddler可以设置浏览器的UA 和 手动 --Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!

    附加以一种软件的方法是:用Fiddler可以设置浏览器的UA 以下3种方法是手动的 通过伪装User-Agent,将浏览器模拟成Android设备. 第一种方法:新建Chrome快捷方式 右击桌面上的 ...