Elasticsearch 入门实战(3)--REST API 使用

打印 上一主题 下一主题

主题 930|帖子 930|积分 2790

本文主要介绍 Elasticsearch REST API 的使用,相关的环境及软件信息如下:CentOS 7.6.1810、Elasticsearch 8.2.2。
1、REST API 使用方法
  1. curl -X <VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' ‐d '<BODY>'
复制代码
说明:
参数说明
请求方法,如:GET,POST,PUT,HEAD 或 DELETE
协议,http 或 https
主机
Elasticsearch 服务端口,默认为9200
API 端点
请求参数
JSON 格式的请求体
2、Compact and aligned text (CAT) APIs

cat API 是提供给人在 Kibana 控制台或命令行中使用的,不适合应用程序调用。
2.1、查看集群健康状况
  1. curl -X GET "http://10.49.196.11:9200/_cat/health?v=true"
复制代码
2.2、查看集节点信息
  1. curl -X GET "http://10.49.196.11:9200/_cat/nodes?v=true"
复制代码
3、Index APIs

3.1、创建索引

同时设置了 setting 和 mapping 信息;setting 里面包含分片和副本信息,mapping 里包含字段设置的详细信息。
  1. curl -X PUT -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index' -d '
  2. {
  3.   "settings": {
  4.     "index": {
  5.       "number_of_shards": 2,
  6.       "number_of_replicas": 1
  7.       }
  8.   },
  9.   "mappings": {
  10.     "properties": {
  11.       "age": {
  12.         "type": "integer"
  13.       },
  14.       "name": {
  15.         "type": "keyword"
  16.       },
  17.       "poems": {
  18.         "type": "text",
  19.         "analyzer": "ik_max_word",
  20.         "search_analyzer": "ik_max_word"
  21.       },
  22.       "about": {
  23.         "type": "text",
  24.         "analyzer": "ik_max_word",
  25.         "search_analyzer": "ik_max_word"
  26.       },
  27.       "success": {
  28.         "type": "text",
  29.         "analyzer": "ik_max_word",
  30.         "search_analyzer": "ik_max_word"
  31.       }
  32.     }
  33.   }
  34. }'
复制代码
3.2、修改 _mapping 信息

字段可以新增,已有的字段只能修改字段的 search_analyze r属性。
  1. curl -X PUT -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index' -d '
  2. {
  3.   "properties": {
  4.     "name": {
  5.       "type": "text",
  6.       "analyzer": "ik_max_word",
  7.       "search_analyzer": "ik_max_word"
  8.     },
  9.     "age": {
  10.       "type": "integer"
  11.     },
  12.     "desc": {
  13.       "type": "text",
  14.       "analyzer": "ik_max_word",
  15.       "search_analyzer": "ik_smart"
  16.     }
  17.   }
  18. }'
复制代码
3.3、删除索引
  1. curl -X DELETE 'http://10.49.196.11:9200/poet-index'
复制代码
3.4、查询索引列表
  1. curl -X GET "http://10.49.196.11:9200/*"
复制代码
  1. curl -X GET "http://10.49.196.11:9200/_all"
复制代码
3.5、查询索引详情
  1. curl -X GET 'http://10.49.196.11:9200/poet-index'
复制代码
4、Document APIs

4.1、新增文档

A、设置 id 为 1
  1. curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_create/1' -d '
  2. {
  3.   "age": 30,
  4.   "name": "李白",
  5.   "poems": "静夜思",
  6.   "about": "字太白",
  7.   "success": "创造了古代浪漫主义文学高峰、歌行体和七绝达到后人难及的高度"
  8. }'
复制代码
B、不设置 id,将自动生成
  1. curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_doc' -d '
  2. {
  3.   "age": 31,
  4.   "name": "杜甫",
  5.   "poems": "登高",
  6.   "about": "字子美",
  7.   "success": "唐代伟大的现实主义文学作家,唐诗思想艺术的集大成者"
  8. }'
复制代码
C、批量新增文档
  1. curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_bulk' -d '
  2. {"index":{"_id":"11"}}
  3. {"age": 30,"name": "杜甫11","poems": "登高","about": "字子美","success": "唐代伟大的现实主义文学作家,唐诗思想艺术的集大成者"}
  4. {"index":{"_id":"12"}}
  5. {"age": 30,"name": "杜甫12","poems": "登高","about": "字子美","success": "唐代伟大的现实主义文学作家,唐诗思想艺术的集大成者"}
  6. '
复制代码
注:最后的空行是需要的,否则会报错。
4.2、删除文档
  1. curl -X DELETE 'http://10.49.196.11:9200/poet-index/_doc/1'
复制代码
4.3、更新文档

