一.创建测试数据

1.创建一个index

curl -X PUT  http://127.0.0.1:9200/student?pretty -H "Content-Type: application/json" -d '{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"class": {
"type": "text",
"analyzer": "ik_max_word"
},
"introduce": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}'

2.验证是否创建成功

curl -XGET "http://127.0.0.1:9200/student?pretty"

3.插入测试数据

curl -X PUT http://127.0.0.1:9200/student/_doc/1?pretty -H "Content-Type: application/json" -d '{
"id":1,
"name":"关云长",
"age":30,
"class":"蜀国一班"
}' curl -X PUT http://127.0.0.1:9200/student/_doc/2?pretty -H "Content-Type: application/json" -d '{
"id":2,
"name":"吕蒙",
"age":25,
"class":"吴国一班"
}' curl -X PUT http://127.0.0.1:9200/student/_doc/3?pretty -H "Content-Type: application/json" -d '{
"id":3,
"name":"吕布",
"age":40,
"class":"三姓一班"
}' curl -X PUT http://127.0.0.1:9200/student/_doc/4?pretty -H "Content-Type: application/json" -d '{
"id":4,
"name":"张翼德",
"age":30,
"class":"蜀国二班"
}'

4.查询所有数据,验证是否正确

curl -XGET http://127.0.0.1:9200/student/_search?pretty -H "Content-Type: application/json" -d '
{
"query": {
"match_all": {}
}
}'

二.验证


#关于term和match,下面两个查询,term没有结果,match有结果,为什么?
curl -XGET http://127.0.0.1:9200/student/_search?pretty -H "Content-Type: application/json" -d '{
"query": {
"term": {"name":"吕蒙"}
}
}' curl -XGET http://127.0.0.1:9200/student/_search?pretty -H "Content-Type: application/json" -d '{
"query": {
"match": {"name":"吕蒙"}
}
}'

拿A去B里匹配,A能分词,B也能分词。term不会将A分词,match会将A分词,存储数据类型keyword不会将B分词,text会将B分词。

  可以看到上面用term方式查找,没有结果,而用match方式查找,能查找到“吕蒙”和“吕布”两个结果

  term是不分词(不拆分搜索字)查找目标字段中是否有要查找的文字,也就是完整查找“吕蒙”两个字,而name这个字段用的是text类型存储的,text类型数据默认是分词的,也就是elasticsearch会将name分词后(分成“吕”和“蒙”)再存储,这时候拿完整的搜索字“吕蒙”去存储的“吕”、“蒙”里找肯定是找不到的。

  match是分词(拆分搜索字)查找目标字段,也就是说会先将要查找的搜索子“吕蒙”拆成“吕”和“蒙”,再分别去name里找“吕”,如果没有找到“吕”,还会去找“蒙”,而存储的数据里,text已经将“吕蒙”和“吕布”都分词成了“吕”,“蒙”,“吕”,“布”存储了,所以光通过一个“吕”字就能找到两条结果。

  这里要区分搜索词的分词,以及字段存储的分词。拿A去B里匹配,A能分词,B也能分词。term不会将A分词,match会将A分词。

  既然name的类型,存储的时候就是分词的,那能不能在存储的时候不分词了,可以用将text类型改成keyword类型

#删除所有文档
curl -XPOST "http://127.0.0.1:9200/student/_delete_by_query?pretty" -v -H "Content-Type: application/json" -d '
{
"query": {
"match_all": {}
}
}' #删除索引
curl -XDELETE "http://127.0.0.1:9200/student?pretty" #重新创建索引,将name字段的类型改成keyword
curl -X PUT http://127.0.0.1:9200/student?pretty -H "Content-Type: application/json" -d '{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"class": {
"type": "text",
"analyzer": "ik_max_word"
},
"introduce": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}' #重新插入上面四条数据 #请复制上面的语句,执行 #下面这条查询将返回“吕蒙”同学
curl -XGET http://127.0.0.1:9200/student/_search?pretty -H "Content-Type: application/json" -d '{
"query": {
"term": {"name":"吕蒙"}
}
}' #下面这条查询将返回0结果,因为存储时类型为keyword没有分词,所以存储的是“吕蒙”和“吕布”,这时候拿#“吕”去匹配,没有匹配的结果
curl -XGET http://127.0.0.1:9200/student/_search?pretty -H "Content-Type: application/json" -d '{
"query": {
"term": {"name":"吕"}
}
}' #下面的结果将只会返回“吕蒙”同学,没有匹配的结果,因为存储时类型为keyword没有分词,所以存储的“吕
#蒙”和“吕布”,这时候拿“吕蒙”去匹配,虽然用的match,会将搜索词拆分成“吕蒙”,“吕”,“蒙”去搜索,但
#“吕”和“蒙”都不会匹配的到存储的“吕蒙”和“吕布”
curl -XGET http://127.0.0.1:9200/student/_search?pretty -H "Content-Type: application/json" -d '{
"query": {
"match": {"name":"吕蒙"}
}
}'

