Elasticsearch 基本操作

Http操作:

高级查询:

高级查询:Elasticsearch 提供了基于 JSON 提供完整的查询 DSL 来定义查询

初始化数据:

es _template查询 es http查询_服务器

查询所有文档:

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_elasticsearch_02


es _template查询 es http查询_字段_03


返回值:

es _template查询 es http查询_服务器_04


返回值解释:

es _template查询 es http查询_elasticsearch_05

匹配查询:

match 匹配类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是 or 的关系

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_es _template查询_06

返回结果为:

es _template查询 es http查询_es _template查询_07

字段匹配查询:

multi_match 与 match 类似,不同的是它可以在多个字段中查询

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_elasticsearch_08


返回结果为:

es _template查询 es http查询_字段_09

关键字精确查询:

term 查询,精确的关键词匹配查询,不对查询条件进行分词

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_字段_10


返回结果为:

es _template查询 es http查询_字段_11

多关键字精确查询:

terms 查询和 term 查询一样,但它允许你指定多值进行匹配。

如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于 mysql 的 in

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_字段_12


返回结果为:

es _template查询 es http查询_搜索引擎_13

指定查询字段:

默认情况下,Elasticsearch 在搜索的结果中,会把文档中保存在_source 的所有字段都返回。

如果我们只想获取其中的部分字段,我们可以添加_source 的过滤

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_搜索引擎_14


返回结果为:

es _template查询 es http查询_es _template查询_15

过滤字段:

我们也可以通过:includes:来指定想要显示的字段、excludes:来指定不想要显示的字段

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_es _template查询_16


返回结果为:

es _template查询 es http查询_elasticsearch_17

组合查询:

bool把各种其它查询通过must(必须 )、must_not(必须不)、should(应该)的方式进行组合

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_搜索引擎_18


返回结果:

es _template查询 es http查询_elasticsearch_19

范围查询:

range 查询找出那些落在指定区间内的数字或者时间。range 查询允许以下字符

es _template查询 es http查询_elasticsearch_20


在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_es _template查询_21


返回结果:

es _template查询 es http查询_字段_22

模糊查询:

返回包含与搜索字词相似的字词的文档。

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_搜索引擎_23


返回结果:

es _template查询 es http查询_字段_24

单字段排序:

sort 可以让我们按照不同的字段进行排序,并且通过 order 指定排序的方式。desc 降序,asc升序。

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_elasticsearch_25


返回结果:

es _template查询 es http查询_搜索引擎_26

高亮查询:

在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮

在Bing搜索“京东”

es _template查询 es http查询_服务器_27


Elasticsearch 可以对查询内容中的关键字部分,进行标签和样式(高亮)的设置。

在使用 match 查询的同时,加上一个 highlight 属性:

  • pre_tags:前置标签
  • post_tags:后置标签
  • fields:需要高亮的字段
  • title:这里声明 title 字段需要高亮,后面可以为这个字段设置特有配置,也可以空

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_elasticsearch_28


返回结果:

es _template查询 es http查询_es _template查询_29

分页查询:
  • from:当前页的起始索引,默认从 0 开始。 from = (pageNum - 1) * size
  • size:每页显示多少条

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

es _template查询 es http查询_搜索引擎_30


返回结果:

es _template查询 es http查询_字段_31

聚合查询:

聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的 group by,当然还有很多其他的聚合,例如取最大值、平均值等等。

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

对某个字段取最大值 max:

es _template查询 es http查询_搜索引擎_32


在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

对某个字段取最小值 min:

es _template查询 es http查询_搜索引擎_33

对某个字段求和 sum:

es _template查询 es http查询_elasticsearch_34

对某个字段取平均值 avg:

es _template查询 es http查询_搜索引擎_35

对某个字段的值进行去重之后再取总数:

es _template查询 es http查询_elasticsearch_36

State 聚合:
  • stats 聚合,对某个字段一次性返回 count,max,min,avg 和 sum 五个指标

es _template查询 es http查询_elasticsearch_37

桶聚合查询:

桶聚合相当于 sql 中的 group by 语句

在 Postman 中,向 ES 服务器发 GET 请求 :http://172.18.20.254:9200/shopping/_search

terms 聚合,分组统计

es _template查询 es http查询_服务器_38


返回结果:

es _template查询 es http查询_elasticsearch_39

在 terms 分组下再进行聚合

es _template查询 es http查询_elasticsearch_40


返回结果:

es _template查询 es http查询_服务器_41