参考:https://blog.csdn.net/qq_26230421/article/details/80366649

集群

  1. # 查看集群是否健康
  2. GET _cat/health?v
  3. # 查看集群节点
  4. GET _cat/nodes?v
  5. # 查看有什么索引
  6. GET _cat/indices

索引

一个分片就是一个Lucene索引,根据自己情况合理设置,每30G(20亿数据)新增一个分片。 备份数可以提高查询性能,根据节点数合理设置,一般1-3个。

  • 创建
  1. PUT /people
  2. {
  3. "settings": {
  4. "number_of_shards": 1, # 分片数
  5. "number_of_replicas": 0, # 备份数
  6. "index.analysis.analyzer.default.type": "ik_max_word" # 自定义默认分词器,默认:english
  7. }
  8. }
  • 删除
  1. DELETE /people
  2. # 也可以删除多个索引
  3. DELETE /index_one,index_two

mapping

  1. 追加或设置mapping
  2. PUT /people/_mapping/doc
  3. {
  4. "dynamic": "strict",
  5. "properties": {
  6. "name": {
  7. "type": "text",
  8. "analyzer": "ik_max_word", # 索引分词器,如果不加,则text会使用默认分词器,
  9. "search_analyzer": "ik-smart" # 搜索分词器,如果不加,默认和索引分词器一致。
  10. },
  11. "age": {
  12. "type": "integer"
  13. },
  14. "birthday": {
  15. "type": "date",
  16. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  17. }
  18. }
  19. }
  20. # 可以为字段设置keyword(方便精确匹配)
  21. PUT /people/_mapping/man
  22. {
  23. "properties": {
  24. "question":{
  25. "type": "text",
  26. "fields": {
  27. "keyword":{
  28. "type": "keyword"
  29. }
  30. }
  31. }
  32. }
  33. }
  34. # 查看mapping
  35. GET /people/_mapping

“dynamic”: “strict” 表示如果遇到陌生field会报错 “dynamic”: “true” 表示如果遇到陌生字段,就进行dynamic mapping

“dynamic”: “false” 表示如果遇到陌生字段,就忽略

