Springboot 系列 (20) - Springboot+ElasticSearch 实现全文搜索(二)| 使用 curl 调用 ElasticSearch API
在 “Springboot 系列 (19) - Springboot+ElasticSearch 实现全文搜索(一)” 里我们演示了安装配置 ElasticSearch (包括 Kibana 和 Logstash),本文将演示使用 curl 调用 ElasticSearch API。
ElasticSearch 提供了多种交互使用方式,包括 Java API 和 RESTful API ,本文主要介绍 RESTful API。使用 RESTful API 通过端口 9200 与 ElasticSearch 进行通信,我们可以使用 Postman 或 curl 命令来与 ElasticSearch 交互。
REST(Representational State Transfer,表述性状态转移)是一组架构约束条件和原则,而满足这些约束条件和原则的应用程序或设计就是 RESTful,其本质就是一种定义接口的规范。RESTful API 的特点:
(1) 基于 HTTP/HTTPS;
(2) 使用 XML 或 JSON 的格式定义;
(3) 每一个 URI 代表一种资源;
(4) 客户端使用 GET、POST、PUT、DELETE 这 4 种表示操作方式的动词对服务端资源进行操作:
GET:获取资源
POST:新建资源(也可以更新资源)
PUT:更新资源
DELETE:删除资源
1. 配置 ElasticSearch
1) 运行环境
操作系统:Ubuntu 20.04
内网 IP:192.168.1.3
ElasticSearch 版本:7.10.2
ElasticSearch 所在路径:~/apps/elasticsearch-7.10.2/
ElasticSearch 端口: 9200
ElasticSearch 账户(用户名/密码):elastic / 123456
2) 启动
$ cd ~/apps/elasticsearch-7.10.2/bin
$ ./elasticsearch
... ERROR: [2] bootstrap checks failed [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] [2]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured ...
注:启动失败,两个问题:1. jvm 内存不够大;2. 集群节点最低配置要求;
# 增加 jvm 内存到 262144
$ sudo sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144
# 修改 elasticsearch.yml 文件,添加如下内容
$ vim ~/apps/elasticsearch-7.10.2/config/elasticsearch.yml
# 单机模式
discovery.type: single-node
# 再次启动
$ ./elasticsearch
2. Index(索引)的操作
1) 创建一个 Index
语法如下:
PUT /spring
命令行:
$ curl -X PUT "http://192.168.1.3:9200/spring" --basic -u elastic:123456
{"acknowledged":true,"shards_acknowledged":true,"index":"spring"}
2) 查看 Index
语法如下:
GET /spring
命令行:
$ curl -X GET "http://192.168.1.3:9200/spring" --basic -u elastic:123456
{"spring":{"aliases":{},"mappings":{},"settings":{"index":{"routing":{"allocation":{"include":{"_tier_preference":"data_content"}}},"number_of_shards":"1","provided_name":"spring","creation_date":"1669727877158","number_of_replicas":"1","uuid":"Rkb56yXLTqOJL4iG90LsMQ","version":{"created":"7100299"}}}}}
3) 删除 Index
语法如下:
DELETE /spring
命令行:
$ curl -X DELETE "http://192.168.1.3:9200/spring" --basic -u elastic:123456
{"acknowledged":true}
# 查看 /spring 索引
$ curl --basic -u elastic:123456 -X GET http://192.168.1.3:9200/spring
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index [spring]","resource.type":"index_or_alias","resource.id":"spring","index_uuid":"_na_","index":"spring"}],"type":"index_not_found_exception","reason":"no such index [spring]","resource.type":"index_or_alias","resource.id":"spring","index_uuid":"_na_","index":"spring"},"status":404}
4) 创建 Index 并指定 settings
语法如下:
PUT /spring { "settings": { # 分片数 "number_of_shards": 5, # 备份数 "number_of_replicas": 1 } }
命令行:
$ curl -X PUT "http://192.168.1.3:9200/spring" --basic -u elastic:123456 -H "Content-Type:application/json" --data '{"settings":{"number_of_shards":5,"number_of_replicas":1}}'
{"acknowledged":true,"shards_acknowledged":true,"index":"spring"}
注:Windows 下运行 curl 命令时,--data 参数要写成如下格式:
"{"""settings""":{"""number_of_shards""":5,"""number_of_replicas""":1}}"
# 查看 /spring 索引
$ curl -X GET "http://192.168.1.3:9200/spring" --basic -u elastic:123456
{"spring":{"aliases":{},"mappings":{},"settings":{"index":{"routing":{"allocation":{"include":{"_tier_preference":"data_content"}}},"number_of_shards":"5","provided_name":"spring","creation_date":"1669729181165","number_of_replicas":"1","uuid":"I-ExJI8mTNS6ZKaEG_EXDw","version":{"created":"7100299"}}}}}
# 查看 /spring 索引的 mapping(创建索引时未指定 mapping,此时为空值)
$ curl -X GET "http://192.168.1.3:9200/spring/_mapping" --basic -u elastic:123456
{"spring":{"mappings":{}}}
注:从 Elasticsearch 7 开始默认不支持指定索引类型,默认索引类型是 _doc,如果想改变,则配置include_type_name: true 即可。官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html
3. Document(文档)的操作
1) 创建一个 Document
创建文档(自动生成 _id),语法如下:
POST /spring/_doc { "name": "Elastic Search", "author": "Elastic Company", "count": 3 }
命令行:
$ curl -X POST "http://192.168.1.3:9200/spring/_doc" --basic -u elastic:123456 -H "Content-Type:application/json" --data '{"name":"Elastic Search","author":"Elastic Company","count":3}'
{"_index":"spring","_type":"_doc","_id":"OZhr4YQBeWfeBrcFuThE","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
# 查看 /spring 索引的 mapping (创建文档后,自动创建了 mapping)
$ curl -X GET "http://192.168.1.3:9200/spring/_mapping" --basic -u elastic:123456
{"spring":{"mappings":{"properties":{"author":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"count":{"type":"long"},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}}}
创建文档(手动指定 _id),语法如下:
POST /spring/_doc/2 { "name": "Elastic Search 2", "author": "Elastic Company 2", "count": 8 }
命令行:
$ curl -X POST "http://192.168.1.3:9200/spring/_doc/2" --basic -u elastic:123456 -H "Content-Type:application/json" --data '{"name":"Elastic Search 2","author":"Elastic Company 2","count":8}'
{"_index":"spring","_type":"_doc","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
2) 查看 Document
语法如下:
GET /spring/_doc/OZhr4YQBeWfeBrcFuThE
命令行:
$ curl -X GET "http://192.168.1.3:9200/spring/_doc/OZhr4YQBeWfeBrcFuThE" --basic -u elastic:123456
{"_index":"spring","_type":"_doc","_id":"OZhr4YQBeWfeBrcFuThE","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{"name":"Elastic Search","author":"Elastic Company","count":3}}
3) 修改 Document
覆盖式修改,语法如下:
PUT /spring/_doc/OZhr4YQBeWfeBrcFuThE { "name": "Elastic Search - overwrite", "author": "Elastic Company - overwrite", "count": 5 }
命令行:
$ curl -X PUT "http://192.168.1.3:9200/spring/_doc/OZhr4YQBeWfeBrcFuThE" --basic -u elastic:123456 -H "Content-Type:application/json" --data '{"name":"Elastic Search - overwrite","author":"Elastic Company - overwrite","count":5}'
基于 doc 方式修改,语法如下:
POST /spring/_doc/2/_update { "doc": { "count": 99 } }
命令行:
$ curl -X POST "http://192.168.1.3:9200/spring/_doc/2/_update" --basic -u elastic:123456 -H "Content-Type:application/json" --data '{"doc":{"count": 99}}'
{"_index":"spring","_type":"_doc","_id":"2","_version":5,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":4,"_primary_term":1}
4) 删除 Document
语法如下:
DELETE /spring/_doc/OZhr4YQBeWfeBrcFuThE
命令行:
$ curl -X DELETE "http://192.168.1.3:9200/spring/_doc/OZhr4YQBeWfeBrcFuThE" --basic -u elastic:123456
{"_index":"spring","_type":"_doc","_id":"OZhr4YQBeWfeBrcFuThE","_version":3,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}
# 查看被删除的文档
$ curl -X GET "http://192.168.1.3:9200/spring/_doc/OZhr4YQBeWfeBrcFuThE" --basic -u elastic:123456
{"_index":"spring","_type":"_doc","_id":"OZhr4YQBeWfeBrcFuThE","found":false}
4. 查询操作
1) 查询全部文档 match_all
语法如下:
GET /spring/_search { "query": { "match_all": {} } }
命令行:
$ curl -X GET "http://192.168.1.3:9200/spring/_search" --basic -u elastic:123456 -H "Content-Type:application/json" --data '{"query":{"match_all":{}}}'
{"took":362,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"spring","_type":"_doc","_id":"OpiW4YQBeWfeBrcFjTgB","_score":1.0,"_source":{"name":"Elastic Search","author":"Elastic Company","count":3}},{"_index":"spring","_type":"_doc","_id":"2","_score":1.0,"_source":{"name":"Elastic Search 2","author":"Elastic Company 2","count":8}},{"_index":"spring","_type":"_doc","_id":"O5ip4YQBeWfeBrcFZjj5","_score":1.0,"_source":{"name":"Elastic Search - Moidfy","author":"Elastic Company - Modify","count":5}}]}}
2) 模糊查询 match_phrase_prefix
语法如下:
GET /spring/_search { "query": { "match_phrase_prefix": { "name": "Elastic Search" } } }
命令行:
$ curl -X GET "http://192.168.1.3:9200/spring/_search" --basic -u elastic:123456 -H "Content-Type:application/json" --data '{"query":{"match_phrase_prefix":{"name":"Elastic Search"}}}'
{"took":18,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":0.5753642,"hits":[{"_index":"spring","_type":"_doc","_id":"OpiW4YQBeWfeBrcFjTgB","_score":0.5753642,"_source":{"name":"Elastic Search","author":"Elastic Company","count":3}},{"_index":"spring","_type":"_doc","_id":"2","_score":0.36464313,"_source":{"name":"Elastic Search 2","author":"Elastic Company 2","count":8}},{"_index":"spring","_type":"_doc","_id":"O5ip4YQBeWfeBrcFZjj5","_score":0.36464313,"_source":{"name":"Elastic Search - Moidfy","author":"Elastic Company - Modify","count":5}}]}}
3) 精准查询 term
语法如下:
GET /spring/_search { "query": { "term": { "name": "Elastic Search" } } }
命令行:
$ curl -X GET "http://192.168.1.3:9200/spring/_search" --basic -u elastic:123456 -H "Content-Type:application/json" --data '{"query":{"term":{"name":"Elastic Search"}}}'
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"max_score":null,"hits":[]}}
注:精准查询 term 没有查到结果。这是因为创建文档时自动创建的 mapping 中 name 字段格式如下:
"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}
"text" 字段会被自动分词,分词列表里不好含 "Elastic Search"。要得到精准查询结果有两种方法:
方法1:查询 name 字段的子字段 "keyword",格式如下:
"query": { "term": { "name.keyword": "Elastic Search" } }
方法2:创建索引,并指定 mapping 自定义 name 字段,格式如下:
PUT /spring PUT /spring/_mapping { "properties": { "name":{ "type": "keyword", "ignore_above": 256 }, "author":{ "type": "keyword", "ignore_above": 256 }, "count":{ "type": "long" } } }