Elasticsearch系列(二)--query、filter、aggregations

2024-01-02 04:20

本文主要是介绍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"}}}}
}
View Code

通过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": "我吃西红柿"}}]}
View Code

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": "飞天鱼"}
}
View Code

3、只查询title和author字段:/1?_source=title,author

{"_index": "book","_type": "novel","_id": "1","_version": 1,"found": true,"_source": {"author": "飞天鱼","title": "万古神帝"}
}
View Code

4、只是显示_source部分:/book/novel/1/_source

{"title": "万古神帝","word_count": "30000","publish_date": "2017-01-01","author": "飞天鱼"
}
View Code

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": "飞天鱼"}}]}
View Code

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": "飞天鱼"}}]}
View Code
hits.total=2,但是只返回了第一条数据,from为从第几条开始,size我返回的条数
7、order by
这里选择对word_count字段进行倒叙排序
{"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]}]}
View Code

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": "飞天鱼"}}]}
View Code 

所以这里有了其余匹配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": "飞天鱼"}}]}
View Code

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": "辰东"}}]}
View Code

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": "烽火戏诸侯"}}]}
View Code

查询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": "辰东"}}]}
View Code

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": "辰东"}}]}
View Code
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": "辰东"}}]}
View Code
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}]}}
View Code

支持多聚合结果:

{"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"}}}
}

转载于:https://www.cnblogs.com/huigelaile/p/11299105.html

这篇关于Elasticsearch系列(二)--query、filter、aggregations的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/561318

相关文章

JavaWeb系列二十: jQuery的DOM操作 下

jQuery的DOM操作 CSS-DOM操作多选框案例页面加载完毕触发方法作业布置jQuery获取选中复选框的值jQuery控制checkbox被选中jQuery控制(全选/全不选/反选)jQuery动态添加删除用户 CSS-DOM操作 获取和设置元素的样式属性: css()获取和设置元素透明度: opacity属性获取和设置元素高度, 宽度: height(), widt

我与Bloom filter

1 海量网页判断用Bloom Filter 面试的时候,一个面试官问我说:“有一个网络爬虫,爬虫程序会不停地爬取页面上的每一个网页,并把爬取后的网页给存储起来,那么爬虫如何判定现在在爬的网页有没有被爬过。” 我当时卡住了半天回答不上来。 面试官给我说用Bloom Filter。 Bloom Filter把爬取过的网页映射到Bloom Filter内,如果再爬取到该网页,Bloom Filt

C语言入门系列:探秘二级指针与多级指针的奇妙世界

文章目录 一,指针的回忆杀1,指针的概念2,指针的声明和赋值3,指针的使用3.1 直接给指针变量赋值3.2 通过*运算符读写指针指向的内存3.2.1 读3.2.2 写 二,二级指针详解1,定义2,示例说明3,二级指针与一级指针、普通变量的关系3.1,与一级指针的关系3.2,与普通变量的关系,示例说明 4,二级指针的常见用途5,二级指针扩展到多级指针 小结 C语言的学习之旅中,二级

JavaWeb系列六: 动态WEB开发核心(Servlet) 上

韩老师学生 官网文档为什么会出现Servlet什么是ServletServlet在JavaWeb项目位置Servlet基本使用Servlet开发方式说明快速入门- 手动开发 servlet浏览器请求Servlet UML分析Servlet生命周期GET和POST请求分发处理通过继承HttpServlet开发ServletIDEA配置ServletServlet注意事项和细节 Servlet注

C语言入门系列:初识函数

文章目录 一,C语言函数与数学函数的区别1,回忆杀-初中数学2,C语言中的函数 二, 函数的声明1,函数头1.1,函数名称1.2,返回值类型1.3,参数列表 2,函数体2.1,函数体2.2,return语句 三,main函数四,函数的参数与传递方式1,实参和形参1.1,函数定义(含形参)1.2,函数调用(使用实参) 2,参数传递方式2.1,值传递2.2,引用传递 五,函数原型与预声明1,

django学习入门系列之第三点《案例 小米商城头标》

文章目录 阴影案例 小米商城头标往期回顾 阴影 设置阴影 box-shadow:水平方向 垂直方向 模糊距离 颜色 box-shadow: 5px 5px 5px #aaa; 案例 小米商城头标 目标样式: CSS中的代码 /*使外边距等于0,即让边框与界面贴合*/body{margin: 0;}/*控制父级边框*/.header{backgroun

国产数据库 - 内核特性 - CloudberryDB中的Runtime Filter

国产数据库 - 内核特性 - CloudberryDB中的Runtime Filter 今年5月份GreenPlum官方将GitHub仓库代码全部删除,各个分支的issues和bugs讨论等信息全部清除,仅将master分支代码进行归档。对于国内应用GPDB的用户来说,这是一个挑战性事件,对与后期维护、升级等都变得非常困难。有幸HashData开源了基于GP衍生版本CloudberryDB版本,

MyBatis系列之分页插件及问题

概述 无论是C端产品页面,还是后台系统页面,不可能一次性将全部数据加载出来。后台系统一般都是PC端登录,用Table组件(如Ant Design Table)渲染展示数据,可点击列表的下一页(或指定某一页)查看数据。C端产品如App,在下滑时可查看更多数据,看起来像是一次性加载数据,实际上也是分批请求后台系统获取数据。而这,就是分页功能。 如果没有使用Hibernate或MyBatis这样的O

CSS列表属性:list-style系列属性详解

CSS(层叠样式表)是用于控制网页样式的一种语言,它允许开发者以一种非常灵活的方式来设置网页元素的外观。在CSS中,list-style属性族是专门用来设置列表样式的。列表是网页设计中常见的元素,它们可以是有序列表(<ol>)或无序列表(<ul>)。list-style系列属性允许你自定义列表项前的标记,包括类型、位置和图像。 1. list-style-type list-style-typ

ArkTS开发系列之导航 (2.7动画)

上篇回顾: ArkTS开发系列之导航 (2.6 图形) 本篇内容:动画的学习使用 一、 知识储备 1. 布局更新动画 包含显式动画(animateTo)和属性动画(animation) 动画类型名称特点显式动画闭包内的变化都会触发动画执行, 可以做较复杂的动画属性动画属性变化时触发动画执行, 设置简单 说白了,显示动画就是靠闭包事件触发,属性动画是挂在组件身上的属性变化触发 显式动画