Elasticsearch API的使用

介绍:

Elasticsearch的通信方式有很多种,其中restful API是核心,通过它可以使用多种语言与elasticsearch建立通信。

因为Elasticsearch是用java写的,所以官网介绍了很多种关于java的API。本文采用的是高水平客户端(因官网介绍

计划在Elasticsearch7.0中弃用TransportClient,并在8.0中完全删除它。相反,使用Java High Level
REST客户端,执行HTTP请求而不是序列化的Java请求)

HTTP API总结(restful API)

1.管理Elasticsearch索引:
基于CentOS7:

ip:192.168.223.10(非远程访问可用localhost)

集群健康状态查看:

curl -XGET http://localhost:9200/_cat/health?v

注释:curl -X<command>是指定与服务器的请求方法
?<parameter> v是指格式

获取节点信息:

curl -XGET http://localhost:9200/_cat/nodes?v

返回所有索引信息:

curl -XGET http://localhost:9200/_cat/indices?v

创建索引:(无参)

curl -XPUT http://localhost:9200/database?pretty
注释:?<parameter> pretty是指返回漂亮的json格式的数据 

创建索引:(有参数)

 curl -H "Content-Type:application/json" -XPUT http://localhost:9200/database/table/1?pretty -d '{
"name":"Ann"
}'
注释:	-H "xxx" 是请求头,ES在6.x之后明确要求json文档格式
	    -d '{json数据}' 传递参数

查询索引:

curl -XGET http://localhost:9200/database/table/1?pretty

删除索引:

curl -XDELETE http://localhost:9200/database?pretty

总结:Elastcsearch http请求格式(基于CentOS7)
curl -X<请求> http://ip///
请求:GET:查询 PUT:添加 POST:修改 DELETE:删除
2.修改数据
新建索引:

curl -H "Content-Type:application/json" -XPUT http://localhost:9200/database/table/1?pretty -d '{
		     "name":"Ann"
		     }'

修改索引:(修改参数再次执行请求,数据被修改)

curl -H "Content-Type:application/json" -XPUT http://localhost:9200/database/table/1?pretty -d '{
		     "name":"Bnn"
		     }'

修改文档:
修改名字,添加年龄

curl -H "Content-Type:application/json" -POST   http://localhost:9200/database/table/1/_updata?pretty -d '{
		     "doc":{"name":"Ann doc","age":"20"}
		     }' 

使用scripts修改

curl -H "Content-Type:application/json" -POST   http://localhost:9200/database/table/1/_updata?pretty -d '{
		     "script":"ctx._source.age+=5"
		     }'  

删除文档:

curl -XDELETE http://localhost:9200/database/table/1?pretty

批量处理:

curl -H "Content-Type:application/json" -POST   
http://localhost:9200/database/table/_bulk?pretty -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }		 
'
注释:_bulk API遵循严格的json格式,每两行为一组(除删除操作外)
导入数据:

json文档格式:

{"index":{"_id":"1"}}
{"name": "John Doe","passwd":"1234"}

文档搜索api(排序)

curl -XGET 'http://localhost:9200/registry/user/_search?pretty&q=*&sort=_id:asc'
注释:q=*是匹配所有文档
	       sort=_id:asc是返回结果排序,按照字段_id进行升序排列
	 
	 
curl -H "Content-Type:application/json"  -XGET 'localhost:9200/registry/user/_search?pretty' -d 
'{"query": { "match_all": {}}, 
		"from":2,
		"size":2,
		"sort":{"_id":"desc"} 
	}'
注意:"query": { "match_all": {}} 查询方式是匹配所有
	"from":2,from是指定从哪个文档开始,没有指定默认是0
	"size":2,返回结果
	"sort":{"_id":"desc"}按照字段_id进行降序序排列

curl -H "Content-Type:application/json"  -XGET 'localhost:9200/registry/user/_search?pretty' -d 
		   '{
			  "query": {
				"bool": {
				  "must": { "match_all": {} },
				  "filter": {
					"range": {
					  "balance": {
						"gte": 20000,
						"lte": 30000
					  }
					}
				  }
				}
			  }
			}'
注释:查询 20000<=balance<=30000范围

三、Java API总结

1.创建客户端:(高级客户端)(单节点)

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.223.10", 9200, "http")));

2.创建索引

IndexRequest request = new IndexRequest("posts","doc","1");   
		String json = "{" +
			        "\"user\":\"kimchy\"," +
			        "\"postDate\":\"2013-01-30\"," +
			        "\"message\":\"trying out Elasticsearch\"" + "}";

request.source(json,XContentType.JSON);
IndexResponse indexResponse = client.index(request,RequestOptions.DEFAULT);

3.查询文档内容

GetRequest getRequest = new GetRequest( "posts", "doc",  "1"); 
GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
 System.out.println(getResponse.getSourceAsMap().get("user"));

4.搜索排序

//创建搜索请求
SearchRequest searchRequest = new SearchRequest("registry");
searchRequest.types("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//降序
searchSourceBuilder.sort("_id",SortOrder.DESC);
//分页
searchSourceBuilder.from(0);
searchSourceBuilder.size(5);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse=client.search(searchRequest,RequestOptions.DEFAULT);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值