Elasticsearch主要是三个功能,存储、检索、和分析。存储功能在入门篇里已经讲过,进阶篇主要讲检索和分析(聚合)。
测试数据:
ES测试数据.txt

一、检索:Query DSL

1、语法

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }
  1. _search API可以使用在后面拼接参数的方式查询,更多是用请求体的方式查询,基本格式如下
  2. GET /索引/_search
  3. {
  4. "query":{
  5. "限制的条件":....
  6. }
  7. }
  8. 限制的条件有很多种,比如match、matchall、bool等等,接下来会分别介绍常用的几种

2、查询全部:match_all

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

表示查出当前索引的所有数据

3、全文检索:match

格式如下:

  1. {
  2. "query": {
  3. "match": {
  4. "字段": "值"
  5. }
  6. }
  7. }

例如:

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match": {
  5. "firstname": "Amber"
  6. }
  7. }
  8. }

表示查找bank索引下的firstname字段为Amber的数据,检索结果如下
image.png

默认分词

默认将条件中的值分词去匹配,只要满足其中的一个就会被检索出来,匹配的越多,则得分越高,得分最高的那个则是命中最多的数据,例如:

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match": {
  5. "address": "Holmes Lane"
  6. }
  7. }
  8. }

匹配值为Holmes Lane的地址,查询结果如下:
image.png

4、非text类型匹配:term

和match的用法类似,不过term不能用来匹配text类型的数据,只能匹配数字之类的非文本类型,官网原话:
image.png
翻译:避免使用term查询text类型的字段。(term)
例如:

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "term": {
  5. "address": "Holmes Lane"
  6. }
  7. }
  8. }

这样是查不出结果的:
image.png
当查询非text字段时,比如数字:

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "term": {
  5. "age": "32"
  6. }
  7. }
  8. }

image.png
和使用match查询的结果是一样的,一般推荐使用match查询text类型的字段,而term查询其他类型的字段

5、匹配短语:match_phrase

match会自动分词,而match_phrase则是用条件中的短语进行匹配,匹配出来的数据是全部包含此短语的。例如:

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "address": "Holmes Lane"
  6. }
  7. }
  8. }

结果:
image.png

6、精确匹配:keyword

在使用match查询时,字段后面加上keyword表示精确查询,也是匹配短语,但和match_phrase不同的是,keyword必须是全部包含,也就是等于整个短语时才能检索出来,例如:

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match": {
  5. "address.keyword": "880 Holmes Lane"
  6. }
  7. }
  8. }

结果:
image.png
当只匹配部分地址时,则匹配不出来:

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match": {
  5. "address.keyword": " Holmes Lane"
  6. }
  7. }
  8. }

结果:
image.png
使用match_phase的匹配

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "address": " Holmes Lane"
  6. }
  7. }
  8. }

image.png

7、多字段匹配:multi_match

可以使用多个字段来匹配,为了更好的展示查询结果,将id为970的文档的state字段改为mill,address的mill去除

  1. POST /bank/_doc/970
  2. {
  3. "account_number" : 970,
  4. "balance" : 19648,
  5. "firstname" : "Forbes",
  6. "lastname" : "Wallace",
  7. "age" : 28,
  8. "gender" : "M",
  9. "address" : "990 Road",
  10. "employer" : "Pheast",
  11. "email" : "forbeswallace@pheast.com",
  12. "city" : "Lopezo",
  13. "state" : "mill"
  14. }

再使用multi_match查询

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "mill",
  6. "fields": ["address" ,"state"]
  7. }
  8. }
  9. }

结果如下:
image.png
state字段和address字段中包含mill的都会被查询出来

8、复合查询:bool

类似mysql中的使用or或and之类的查询

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "address":" Holmes Lane"
  9. }
  10. }
  11. ]
  12. , "must_not": [
  13. {"match": {
  14. "age": "32"
  15. }}
  16. ],
  17. "should": [
  18. {"match": {
  19. "state": "GA"
  20. }}
  21. ]
  22. }
  23. }
  24. }

表示查询address含有Holmes Lane(允许分词),并且age不等于32的,或者state等于GA的
结果如下:

must

必须满足must里的条件

must_not

必须不能满足must_not里的条件

should

应该满足,不过就算不满足也没关系,如果满足了,则会使命中的分数更高
image.png

注意:filter

filter和must一样,用来过滤出满足条件的数据,但是filter不会影响得分score,而must会,所以一般在查询条件最后加上filter来过滤不想要的数据(结果过滤)

二、分析:aggregations(执行聚合)

在检索出需要的数据后,正常需要做一些分析,提取我们想要的信息,例如统计某个每个年龄段的