只更新参数设置的字段。
  1. curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_update/1' -d '
  2. {
  3.   "doc": {
  4.     "age": 32,
  5.     "poems": "望庐山瀑布"
  6.   }
  7. }'
复制代码
4.4、新增或覆盖文档

没有对应 id 的文档就创建,有就覆盖更新所有的字段(相当于先删除再新增)。
  1. curl -X PUT -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_doc/1' -d '
  2. {
  3.   "age": 31,
  4.   "name": "李白",
  5.   "poems": "静夜思",
  6.   "about": "字太白"
  7. }'
复制代码
5、Search APIs

5.1、查询一个索引的所有文档
  1. curl -X GET 'http://10.49.196.11:9200/poet-index/_search'
复制代码
5.2、根据 id 查询文档
  1. curl -X GET 'http://10.49.196.11:9200/poet-index/_doc/1'
复制代码
5.3、term 查询

term 查询不会对输入的内容进行分词处理,而是作为一个整体来查询。
A、查询单个词
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "term": {
  5.       "name": {
  6.         "value": "李白"
  7.       }
  8.     }
  9.   }
  10. }'
复制代码
B、查询多个词
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "terms": {
  5.       "name": ["李白", "杜甫"]
  6.     }
  7.   }
  8. }'
复制代码
5.4、range 查询
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "range": {
  5.       "age": {
  6.         "gte": 20,
  7.         "lte": 35
  8.       }
  9.     }
  10.   }
  11. }'
复制代码
5.5、全文查询

5.5.1、match

对输入的内容进行分词处理,再根据分词查询。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match": {
  5.       "success": "理想主义"
  6.     }
  7.   },
  8.   "from": 0,
  9.   "size": 10,
  10.   "sort": [{
  11.     "name": {
  12.       "order": "asc"
  13.     }
  14.   }]
  15. }'
复制代码
5.5.2、multi_match

多字段匹配。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "multi_match": {
  5.       "query": "太白",
  6.       "fields": ["about", "success"]
  7.     }
  8.   }
  9. }'
复制代码
5.5.3、match_phrase

匹配整个查询字符串。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match_phrase": {
  5.       "success": "文学作家"
  6.     }
  7.   }
  8. }'
复制代码
5.5.4、match_all

查询所有数据。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match_all": {
  5.      }
  6.   }
  7. }'
复制代码
5.5.5、query_string

query_string 可以同时实现前面几种查询方法。
A、类似 match
  1. curl -X GET  -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "default_field": "success",
  6.       "query": "古典文学"
  7.     }
  8.   }
  9. }'
复制代码
B、类似 mulit_match
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "query": "古典文学",
  6.       "fields": ["about", "success"]
  7.     }
  8.   }
  9. }'
复制代码
C、类似 match_phrase
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "default_field": "success",
  6.       "query": ""古典文学""
  7.     }
  8.   }
  9. }'
复制代码
D、带运算符查询,运算符两边的词不再分词
1、查询同时包含 ”文学“ 和 ”伟大“ 的文档
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "default_field": "success",
  6.       "query": "文学 AND 伟大"
  7.     }
  8.   }
  9. }'
复制代码
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "fields": ["success"],
  6.       "query": "文学 伟大",
  7.       "default_operator": "AND"
  8.     }
  9.   }
  10. }'
复制代码
2、查询 name 或 success 字段包含"文学"和"伟大"这两个单词,或者包含"李白"这个单词的文档。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "query": "(文学 AND 伟大) OR 李白",
  6.       "fields": ["name", "success"]
  7.     }
  8.   }
  9. }'
复制代码
5.5.6、simple_query_string

类似 query_string,主要区别如下:
1、不支持AND OR NOT ,会当做字符处理;使用 + 代替 AND,| 代替OR,- 代替 NOT
2、会忽略错误的语法
查询同时包含 ”文学“ 和 ”伟大“ 的文档:
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "simple_query_string": {
  5.       "fields": ["success"],
  6.       "query": "文学 + 伟大"
  7.     }
  8.   }
  9. }'
复制代码
5.6、模糊查询

模糊查询时使用的参数:
fuzziness
允许的最大编辑距离,默认不开启模糊查询,相当于 fuzziness=0。支持的格式
1、可以是数字(0、1、2)代表固定的最大编辑距离
2、自动模式,AUTO:[low],[high]
    查询词长度在 [0-low)范围内编辑距离为 0(即强匹配)
    查询词长度在 [low, high) 范围内允许编辑 1 次
    查询词长度 >high 允许编辑 2 次
