介绍
REST Client 有两个版本,一个是 Low Level,提供了比较简单的 HTTP 客户端功能。具体可参考 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.2/java-rest-low.html 。
另一个是 High Level Client,基于 Low level Client,做了一些封装,比较好用。具体文档见 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.2/java-rest-high.html。
本文使用 High Level Client 。
实战1
ES 版本 7.13.4
gradle 依赖:
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.13.4'
数据准备
# 创建索引
PUT student
{
"mappings" : {
"properties" : {
"uid": {
"type" : "integer"
},
"name" : {
"type" : "keyword"
},
"age" : {
"type" : "integer"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 10,
"number_of_replicas" : 1
}
}
}
# 插入数据
POST student/_doc/1?routing=1
{
"uid": 1,
"name": "张三",
"age": 10
}
# 插入数据
POST student/_doc/2?routing=2
{
"uid": 2,
"name": "张三",
"age": 10
}
查询数据:
# 请求
GET student/_search
{
"query": {
"match_all": {}
},
"explain": true
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_shard" : "[student][7]",
"_node" : "gejvpvFJTTK8LAnyvaI3_Q",
"_index" : "student",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_routing" : "2",
"_source" : {
"uid" : 2,
"name" : "张三",
"age" : 10
},
"_explanation" : {
"value" : 1.0,
"description" : "*:*",
"details" : [ ]
}
},
{
"_shard" : "[student][8]",
"_node" : "gejvpvFJTTK8LAnyvaI3_Q",
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"uid" : 1,
"name" : "张三",
"age" : 10
},
"_explanation" : {
"value" : 1.0,
"description" : "*:*",
"details" : [ ]
}
}
]
}
}
查询 _id 为 1 的数据
package esdemo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class TestClient {
public static void main(String[] args) throws IOException {
RestClientBuilder restClientBuilder = RestClient.builder(
new HttpHost("localhost", 9200, "http")
);
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
GetRequest getRequest = new GetRequest();
getRequest.index("student");
getRequest.routing("1");
getRequest.id("1");
System.out.println("请求:");
System.out.println(getRequest);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println("查询结果:");
System.out.println(getResponse);
client.close();
}
}
执行结果:
get [student][_doc][1]: routing [1]
查询结果:
{"_index":"student","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"_routing":"1","found":true,"_source":{"uid":1,"name":"张三","age":10}}
查询 _id 1 数据的详细信息
package esdemo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class TestClient {
public static void main(String[] args) throws IOException {
RestClientBuilder restClientBuilder = RestClient.builder(
new HttpHost("localhost", 9200, "http")
);
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
GetRequest getRequest = new GetRequest();
getRequest.index("student");
getRequest.routing("1");
getRequest.id("1");
System.out.println("请求:");
System.out.println(getRequest);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println("索引: " + getResponse.getIndex());
System.out.println("type: " + getResponse.getType());
System.out.println("id: " + getResponse.getId());
System.out.println("source: " + getResponse.getSourceAsString());
System.out.println("source.name: " + getResponse.getSourceAsMap().get("name"));
client.close();
}
}
执行结果:
请求:
get [student][_doc][1]: routing [1]
索引: student
type: _doc
id: 1
source: {"uid":1,"name":"张三","age":10}
source.name: 张三
基于 term 查询,结果有数据
package esdemo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class TestClient {
public static void main(String[] args) throws IOException {
RestClientBuilder restClientBuilder = RestClient.builder(
new HttpHost("localhost", 9200, "http")
);
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("student");
searchRequest.allowPartialSearchResults(false);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.timeout(TimeValue.timeValueMillis(1000));
searchSourceBuilder.query(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("name", "张三")));
searchRequest.source(searchSourceBuilder);
System.out.println("查询语句: ");
System.out.println(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("查询结果:");
System.out.println(searchResponse);
client.close();
}
}
执行结果:
查询语句: {
"timeout": "1000ms",
"query": {
"bool": {
"filter": [
{
"term": {
"name": {
"value": "张三",
"boost": 1.0
}
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
}
}
查询结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "student",
"_type": "_doc",
"_id": "2",
"_score": 0.0,
"_routing": "2",
"_source": {
"uid": 2,
"name": "张三",
"age": 10
}
},
{
"_index": "student",
"_type": "_doc",
"_id": "1",
"_score": 0.0,
"_routing": "1",
"_source": {
"uid": 1,
"name": "张三",
"age": 10
}
}
]
}
}