Elasticsearch系列(二)--query、filter、aggregations
本文基于ES6.4版本,我也是出于学习阶段,对学习内容做个记录,如果文中有错误,请指出。
实验数据:
index:book
type:novel
mappings:
- {
- "mappings": {
- "novel": {
- "dynamic": "false",
- "properties": {
- "word_count": {
- "type": "integer"
- },
- "author": {
- "type": "keyword"
- },
- "title": {
- "type": "text"
- },
- "publish_date": {
- "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
- "type": "date"
- }
- }
- }
- }
- }
通过put创建索引,使用head可视化界面,数据如下:
Elasticsearch的查询分为:
1、子条件查询:查询特定字段的特定值
Query context
查询过程中,除了判断Document是否满足条件,还会计算出_score表示匹配程度,数值越大,证明匹配程度越高
1、查询全部:/book/novel/_search
- "hits": {
- "total": 10,
- "max_score": 1.0,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "5",
- "_score": 1.0,
- "_source": {
- "title": "永夜君王",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "烟雨江南"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "8",
- "_score": 1.0,
- "_source": {
- "title": "万古令",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "听奕"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "9",
- "_score": 1.0,
- "_source": {
- "title": "天帝传",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "飞天鱼"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "10",
- "_score": 1.0,
- "_source": {
- "title": "剑来",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "烽火戏诸侯"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "2",
- "_score": 1.0,
- "_source": {
- "title": "完美世界",
- "word_count": "130000",
- "publish_date": "2017-03-01",
- "author": "辰东"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "4",
- "_score": 1.0,
- "_source": {
- "title": "民国谍影",
- "word_count": "110000",
- "publish_date": "2019-03-01",
- "author": "寻青藤"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "6",
- "_score": 1.0,
- "_source": {
- "title": "遮天",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "辰东"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "1",
- "_score": 1.0,
- "_source": {
- "title": "万古神帝",
- "word_count": "30000",
- "publish_date": "2017-01-01",
- "author": "飞天鱼"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "7",
- "_score": 1.0,
- "_source": {
- "title": "圣墟",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "辰东"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "3",
- "_score": 1.0,
- "_source": {
- "title": "星辰变",
- "word_count": "100000",
- "publish_date": "2018-03-01",
- "author": "我吃西红柿"
- }
- }
- ]
- }
2、查询id为1的数据:/book/novel/1
- {
- "_index": "book",
- "_type": "novel",
- "_id": "1",
- "_version": 1,
- "found": true,
- "_source": {
- "title": "万古神帝",
- "word_count": "30000",
- "publish_date": "2017-01-01",
- "author": "飞天鱼"
- }
- }
3、只查询title和author字段:/1?_source=title,author
- {
- "_index": "book",
- "_type": "novel",
- "_id": "1",
- "_version": 1,
- "found": true,
- "_source": {
- "author": "飞天鱼",
- "title": "万古神帝"
- }
- }
4、只是显示_source部分:/book/novel/1/_source
- {
- "title": "万古神帝",
- "word_count": "30000",
- "publish_date": "2017-01-01",
- "author": "飞天鱼"
- }
5、筛选单字段查询:/book/novel/_search
- {
- "query": {
- "match": {
- "author": "飞天鱼"
- }
- }
- }
- "hits": {
- "total": 2,
- "max_score": 1.2039728,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "9",
- "_score": 1.2039728,
- "_source": {
- "title": "天帝传",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "飞天鱼"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "1",
- "_score": 0.6931472,
- "_source": {
- "title": "万古神帝",
- "word_count": "30000",
- "publish_date": "2017-01-01",
- "author": "飞天鱼"
- }
- }
- ]
- }
6、limit:我们查询到2条数据,如果我们只想得到第一条数据,可以使用from和size联合查询
- {
- "query": {
- "match": {
- "author": "飞天鱼"
- }
- },
- "from": 0,
- "size": 1
- }
- "hits": {
- "total": 2,
- "max_score": 1.2039728,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "9",
- "_score": 1.2039728,
- "_source": {
- "title": "天帝传",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "飞天鱼"
- }
- }
- ]
- }
- {
- "query": {
- "match": {
- "author": "辰东"
- }
- },
- "sort": [
- {
- "word_count": {
- "order": "desc"
- }
- }
- ]
- }
- "hits": {
- "total": 3,
- "max_score": null,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "2",
- "_score": null,
- "_source": {
- "title": "完美世界",
- "word_count": "130000",
- "publish_date": "2017-03-01",
- "author": "辰东"
- },
- "sort": [
- 130000
- ]
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "6",
- "_score": null,
- "_source": {
- "title": "遮天",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "辰东"
- },
- "sort": [
- 110000
- ]
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "7",
- "_score": null,
- "_source": {
- "title": "圣墟",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "辰东"
- },
- "sort": [
- 110000
- ]
- }
- ]
- }
8、其余匹配match_phrase:
query、match的方式本质上就是模糊查询,而且中文会自动分词到最大粒度,可以看到会查询到只要匹配任意一个字都是可以的
- {
- "query": {
- "match": {
- "title": "万古神帝"
- }
- }
- }
- "hits": {
- "total": 3,
- "max_score": 2.439878,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "1",
- "_score": 2.439878,
- "_source": {
- "title": "万古神帝",
- "word_count": "30000",
- "publish_date": "2017-01-01",
- "author": "飞天鱼"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "8",
- "_score": 2.4079456,
- "_source": {
- "title": "万古令",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "听奕"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "9",
- "_score": 1.2039728,
- "_source": {
- "title": "天帝传",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "飞天鱼"
- }
- }
- ]
- }
所以这里有了其余匹配match_phrase,结果只有完全包含"万古神帝"的title才可以被查询到
- {
- "query": {
- "match_phrase": {
- "title": "万古神帝"
- }
- }
- }
- "hits": {
- "total": 1,
- "max_score": 2.439878,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "1",
- "_score": 2.439878,
- "_source": {
- "title": "万古神帝",
- "word_count": "30000",
- "publish_date": "2017-01-01",
- "author": "飞天鱼"
- }
- }
- ]
- }
9、多条件查询multi_match:查询title或者author包含"万古神帝"的数据
- {
- "query": {
- "multi_match": {
- "query": "万古神天",
- "fields": ["title","author"]
- }
- }
- }
- "hits": {
- "total": 4,
- "max_score": 2.4079456,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "8",
- "_score": 2.4079456,
- "_source": {
- "title": "万古令",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "听奕"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "1",
- "_score": 1.8299085,
- "_source": {
- "title": "万古神帝",
- "word_count": "30000",
- "publish_date": "2017-01-01",
- "author": "飞天鱼"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "9",
- "_score": 1.2039728,
- "_source": {
- "title": "天帝传",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "飞天鱼"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "6",
- "_score": 1.1727304,
- "_source": {
- "title": "遮天",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "辰东"
- }
- }
- ]
- }
10、语法查询query_string:
- {
- "query": {
- "query_string": {
- "query": "万古"
- }
- }
- }
这里和match没有区别,query可以使用AND和OR,match的filed也可以,注意这里一定是大写,小写就被当做搜索的内容了
- {
- "query": {
- "query_string": {
- "query": "万古 OR 剑来"
- }
- }
- }
- {
- "query": {
- "match": {
- "title": "万古 OR 剑来"
- }
- }
- }
指定fields:
- {
- "query": {
- "query_string": {
- "query": "万古 OR 剑来 OR 辰东 ",
- "fields": ["author","title"]
- }
- }
- }
11、精确匹配term:
title为text类型,author为keyword类型,实验发现查询title只有是单个字的时候才能匹配(精确匹配查不到数据),而author必须是精确匹配
例如:title不支持精确匹配,支持模糊查询(而且是单个字才可以,多个字照样查不到数据)
- {
- "query": {
- "term": {
- "title": "剑来"
- }
- }
- }
如果只是查询一个字就可以
- {
- "query": {
- "term": {
- "title": "来"
- }
- }
- }
- "hits": {
- "total": 1,
- "max_score": 1.3940737,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "10",
- "_score": 1.3940737,
- "_source": {
- "title": "剑来",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "烽火戏诸侯"
- }
- }
- ]
- }
查询author字段:有三条数据
- {
- "query": {
- "term": {
- "author": "辰东"
- }
- }
- }
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "7",
- "_score": 0.6931472,
- "_source": {
- "title": "圣墟",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "辰东"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "2",
- "_score": 0.47000363,
- "_source": {
- "title": "完美世界",
- "word_count": "130000",
- "publish_date": "2017-03-01",
- "author": "辰东"
- }
- },
- {
- "_index": "book",
- "_type": "novel",
- "_id": "6",
- "_score": 0.47000363,
- "_source": {
- "title": "遮天",
- "word_count": "110000",
- "publish_date": "2015-03-01",
- "author": "辰东"
- }
- }
- ]
- }
author不知道模糊查询:下面结果为null
- {
- "query": {
- "term": {
- "author": "东"
- }
- }
- }
12、范围查找range:包括integer和日期类型,日期支持now函数,也就是当前日期
- {
- "query": {
- "range": {
- "word_count": {
- "gt": 110000,
- "lte": 130000
- }
- }
- }
- }
- "hits": {
- "total": 1,
- "max_score": 1.0,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "2",
- "_score": 1.0,
- "_source": {
- "title": "完美世界",
- "word_count": "130000",
- "publish_date": "2017-03-01",
- "author": "辰东"
- }
- }
- ]
- }
Filter context
查询过程中,只是判断Document是否满足条件,只有yes or no。用来做数据过滤,而且ES还会对结果进行缓存,效率相对query更高一点
- {
- "query": {
- "bool": {
- "filter": {
- "term": {
- "word_count": 130000
- }
- }
- }
- }
- }
- "hits": {
- "total": 1,
- "max_score": 0.0,
- "hits": [
- {
- "_index": "book",
- "_type": "novel",
- "_id": "2",
- "_score": 0.0,
- "_source": {
- "title": "完美世界",
- "word_count": "130000",
- "publish_date": "2017-03-01",
- "author": "辰东"
- }
- }
- ]
- }
2、复合条件查询:组合子条件查询
1、固定分数查询:不支持match,支持filter
- {
- "query": {
- "constant_score": {
- "filter": {
- "match": {
- "title": "天帝传"
- }
- }
- }
- }
- }
- {
- "query": {
- "constant_score": {
- "filter": {
- "match": {
- "title": "天帝传"
- }
- },
- "boost": 2
- }
- }
- }
2、bool查询:
should:就是or的关系
- {
- "query": {
- "bool": {
- "should": [
- {
- "match": {
- "author": "辰东"
- }
- },
- {
- "match": {
- "title": "天帝传"
- }
- }
- ]
- }
- }
- }
must:相当于and
- {
- "query": {
- "bool": {
- "must": [
- {
- "match": {
- "author": "辰东"
- }
- },
- {
- "match": {
- "title": "天帝传"
- }
- }
- ]
- }
- }
- }
must_not:相当于<>
- {
- "query": {
- "bool": {
- "must_not": {
- "term": {
- "author": "辰东"
- }
- }
- }
- }
- }
bool查询也可以使用filter:
- {
- "query": {
- "bool": {
- "must": [
- {
- "match": {
- "author": "辰东"
- }
- },
- {
- "match": {
- "title": "天帝传"
- }
- }
- ],
- "filter": [
- {
- "term": {
- "word_count": 110000
- }
- }
- ]
- }
- }
- }
aggregations:
- {
- "aggs": {
- "group_by_author": {
- "terms": {
- "field": "author"
- }
- }
- }
- }
- "aggregations": {
- "group_by_author": {
- "doc_count_error_upper_bound": 0,
- "sum_other_doc_count": 0,
- "buckets": [
- {
- "key": "辰东",
- "doc_count": 3
- },
- {
- "key": "飞天鱼",
- "doc_count": 2
- },
- {
- "key": "听奕",
- "doc_count": 1
- },
- {
- "key": "寻青藤",
- "doc_count": 1
- },
- {
- "key": "我吃西红柿",
- "doc_count": 1
- },
- {
- "key": "烟雨江南",
- "doc_count": 1
- },
- {
- "key": "烽火戏诸侯",
- "doc_count": 1
- }
- ]
- }
- }
支持多聚合结果:
- {
- "aggs": {
- "group_by_author": {
- "terms": {
- "field": "author"
- }
- },
- "group_by_word_count": {
- "terms": {
- "field": "word_count"
- }
- }
- }
- }
aggregations除了支持term,还有stats、min、max、avg等
- {
- "aggs": {
- "group_by_author": {
- "stats": {
- "field": "word_count"
- }
- }
- }
- }
- "aggregations": {
- "group_by_author": {
- "count": 10,
- "min": 30000.0,
- "max": 130000.0,
- "avg": 103000.0,
- "sum": 1030000.0
- }
- }
avg:
- {
- "aggs": {
- "group_by_author": {
- "avg": {
- "field": "word_count"
- }
- }
- }
- }
Elasticsearch系列(二)--query、filter、aggregations的更多相关文章
- elasticsearch系列二:索引详解(快速入门、索引管理、映射详解、索引别名)
一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...
- Elasticsearch学习笔记(十二)filter与query
一.keyword 字段和keyword数据类型 1.测试准备数据 POST /forum/article/_bulk { "index": { "_id" ...
- WEB API 系列(二) Filter的使用以及执行顺序
在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...
- Web API系列(二) Filter的使用以及执行顺序
在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...
- Elasticsearch系列---常见搜索方式与聚合分析
概要 本篇主要介绍常见的6种搜索方式.聚合分析语法,基本是上机实战,可以和关系型数据库作对比,如果之前了解关系型数据库,那本篇只需要了解搜索和聚合的语法规则就可以了. 搜索响应报文 以上篇建立的mus ...
- Elasticsearch入门教程(六):Elasticsearch查询(二)
原文:Elasticsearch入门教程(六):Elasticsearch查询(二) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...
- Wireshark入门与进阶系列(二)
摘自http://blog.csdn.net/howeverpf/article/details/40743705 Wireshark入门与进阶系列(二) “君子生非异也,善假于物也”---荀子 本文 ...
- Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能
Android高效率编码-第三方SDK详解系列(二)--Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能 我的本意是第二篇写Mob的shareSD ...
- 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+kibanaV5.4.2+x-packV5.4.2安装
相关博文: 搜索引擎ElasticSearchV5.4.2系列一之ES介绍 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+klanaV5.4.2+x-p ...
随机推荐
- MYSQL - database 以及 table 的增删改查
MYSQL - database 以及 table 的增删改查 MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这 ...
- shiro框架的组成和内部结构(下一篇为spring整合shiro)
1.shiro简介 Apache Shiro是Java的一个安全框架.功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案. 实际上,Shir ...
- CF459E Pashmak and Graph (Dag dp)
传送门 解题思路 \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了.注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移. 代码 ...
- 如何有效管理Windows系统帐户权限
权限是Windows管理的基础,当然与Windows用户关系最密切,平时接触最多的是与帐户相关的权限.对于Windows帐户权限的管理,你是否完全了解呢?下面,笔者以Winsows XP为例进行相关测 ...
- fatal error U1087: cannot have : and :: dependents for same target Stop.
转自VC错误:http://www.vcerror.com/?p=72 问题描述: 完成后编译,发现有错误 D:\WinDDK\7600.16385.1\bin\makefile.new(7117) ...
- System.Math.cs
ylbtech-System.Math.cs 1. 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c56193 ...
- play framework 从环境搭建到简单运行
download 官网:https://www.playframework.com/ 将zip下载至本地,进行unzip. 环境变量 检测是否安装成功:解压成功后进入解压的目录,运行 play 终端显 ...
- <a>标签的SEO优化细节
<a>标签的SEO优化细节 如果需要在新窗口中打开链接,我们使用的方法是在a上加上taget=“_blank”,但很多人不知道这是不符合w3c的规范的,在使用严格的DOCTYPE(xhtm ...
- Java之RabbitMQ(一)与SpringBoot整合
应用场景:异步处理.应用解耦.流量削峰 参照:https://blog.csdn.net/weixin_38364973/article/details/82834348 开端 RabbitAutoC ...
- html自定义分页
public class MyPager { /// <summary> /// 每一页数据的条数 /// </summary> public int PageSize { g ...