prefix_length
控制两个字符串匹配的最小相同的前缀大小,也就是前 n 个字符不允许编辑,必须与查询词相同,默认是 0,大于 0 时可以显著提升查询性能
max_expansions
产生的最大模糊选项
transpositions
相邻位置字符互换是否算作 1 次编辑距离,全文查询不支持该参数
A、全文查询时使用模糊参数
先分词再计算模糊选项。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match": {
  5.         "success": {
  6.          "query": "古典文化",
  7.         "fuzziness": 1,
  8.         "prefix_length": 0,
  9.         "max_expansions": 5
  10.         }
  11.     }
  12.   }
  13. }'
复制代码
B、使用 fuzzy query
对输入不分词,直接计算模糊选项。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "fuzzy": {
  5.       "success": {
  6.         "value": "理想",
  7.         "fuzziness": 1,
  8.         "prefix_length": 0,
  9.         "transpositions": true
  10.       }
  11.     }
  12.   }
  13. }'
复制代码
5.7、组合查询

组合查询使用 bool 来组合多个查询条件。
条件说明
must同时满足
should满足其中任意一个
must_not同时不满足
filter过滤搜索,不计算得分
A、查询 success 包含 “思想” 且 age 在 [20-40] 之间的文档:
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "must": [{
  6.         "simple_query_string": {
  7.           "query": "思想",
  8.           "fields": ["success"]
  9.         }
  10.       }, {
  11.         "range": {
  12.           "age": {
  13.             "gte": 20,
  14.             "lte": 40
  15.           }
  16.         }
  17.       }]
  18.     }
  19.   }
  20. }'
复制代码
B、过滤出 success 包含 “思想” 且 age 在 [20-40] 之间的文档,不计算得分:
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "filter": [{
  6.         "simple_query_string": {
  7.           "query": "思想",
  8.           "fields": ["success"]
  9.         }
  10.       }, {
  11.         "range": {
  12.           "age": {
  13.             "gte": 20,
  14.             "lte": 40
  15.           }
  16.         }
  17.       }]
  18.     }
  19.   }
  20. }'
复制代码
5.8、聚合查询

A、求和
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "age_sum": {
  5.       "sum": {
  6.         "field": "age"
  7.       }
  8.     }
  9.   }
  10. }'
复制代码
B、类似 select count distinct(age) from poet-index
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/test-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "age_count": {
  5.       "cardinality": {
  6.         "field": "age"
  7.       }
  8.     }
  9.   }
  10. }'
复制代码
C、数量、最大、最小、平均、求和
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "age_stats": {
  5.       "stats": {
  6.         "field": "age"
  7.       }
  8.     }
  9.   },
  10.   "size": 0
  11. }'
复制代码
D、类似 select name,count(*) from poet-index group by name
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "name_terms": {
  5.       "terms": {
  6.         "field": "name"
  7.       }
  8.     }
  9.   },
  10.   "size": 0
  11. }'
复制代码
E、类似 select name,age, count(*) from poet-index group by name,age
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "name_terms": {
  5.       "terms": {
  6.         "field": "name"
  7.       },
  8.       "aggs": {
  9.         "age_terms": {
  10.           "terms": {
  11.             "field": "age"
  12.           }
  13.         }
  14.       }
  15.     }
  16.   },
  17.   "size": 0
  18. }'
复制代码
F、类似 select avg(age) from poet-indexwhere name='李白'
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "filter": {
  6.         "term": {
  7.           "name": "李白"
  8.         }
  9.       }
  10.     }
  11.   },
  12.   "aggs": {
  13.     "age_avg": {
  14.       "avg": {
  15.         "field": "age"
  16.       }
  17.     }
  18.   },
  19.   "size": 0
  20. }'
复制代码
5.9、推荐搜索

如果希望 Elasticsearch 能够根据我们的搜索内容给一些推荐的搜索选项,可以使用推荐搜索。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "suggest": {
  4.     "success_suggest": {
  5.       "text": "思考",
  6.       "term": {
  7.         "field": "success",
  8.         "analyzer": "ik_max_word",
  9.         "suggest_mode": "always",
  10.         "min_word_length":2
  11.       }
  12.     }
  13.   }
  14. }'
复制代码
推荐模式 suggest_mode:
 推荐模式说明
popular推荐词频更高的一些搜索
missing当没有要搜索的结果的时候才推荐
always无论什么情况下都进行推荐
5.10、高亮显示

对搜索结果中的关键字高亮显示。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match": {
  5.       "success": "思想"
  6.     }
  7.   },
  8.   "highlight": {
  9.     "pre_tags": "<span color='red'>",  
  10.     "post_tags": "</span>",        
  11.     "fields": {            
  12.       "success": {}
  13.     }
  14.   }
  15. }'
复制代码
5.11、SQL 查询

Elasticsearch 支持通过 SQL 查询数据。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/_sql' -d '
  2. {
  3.   "query": "SELECT * FROM "poet-index" limit 3"
  4. }'
复制代码
 
详细的 Elasticsearch REST API 使用说明,请参考官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

汕尾海湾

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表