java 連接ES
<!--創(chuàng)建maven工程-->
<!--1.elasticsearch-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6..5.4</version>
</dependency>
<!--2.elasticsearch 高級(jí)API-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.5.4</version>
</dependency>
<!--3.junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--4.lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
創(chuàng)建client鏈接
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(){
// 創(chuàng)建 HttpHost
HttpHost httpHost = new HttpHost("127.0.0.1", 9200);
// 創(chuàng)建 RestClientBuilder
RestClientBuilder builder = RestClient.builder(httpHost);
// 創(chuàng)建 RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
創(chuàng)建索引
import com.utils.EsClient;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;
public class Demo2 {
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void createIndx() throws Exception{
// 1.準(zhǔn)備關(guān)于索引的setting
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 2)
.put("number_of_replicas", 1);
// 2.準(zhǔn)備關(guān)于索引的mapping
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();
// 3.將settings和mappings 封裝到到一個(gè)Request對(duì)象中
CreateIndexRequest request = new CreateIndexRequest(index)
.settings(settings)
.mapping(type, mappings);
// 4.使用client 去連接ES
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("response:" + response.toString());
}
}
檢查索引是否存在法精,刪除索引
檢查索引存在
import com.utils.EsClient;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;
import java.io.IOException;
public class Demo2 {
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void existTest() throws IOException {
// 1.準(zhǔn)備request 對(duì)象
GetIndexRequest request = new GetIndexRequest(index);
// 2.通過(guò)client 去操作
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
// 3.輸出結(jié)果
System.out.println(exists);
}
}
刪除索引
import com.utils.EsClient;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;
import java.io.IOException;
public class Demo2 {
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void testDelete() throws IOException {
// 1.獲取request
DeleteIndexRequest request = new DeleteIndexRequest(index);
// 2.使用client 操作request
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
// 3.輸出結(jié)果
System.out.println(delete.isAcknowledged());
}
}
Java操作文檔
添加文檔操作
public class Demo3 {
ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void createDocTest() throws IOException {
// 1.準(zhǔn)備一個(gè)json數(shù)據(jù)
Person person = new Person(1,"張三",33,new Date());
String json = mapper.writeValueAsString(person);
// 2.創(chuàng)建一個(gè)request對(duì)象(手動(dòng)指定的方式創(chuàng)建)
IndexRequest request = new IndexRequest(index,type,person.getId().toString());
request.source(json, XContentType.JSON);
// 3.使用client 操作request對(duì)象生成doc
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
// 4.輸出返回結(jié)果
System.out.println(response.getResult().toString());
}
}
修改文檔
public class Demo3 {
ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void updateDocTest() throws Exception{
// 1.創(chuàng)建要跟新的Map
Map<String,Object> doc = new HashMap<>();
doc.put("name","張三三");
// 2.創(chuàng)建request, 將doc 封裝進(jìn)去
UpdateRequest request = new UpdateRequest(index,type,"1");
request.doc(doc);
// 3. client 去操作 request
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
// 4.輸出 更新結(jié)果
System.out.println(response.getResult());
}
}
刪除文檔
public class Demo3 {
ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type="man";
@Test
public void deleteDocTest() throws Exception{
// 1.封裝刪除對(duì)象
DeleteRequest request = new DeleteRequest(index,type,"1");
// 2 client 操作 request對(duì)象
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
// 3.輸出結(jié)果
System.out.println(response.getResult().toString());
}
}
java批量操作文檔
批量新增
@Test
public void bulkCreateDoc() throws Exception{
// 1.準(zhǔn)備多個(gè)json 對(duì)象
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);
// 2.創(chuàng)建request
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));
// 3.client 執(zhí)行
BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
// 4.輸出結(jié)果
System.out.println(responses.getItems().toString());
}
批量刪除
public void bulkDelete() throws Exception{
// 1.創(chuàng)建Request 對(duì)象
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"));
// 2.執(zhí)行
BulkResponse re = client.bulk(bulkRequest, RequestOptions.DEFAULT);
// 3.輸出結(jié)果
System.out.println(re.toString());
}