ElasticSearch中term和match探索的更多相关文章

  1. elasticsearch 中的Multi Match Query

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

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

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

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

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

  4. Elasticsearch学习系列之term和match查询

    lasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为book ...

  5. Elasticsearch学习系列之term和match查询实例

    Elasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为boo ...

  6. Elasticsearch中的Term查询和全文查询

    目录 前言 Term 查询 exists 查询 fuzzy 查询 ids 查询 prefix 查询 range 查询 regexp 查询 term 查询 terms 查询 terms_set 查询 t ...

  7. 在Elasticsearch中查询Term Vectors词条向量信息

    这篇文章有点深度,可能需要一些Lucene或者全文检索的背景.由于我也很久没有看过Lucene了,有些地方理解的不对还请多多指正. 更多内容还请参考整理的ELK教程 关于Term Vectors 额, ...

  8. 如何在Elasticsearch中安装中文分词器(IK+pinyin)

    如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题--中文词语被分成了一个一个的汉字,当用Kibana作图的时候,按照term来分组,结果一个汉字被分成了一组. ...

  9. elasticsearch中常用的API

    elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...

随机推荐

  1. Grafana +Zabbix 系列二

    Grafana +Zabbix 系列二 Grafana 简介补充 Grafana自身并不存储数据,数据从其他地方获取.需要配置数据源 Grafana支持从Zabbix中获取数据 Grafana优化图形 ...

  2. Appium获取toast消息

    Android获取toast,需要在参数里设置automationName:Uiautomator2 设置设备的信息 desired_caps = { 'platformName': 'Android ...

  3. expdp / impdp 用法详解 ,和exp / imp 的区别

    一  关于expdp和impdp     使用EXPDP和IMPDP时应该注意的事项:EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.EXPDP和IMPDP是服务端的工具程 ...

  4. kotlin泛型类型变异

    在java泛型中中会有 ? extends E 可以解决类似于List<String> 赋给List<Object>  的问题,但是在kotlin泛型中并没有提供通配符,而是o ...

  5. Onvif协议及其在Android下的实现

    好久没有写博客,今天将前段时间做的Onvif协议在Android上的实现分享给大家. 首先,我们先来了解一下什么是Onvif协议:ONVIF 协议是由Open Network Video Interf ...

  6. 21 Flutter仿京东商城项目 商品详情 请求接口渲染数据 商品属性数据渲染

    加群452892873 下载对应21可文件,运行方法,建好项目,直接替换lib目录,在往pubspec.yaml添加上一下扩展.   cupertino_icons: ^0.1.2   flutter ...

  7. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_15-异常处理-异常处理流程

    右侧是框架报的异常 不可预知的,例如数据库连不上这一类的.可以在map中制定某些类的异常,如果找不到就最右边的 99999的, 系统对异常的处理使用统一的异常处理流程: 1.自定义异常类型. 2.自定 ...

  8. 图解 HTTP 笔记(一)——了解 Web 及网络基础

    本章内容:Web 建立在何种技术之上,HTTP 协议如何诞生并发展? 一.Web 基于 HTTP 通信 Web 使用一种名为 HTTP (HyperText Transfer Protocol,超文本 ...

  9. sersync+rsync做实时同步

    (1).实验环境 源主机:youxi1 192.168.5.101 目的主机:youxi2 192.168.5.102 目的:实时同步数据 sersync默认端口874,rsync默认端口873 (2 ...

  10. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...