ES基本操作
package com.atguigu.es;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
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.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
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.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
public class ESTest {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http")
));
@Test
public void testCreateIndex() throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest("students");
createIndexRequest.mapping("{\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"keyword\",\n" +
" \"index\": true,\n" +
" \"store\": true\n" +
" },\n" +
" \"age\": {\n" +
" \"type\": \"integer\",\n" +
" \"index\": true,\n" +
" \"store\": true\n" +
" },\n" +
" \"remark\": {\n" +
" \"type\": \"text\",\n" +
" \"index\": true,\n" +
" \"store\": true,\n" +
" \"analyzer\": \"ik_max_word\",\n" +
" \"search_analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }", XContentType.JSON);
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("acknowledged = " + acknowledged);
}
@Test
public void testGetIndex() throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest("students");
GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);
Map<String, Settings> settings = getIndexResponse.getSettings();
System.out.println("settings = " + settings);
Map<String, MappingMetaData> mappings = getIndexResponse.getMappings();
System.out.println("mappings = " + mappings);
}
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("book");
AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
boolean acknowledged = acknowledgedResponse.isAcknowledged();
System.out.println("acknowledged = " + acknowledged);
}
@Test
public void testIndexDoc() throws IOException {
IndexRequest indexRequest = new IndexRequest("students");
IndexRequest id = indexRequest.id("1");
Student student = new Student("张三", 23, "三三三三三");
String studentJsonString = JSONObject.toJSONString(student);
IndexRequest source = indexRequest.source(studentJsonString, XContentType.JSON);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
DocWriteResponse.Result result = indexResponse.getResult();
System.out.println("result = " + result);
}
@Test
public void testGetDoc() throws IOException {
GetRequest getRequest = new GetRequest("students", "1");
GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
String sourceAsString = documentFields.getSourceAsString();
System.out.println("sourceAsString = " + sourceAsString);
}
@Test
public void testUpdateDoc() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("students", "1");
Student student = new Student();
student.setName("张小三");
UpdateRequest doc = updateRequest.doc(JSONObject.toJSONString(student), XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println("update = " + update);
}
@Test
public void testBulkDoc() {
BulkRequest bulkRequest = new BulkRequest();
Student student = new Student();
for (int i = 0; i < 10; i++) {
student.setAge(18 + i);
student.setName("robin" + i);
student.setRemark("good man " + i);
bulkRequest.add(new IndexRequest("students").id(String.valueOf(10 + i)).source(JSONObject.toJSONString(student), XContentType.JSON));
}
try {
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
for (BulkItemResponse itemResponse : response.getItems()) {
System.out.println(itemResponse.isFailed());
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void deleteDocument() {
DeleteRequest request = new DeleteRequest("students", "11");
try {
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
} catch (IOException e) {
e.printStackTrace();
}
}
}