api 类
package com.api;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ApiService {
@Autowired
RestHighLevelClient restHighLevelClient;
@SneakyThrows
public CreateIndexResponse createIndex(String index) {
CreateIndexRequest request = new CreateIndexRequest(index);
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
return response;
}
@SneakyThrows
public GetIndexResponse queryIndex(String index) {
GetIndexRequest request = new GetIndexRequest(index);
GetIndexResponse response = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
return response;
}
@SneakyThrows
public AcknowledgedResponse removeIndex(String index) {
DeleteIndexRequest request = new DeleteIndexRequest(index);
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
return response;
}
@SneakyThrows
public IndexResponse addDocument(String index, String id, Object o) {
IndexRequest indexRequest = new IndexRequest(index);
indexRequest.id(id);
indexRequest.timeout(TimeValue.timeValueSeconds(1));
indexRequest.source(JSON.toJSONString(o), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
return response;
}
@SneakyThrows
public DeleteResponse removeDocument(String index, String id) {
DeleteRequest deleteRequest = new DeleteRequest(index, id);
deleteRequest.id(id);
deleteRequest.timeout(TimeValue.timeValueSeconds(1));
DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
return response;
}
@SneakyThrows
public UpdateResponse updateDocument(String index, String id, Object o) {
UpdateRequest updateRequest = new UpdateRequest(index, id);
updateRequest.timeout(TimeValue.timeValueSeconds(1));
updateRequest.doc(JSON.toJSONString(o),XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
return response;
}
@SneakyThrows
public boolean existsDocument(String index, String id) {
GetRequest request = new GetRequest(index, id);
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean response = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
return response;
}
@SneakyThrows
public GetResponse queryDocument(String index, String id) {
GetRequest request = new GetRequest(index, id);
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
return response;
}
@SneakyThrows
public BulkResponse bulkDocument(String index, List<Object> list) {
BulkRequest bulkRequest = new BulkRequest(index);
bulkRequest.timeout(TimeValue.timeValueSeconds(10));
for (int i = 0; i < list.size(); i++) {
bulkRequest.add(new IndexRequest(index)
.source(JSON.toJSONString(list.get(i)),XContentType.JSON)
);
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
return bulk;
}
@SneakyThrows
public SearchResponse searchDocument(String index, String key,String value) {
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(0);
sourceBuilder.size(10);
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(key, value);
matchQueryBuilder.analyzer("ik_smart");
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.timeout(TimeValue.timeValueSeconds(100));
searchRequest.source(sourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
return search;
}
}
测试类
package com.es.api;
import com.alibaba.fastjson.JSON;
import com.api.ApiService;
import com.dao.Student;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
@SpringBootTest
class EsApiApplicationTests {
@Autowired
ApiService apiService;
@Test
void testCreateIndex() {
CreateIndexResponse indexResponse = apiService.createIndex("jd");
System.out.println(JSON.toJSONString(indexResponse));
}
@Test
void testQueryIndex() {
GetIndexResponse queryIndex = apiService.queryIndex("test");
System.out.println(queryIndex.getSettings());
}
@Test
void testDeleteIndex() {
AcknowledgedResponse removeIndex = apiService.removeIndex("test");
System.out.println(JSON.toJSONString(removeIndex));
}
@Test
void testAddDoc() {
Student student = new Student("张三",1);
IndexResponse addDocument = apiService.addDocument("test","1",student);
System.out.println(JSON.toJSONString(addDocument));
}
@Test
void testUpdateDoc() {
Student student = new Student("张三2",1);
UpdateResponse updateDocument = apiService.updateDocument("test","1",student);
System.out.println(JSON.toJSONString(updateDocument));
}
@Test
void existsUpdateDoc() {
Student student = new Student("张三2",1);
boolean existsDocument = apiService.existsDocument("test","1");
System.out.println(JSON.toJSONString(existsDocument));
}
@Test
void deleteUpdateDoc() {
DeleteResponse removeDocument = apiService.removeDocument("test","1");
System.out.println(JSON.toJSONString(removeDocument));
}
@Test
void queryUpdateDoc() {
GetResponse queryDocument = apiService.queryDocument("test","1");
System.out.println(queryDocument.getSourceAsString());
}
@Test
void seqrchUpdateDoc() {
SearchResponse queryDocument = apiService.searchDocument("test","name","张三");
SearchResponse queryDocument2 = apiService.searchDocument("test","name","张");
SearchResponse queryDocument3 = apiService.searchDocument("test","name","三");
System.out.println(queryDocument);
System.out.println(queryDocument2);
System.out.println(queryDocument3);
}
@Test
public void testBult()throws Throwable{
Student student = new Student("张三",1);
Student student2 = new Student("张三2",2);
ArrayList<Object> list = new ArrayList<>();
list.add(student);
list.add(student2);
BulkResponse bulkResponse = apiService.bulkDocument("test", list);
System.out.println(bulkResponse.hasFailures());
}
}