别名

  1. # 创建别名
  2. PUT /people/_alias/test
  3. # 查询索引的别名
  4. GET /people/_alias
  5. >>>
  6. {
  7. "people": {
  8. "aliases": {
  9. "test": {}
  10. }
  11. }
  12. }
  13. # 查询别名指向哪一个索引
  14. GET /*/_alias/test
  15. >>>
  16. {
  17. "people": {
  18. "aliases": {
  19. "test": {}
  20. }
  21. }
  22. }

文档

增加

put必须设置id,post则不用

  1. PUT /people/man/1
  2. {
  3. "name": "叶良辰",
  4. "country": "china",
  5. "age": 25,
  6. "birthday": "1993-01-01",
  7. "desc": "叶良辰,本地人,狂妄自大,惹了他,会有一百种方法让你呆不下去!与赵日天是好基友,两个人风风火火闯九州"
  8. }
  9. 》》》
  10. {
  11. "_index": "people",
  12. "_type": "man",
  13. "_id": "1",
  14. "_version": 1,
  15. "result": "created",
  16. "_shards": {
  17. "total": 1,
  18. "successful": 1,
  19. "failed": 0
  20. },
  21. "created": true
  22. }
  1. POST /people/man
  2. {
  3. "name": "叶良辰",
  4. "country": "china",
  5. "age": 25,
  6. "birthday": "1993-01-01",
  7. "desc": "叶良辰,本地人,狂妄自大,惹了他,会有一百种方法让你待不下去!与赵日天是好基友,两个人风风火火闯九州"
  8. }
  9. 》》》
  10. {
  11. "_index": "people",
  12. "_type": "man",
  13. "_id": "AW7P2HV23ydeaETTj2Cz",
  14. "_version": 1,
  15. "result": "created",
  16. "_shards": {
  17. "total": 1,
  18. "successful": 1,
  19. "failed": 0
  20. },
  21. "created": true
  22. }

修改

  • 全文修改使用的是PUT命令,把所有字段都带上
  1. PUT /people/man/1
  2. {
  3. "name": "叶良辰",
  4. "country": "china",
  5. "age": 26,
  6. "birthday": "1993-01-01",
  7. "desc": "叶良辰,本地人,狂妄自大,惹了他,会有一百种方法让你呆不下去!与赵日天是好基友,两个人风风火火闯九州"
  8. }
  9. 》》》
  10. {
  11. "_index": "people",
  12. "_type": "man",
  13. "_id": "1",
  14. "_version": 2,
  15. "result": "updated",
  16. "_shards": {
  17. "total": 1,
  18. "successful": 1,
  19. "failed": 0
  20. },
  21. "created": false
  22. }
  • 部分修改使用POST
  1. POST /people/man/1/_update
  2. {
  3. "doc": {
  4. "name": "zhangsan"
  5. }
  6. }
  7. 》》》
  8. {
  9. "_index": "people",
  10. "_type": "man",
  11. "_id": "1",
  12. "_version": 3,
  13. "result": "updated",
  14. "_shards": {
  15. "total": 1,
  16. "successful": 1,
  17. "failed": 0
  18. }
  19. }
  • 脚本修改
  1. POST /people/man/1/_update
  2. {
  3. "script": "ctx._source.age += 10"
  4. }

删除

  1. DELETE /people/man/1
  2. POST /people/man/_delete_by_query?conflicts=proceed
  3. {
  4. "query": {
  5. "match_all": {}
  6. }
  7. }

查询

简单查询

match分词查询

  1. 方式一(简单查询):
  2. GET /people/_search
  3. 方式二:
  4. POST /people/_search
  5. {
  6. "query": {
  7. "match_all": {}
  8. }
  9. }
  10. GET people/man/_search?_source=name,country
  11. {
  12. "query": {
  13. "match": {
  14. "age": "25"
  15. }
  16. }
  17. }

排序

  1. POST /people/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "叶良辰"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "birthday": {
  11. "order": "desc"
  12. }
  13. }
  14. ]
  15. }

多字段搜索

  1. GET /people/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "页良",
  6. "fields": ["name","desc"]
  7. }
  8. }
  9. }

match_phrase:分词,并且要全部包含

  1. 完全匹配可能比较严,我们会希望有个可调节因子,少匹配一个也满足,那就需要使用到slop。
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "content" : {
  6. "query" : "我的宝马多少马力",
  7. "slop" : 1
  8. }
  9. }
  10. }
  11. }

query_string

  1. GET /people/_search
  2. {
  3. "query": {
  4. "query_string": {
  5. "default_field": "desc",
  6. "query": "(叶良辰 AND 风)"
  7. }
  8. }
  9. }
  10. “叶良辰 AND 风”会先把“叶良辰”拆分成“叶”、“良”和“辰”,然后后面必须有“风”
  11. GET /people/_search
  12. {
  13. "query": {
  14. "query_string": {
  15. "query": "(叶良辰 AND 火) OR (赵日天 AND 风)",
  16. "fields": ["name","desc"]
  17. }
  18. }
  19. }

term

  1. GET /people/_search
  2. {
  3. "query": {
  4. "term": {
  5. "name": "叶良辰"
  6. }
  7. }
  8. }

分页

  1. GET people/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "from": 0,
  7. "size": 1
  8. }

范围查询

数字

  1. GET /people/_search
  2. {
  3. "query": {
  4. "range": {
  5. "age": {
  6. "gte": 10,
  7. "lte": 26
  8. }
  9. }
  10. }
  11. }

日期

  1. GET /people/_search
  2. {
  3. "query": {
  4. "range": {
  5. "birthday": {
  6. "gte": "1993-01-01",
  7. "lte": "now"
  8. }
  9. }
  10. }
  11. }

过滤查询

过滤很快,但是没有评分,后面的boost就是默认的分数。

  1. POST /people/man/_search
  2. {
  3. "query": {
  4. "constant_score": {
  5. "filter": {
  6. "range": {
  7. "age": {
  8. "gte": 20,
  9. "lte": 30
  10. }
  11. }
  12. },
  13. "boost": 1.2
  14. }
  15. }
  16. }

也可以使用bool must 过滤

  1. GET /city/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {"match_all": {}}
  7. ],
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "1000km",
  11. "location": {
  12. "lat": 23.73,
  13. "lon": 112.1
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

布尔查询

shoud 相当于 or
must 相当于and

  1. POST /people/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. {
  7. "match": {
  8. "name": "叶良辰"
  9. }
  10. },
  11. {
  12. "match": {
  13. "desc": "赵日天"
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }
  1. POST /people/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "name": "叶良辰"
  9. }
  10. },
  11. {
  12. "match": {
  13. "desc": "赵日天"
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

高亮

  1. GET /people/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "叶良辰2"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "name": {}
  11. }
  12. }
  13. }

效果:

image.png

当前,也可以指定标签(虽然感觉没啥用)

  1. GET people/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "叶良辰"
  6. }
  7. },
  8. "highlight": {
  9. "pre_tags": ["<b>"],
  10. "post_tags": ["</b>"],
  11. "fields": {
  12. "name": {}
  13. }
  14. }
  15. }

聚合

根据字段类型查询

  1. GET /people/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "叶良辰"
  6. }
  7. },
  8. "aggs": {
  9. "groupByAge": {
  10. "terms": {
  11. "field": "age",
  12. "size": 10
  13. }
  14. }
  15. },
  16. "size": 0
  17. }
  18. >>>
  19. {
  20. …………………………………………………………………………
  21. "aggregations": {
  22. "groupByAge": {
  23. "doc_count_error_upper_bound": 0,
  24. "sum_other_doc_count": 0,
  25. "buckets": [
  26. {
  27. "key": 25,
  28. "doc_count": 5
  29. },
  30. {
  31. "key": 26,
  32. "doc_count": 1
  33. }
  34. ]
  35. }
  36. }
  37. }

统计:求和、平均值

  1. GET /people/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "叶良辰"
  6. }
  7. },
  8. "aggs": {
  9. "groupByAge": {
  10. "stats": { // 可以替换为min等
  11. "field": "age"
  12. }
  13. }
  14. },
  15. "size": 0
  16. }
  17. 》》》
  18. "aggregations": {
  19. "groupByAge": {
  20. "count": 6,
  21. "min": 25,
  22. "max": 26,
  23. "avg": 25.166666666666668,
  24. "sum": 151
  25. }
  26. }

先分组再计算

  1. GET /people/_search
  2. {
  3. "aggs": {
  4. "groupByAge": {
  5. "terms": {
  6. "field": "key"
  7. },
  8. "aggs": {
  9. "avg-age": {
  10. "avg": {
  11. "field": "age"
  12. }
  13. }
  14. }
  15. }
  16. },
  17. "size": 0
  18. }
  19. 》》》
  20. "aggregations": {
  21. "groupByAge": {
  22. "doc_count_error_upper_bound": 0,
  23. "sum_other_doc_count": 0,
  24. "buckets": [
  25. {
  26. "key": "叶良辰",
  27. "doc_count": 1,
  28. "avg-age": {
  29. "value": 26
  30. }
  31. }
  32. ]
  33. }
  34. }
  35. 提示:
  36. group字段必须没有分词,并且设置fielddata,如下,否则会报错(不一定)
  37. POST /people/_mapping/man
  38. {
  39. "properties": {
  40. "name":{
  41. "type": "text",
  42. "fielddata": true
  43. }
  44. }
  45. }

深度排序

实时查询:使用search-after

第一步正常查询,但是排序的时候,多加一个字段_uid:

  1. POST /people/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "叶良辰"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "birthday": {
  11. "order": "desc"
  12. },
  13. "_uid":{
  14. "order": "desc"
  15. }
  16. }
  17. ]
  18. }
  19. 》》》
  20. "_index": "people",
  21. "_type": "man",
  22. "_id": "AW7P7ag_3ydeaETTj2C3",
  23. "_score": null,
  24. "_source": {
  25. "name": "叶良辰",
  26. "country": "china",
  27. "age": 25,
  28. "birthday": "1993-01-01",
  29. "desc": "叶良辰,本地人,狂妄自大,惹了他,会有一百种方法让你待不下
  30. 日天是好基友,两个人风风火火闯九州"
  31. },
  32. "sort": [
  33. 725846400000,
  34. "man#AW7P7ag_3ydeaETTj2C3"
  35. ]
  36. },

返回的结果里面会sort的值,

第二步查询的时候加上第一次返回的最后的值,然后就OK啦

  1. POST /people/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "叶良辰"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "birthday": {
  11. "order": "desc"
  12. },
  13. "_uid":{
  14. "order": "desc"
  15. }
  16. }
  17. ],
  18. "search_after": [725846400000, "man#AW7QFn6r3ydeaETTj2C8"]
  19. }

后台任务可以使用scroll
**
相当于获取一个镜像,然后可以随意处理

第一步

  1. POST /people/_search?scroll=1m
  2. {
  3. "query": {
  4. "match": {
  5. "name": "叶良辰"
  6. }
  7. },
  8. "sort": "_doc",
  9. "size": 2
  10. }

获得scroll_id

image.png

第二步或者第N步(id不会变化)

  1. POST /_search/scroll
  2. {
  3. "scroll": "1m",
  4. "scroll_id":"DnF1ZXJ5VGhlbkZldGNoAwAAAAAACoDvFmpIaWxISVUxUUVDZUVpS1E4VXNaWmcAAAAAAAqA7hZqSGlsSElVMVFFQ2VFaUtROFVzWlpnAAAAAAAKgPAWakhpbEhJVTFRRUNlRWlLUThVc1paZw=="
  5. }

分词查询

  1. GET _analyze
  2. {
  3. "analyzer": "ik_smart",
  4. "text": ["今天天气不错"]
  5. }
  6. GET _analyze
  7. {
  8. "analyzer": "standard",
  9. "text": ["今天天气不错"]
  10. }

地理位置查询

设置索引

  1. PUT /city
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 0
  6. },
  7. "mappings": {
  8. "doc":{
  9. "properties":{
  10. "city":{
  11. "type":"text"
  12. },
  13. "state": {"type": "text"},
  14. "location":{"type":"geo_point"}
  15. }
  16. }
  17. }
  18. }

插入数据

  1. POST /city/doc
  2. {
  3. "city": "Guangzhou",
  4. "state": "GD",
  5. "location": {
  6. "lat": "23.16667",
  7. "lon": "113.23333"
  8. }
  9. }

查询

  1. GET /city/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {"match_all": {}}
  7. ],
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "1000km",
  11. "location": {
  12. "lat": 23.73,
  13. "lon": 112.1
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }