maven 依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.10</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.8.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
客户端
package com.utils;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
public class EsClient {
public static RestHighLevelClient getClient(){
HttpHost httpHost = new HttpHost("127.0.0.1",9200);
RestClientBuilder builder = RestClient.builder(httpHost);
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
索引操作
public class CreateInx{
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void createIndx() throws Exception{
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 2)
.put("number_of_replicas", 1);
XContentBuilder mappings = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("name")
.field("type", "text")
.endObject()
.startObject("age")
.field("type", "integer")
.endObject()
.startObject("birthday")
.field("type", "date")
.field("format", "yyyy-MM-dd")
.endObject()
.endObject()
.endObject();
CreateIndexRequest request = new CreateIndexRequest(index)
.settings(settings)
.mapping(type,mappings);
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("response:"+response.toString());
}
}
public class CheckIdxExit {
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void existTest() throws IOException {
GetIndexRequest request = new GetIndexRequest(index);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
}
public class DeleteIdx {
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void testDelete() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(index);
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
}
文档操作
public class CreateDoc {
ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void createDoc() throws IOException {
Person person = new Person(1,"张三",33,new Date());
String json = mapper.writeValueAsString(person);
IndexRequest request = new IndexRequest(index,type,person.getId().toString());
request.source(json, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult().toString());
}
@Test
public void bulkCreateDoc() throws Exception{
Person p1 = new Person(1,"张三",23,new Date());
Person p2 = new Person(2,"里斯",24,new Date());
Person p3 = new Person(3,"王武",24,new Date());
String json1 = mapper.writeValueAsString(p1);
String json2 = mapper.writeValueAsString(p2);
String json3 = mapper.writeValueAsString(p3);
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest(index,type,p1.getId().toString()).source(json1,XContentType.JSON))
.add(new IndexRequest(index,type,p2.getId().toString()).source(json2,XContentType.JSON))
.add(new IndexRequest(index,type,p3.getId().toString()).source(json3,XContentType.JSON));
BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(responses.getItems().toString());
}
}
public class UpdateDoc {
ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void updateDocTest() throws Exception{
Map<String,Object> doc = new HashMap<>();
doc.put("name","张三三");
UpdateRequest request = new UpdateRequest(index,type,"1");
request.doc(doc);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
}
public class DeleteDoc {
ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void deleteDocTest() throws Exception{
DeleteRequest request = new DeleteRequest(index,type,"1");
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.getResult().toString());
}
@Test
public void bulkDelete() throws Exception {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new DeleteRequest(index, type, "1"));
bulkRequest.add(new DeleteRequest(index, type, "2"));
bulkRequest.add(new DeleteRequest(index, type, "3"));
BulkResponse re = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(re.toString());
}
}