Elasticsearch学习第二篇--常用的几种搜索方式
Elasticsearch学习第二篇--常用的几种搜索方式
- 一、Query String Search
- 查询全部
- 条件查询
- 二、Query DSL
- 查询全部
- 条件查询
- 三、Query Filter
- 四、Full-text Search 全文检索
- 六、Phase Search 短语搜索
- 六、Highlight search 高亮搜索
一、Query String Search
类似HTTP的GET请求,参数拼接。
查询全部
get test_index/base/_search
{"took": 1,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "test_index","_type": "base","_id": "2","_score": 1,"_source": {"name": "xiaomei","age": 18,"sex": "F"}},{"_index": "test_index","_type": "base","_id": "1","_score": 1,"_source": {"name": "xiaoming","age": 18,"sex": "M"}},{"_index": "test_index","_type": "base","_id": "3","_score": 1,"_source": {"name": "xiaojiejie","age": 25,"sex": "F"}}]}
}
这里为了学习方便,先不用中文了。
took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据
条件查询
GET /test_index/base/_search?q=age:18&q=name:xiaomei&sort=age:desc
查询结果
{"took": 1,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": null,"hits": [{"_index": "test_index","_type": "base","_id": "2","_score": null,"_source": {"name": "xiaomei","age": 18,"sex": "F"},"sort": [18]}]}
}
这种只适合比较简单的查询,比如根据id;那些复杂的查询不适用。一般不使用这种查询方法。
二、Query DSL
有点类似POST请求。可以用json的格式来构建查询语法,支持复杂的语法,更加强大。常用的就是此方法。
查询全部
GET /test_index/base/_search
{"query": {"match_all": {}}
}
可以查得全部。
条件查询
GET /test_index/base/_search
{"query": {"match": {"name": "xiaomei"}},"sort": [{"age": {"order": "desc"}}]
}
查询结果
{"took": 1,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": null,"hits": [{"_index": "test_index","_type": "base","_id": "2","_score": null,"_source": {"name": "xiaomei","age": 18,"sex": "F"},"sort": [18]}]}
}
三、Query Filter
对数据过滤
如下:查询10-20岁的女性
GET /test_index/base/_search
{"query": {"bool": {"must": [{"match": {"sex": "F"}}],"filter": {"range": {"age": {"gte": 10,"lte": 20}}}}},"sort": [{"age": {"order": "desc"}}]
}
查询结果
过滤掉了25岁的xiaojiejie
{"took": 11,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": null,"hits": [{"_index": "test_index","_type": "base","_id": "2","_score": null,"_source": {"name": "xiaomei","age": 18,"sex": "F"},"sort": [18]}]}
}
四、Full-text Search 全文检索
我先加个兴趣属性,方便测试
{"took": 0,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "test_index","_type": "base","_id": "2","_score": 1,"_source": {"name": "xiaomei","age": 18,"sex": "F","interests": "music movie"}},{"_index": "test_index","_type": "base","_id": "1","_score": 1,"_source": {"name": "xiaoming","age": 18,"sex": "M","interests": "sport movie"}},{"_index": "test_index","_type": "base","_id": "3","_score": 1,"_source": {"name": "xiaojiejie","age": 25,"sex": "F","interests": "movie"}}]}
}
然后开始查询兴趣为movie的数据
GET /test_index/base/_search
{"query": {"match": {"interests": "music movie"}}
}
结果
{"took": 3,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 3,"max_score": 0.5753642,"hits": [{"_index": "test_index","_type": "base","_id": "2","_score": 0.5753642,"_source": {"name": "xiaomei","age": 18,"sex": "F","interests": "music movie"}},{"_index": "test_index","_type": "base","_id": "1","_score": 0.2876821,"_source": {"name": "xiaoming","age": 18,"sex": "M","interests": "sport movie"}},{"_index": "test_index","_type": "base","_id": "3","_score": 0.2876821,"_source": {"name": "xiaojiejie","age": 25,"sex": "F","interests": "movie"}}]}
}
可以看出查到3条记录,但score有差异。
先查看interests字段的type
GET /test_index/_mapping/base
结果
{"test_index": {"mappings": {"base": {"properties": {"age": {"type": "long"},"interests": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"name": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"sex": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}}}
}
可以看出为text类型,所以我们查询music movie时,会被拆解为music 、movie 。现在总共3条数据 满足情况如下:
name | interests | match |
---|---|---|
xiaomei | music movie | 2 |
xiaoming | sport movie | 1 |
xiaojiejie | movie | 1 |
所以匹配出3条数据,其中xiaomei的匹配度最高,score最大。
六、Phase Search 短语搜索
和全文检索不同,全部检索是拆解,然后倒排索引中搜索;短语检索是整个短语作为条件搜索。
GET /test_index/base/_search
{"query": {"match_phrase": {"interests": "music movie"}}
}
结果:只有xiaomei匹配上
{"took": 14,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 0.5753642,"hits": [{"_index": "test_index","_type": "base","_id": "2","_score": 0.5753642,"_source": {"name": "xiaomei","age": 18,"sex": "F","interests": "music movie"}}]}
}
六、Highlight search 高亮搜索
就是把一些字段高亮显示
GET /test_index/base/_search
{"query": {"match_phrase": {"interests": "music movie"}},"highlight": {"fields": {"interests": {}}}
}
结果:除了返回的数据外,另外返回了高亮显示的部分
“music movie”
{"took": 56,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 0.5753642,"hits": [{"_index": "test_index","_type": "base","_id": "2","_score": 0.5753642,"_source": {"name": "xiaomei","age": 18,"sex": "F","interests": "music movie"},"highlight": {"interests": ["<em>music</em> <em>movie</em>"]}}]}
}
Elasticsearch学习第二篇--常用的几种搜索方式相关推荐
- ElasticSearch入门 第二篇:集群配置
这是ElasticSearch 2.4 版本系列的第二篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- TLS协议学习-第二篇-握手协议
TLS协议学习第二篇,内容来自:https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-200 ...
- 常用的几种布局方式---Flex 布局(垂直居中展示)
常用的几种布局方式---Flex 布局(垂直居中展示) 前言 一.默认使用静态布局 二.flex布局 1.父元素container 1.1.display:flex 1.2.flex-directio ...
- Java框架篇---spring aop两种配置方式
Java框架篇---spring aop两种配置方式 第一种:注解配置AOP 注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中 ...
- spring入门之Spring 常用的三种注入方式
Spring 常用的三种注入方式 Spring 通过 DI(依赖注入)实现 IOC(控制反转),常用的注入方式主要有三种:构造方法注入,set 方法注入,基于注解的注入. 一.通过构造方法注入 先简单 ...
- Map接口常用的几种遍历方式与小练习
Map接口常用的几种遍历方式与小练习 keyset:获取所有的键,我们可以通过键获取值 entryset:获取所有的键值对 values:获取所有的值:此方法只能获取到value,无法获取key pa ...
- pcb 理论阻值、 过孔_超实用!PCB设计中过孔常用的6种处理方式
原标题:超实用!PCB设计中过孔常用的6种处理方式 小伙伴们我们又见面啦! 上一次的" 神仙过孔 ",还没过瘾吧? <整齐的过孔固然符合审美,但是却...> 今天的小课 ...
- 【温故知新】——原生js中常用的四种循环方式
一.引言 本文主要是利用一个例子,讲一下原生js中常用的四种循环方式的使用与区别: 实现效果: 在网页中弹出框输入0 网页输出"欢迎下次光临" 在网页中弹出框输入1 网页输 ...
- vpwm的控制变频_变频器常用的几种控制方式
变频器常用的几种控制方式 变频器常用的几种控制方式 变频调速技术就是现代电力传动技术的重要发展方向 , 而作为变频调速系 统的核心-变频器的性能也越来越成为调速性能优劣的决定因素 , 除了变频器本 身 ...
最新文章
- 最新!QS发布2021亚洲大学排行榜!清华第二,浙大、复旦力压北大
- Android Color 判断色值小结
- 聊聊flink的logback配置
- pgsql查表名_PostgreSQL 查询一个表
- 如何用python制作动画_如何基于Python Matplotlib实现网格动画
- 小白用python处理excel文件-刚入门的小白用Python操作excel表格!使工作效率提升一倍不止!...
- Vue项目上传github并预览
- 计算机软件毕业论文教师指导记录,毕业论文导师指导记录【毕业论文指导记录(精选多篇)】...
- 头脑王者 物理化学生物
- SharePoint - CAML
- 计算机数字信号和模拟信号,模拟信号和数字信号有什么区别
- 软件测试面试该如何谈薪
- Uber收购动作引发巨震 美国外卖“三国杀”有望诞生“美团”?
- OBS更改标题?标题一改,安能辨我是雌雄?
- 北交桑基韬:“超”人的机器学习,非语义特征的得与失
- 【电气安全】关于医疗专用隔离电源系统在医院配电系统中的应用
- 看了让人吐血的146个脑筋急转弯问题
- 2. WordCount案例实操
- 草食系的“恋爱秘方”
- IEEE SIDAS 2016参会总结