1、api种类
1.1 TransportClient
是Elasticsearch官方的api
TransportClient可以支持2.x,5.x版本,TransportClient将会在Elasticsearch 7.0弃用并在8.0中完成删除。
1.2 RestClient
也是Elasticsearch官方的api。
官方地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html
替代TransportClient,它使用HTTP请求而不是Java序列化请求。
1.3 Jest
Jest是Java社区开发的,是Elasticsearch的Java Http Rest客户端
1.4 Spring Data Elasticsearch
spring集成的Elasticsearch开发包
2、 RestClient-索引和文档操作
pom:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
java代码:
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
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.IndicesOptions;
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.CloseIndexRequest;
import org.elasticsearch.client.indices.CloseIndexResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RestHighLevelClientDemo {
private RestHighLevelClient client;
/**
* 创建客户端
*
* @return
*/
@Before
public void buildClient() {
client = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.1.1.5", 9200, "http")));
}
/**
* 创建索引
* @throws IOException
*/
@Test
public void test001createIndex() throws IOException {
deleteIndex();
CreateIndexRequest request = new CreateIndexRequest("person_index");//索引名不能有大写字母,必须全部小写,否则es无法创建索引
// 分片配置
request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));
// mapping配置
Map<String, Object> name = new HashMap<String, Object>();
name.put("type", "text");
Map<String, Object> age = new HashMap<String, Object>();
age.put("type", "text");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("name", name);
properties.put("age", age);
Map<String, Object> mapping = new HashMap<String, Object>();
mapping.put("properties", properties);
request.mapping(mapping);
// 为索引设置一个别名
request.alias(new Alias("person_alias"));
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(request.index()+"创建索引"+createIndexResponse.isAcknowledged());
}
/**
* 判断索引是否存在
* @throws IOException
*/
@Test
public void test002existsIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("person_index");
request.includeDefaults(true);
request.indicesOptions(IndicesOptions.lenientExpandOpen());
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
if (exists) {
System.out.println("索引存在");
} else {
System.out.println("索引不存在");
}
assertTrue(exists);
}
/**
* 打开索引
*/
@Test
public void test003openIndex() {
OpenIndexRequest request = new OpenIndexRequest("person_index");
try {
OpenIndexResponse openIndexResponse = client.indices().open(request, RequestOptions.DEFAULT);
boolean acknowledged = openIndexResponse.isAcknowledged();
System.out.println("openIndex:"+acknowledged);
} catch (IOException e) {
e.printStackTrace();
}
}
/***
* 关闭索引
*/
@Test
public void test004closeIndex() {
CloseIndexRequest request = new CloseIndexRequest("person_index");
try {
CloseIndexResponse closeIndexResponse = client.indices().close(request, RequestOptions.DEFAULT);
boolean acknowledged = closeIndexResponse.isAcknowledged();
System.out.println("closeIndex:"+acknowledged);
test003openIndex();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除索引
*/
public void deleteIndex() {
DeleteIndexRequest request = new DeleteIndexRequest("person_index");
AcknowledgedResponse deleteIndexResponse;
try {
deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
boolean acknowledged = deleteIndexResponse.isAcknowledged();
System.out.println("deleteIndex:"+acknowledged);
} catch (IOException e) {
e.printStackTrace();
}
}
// doc 操作#########################################################################
/**
* 新增文档
* @throws IOException
*/
@Test
public void test006insertDoc() throws IOException {
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("id", new Date().getTime());
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch22");
IndexRequest indexRequest = new IndexRequest("person_index").id("2").source(jsonMap);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("insertDoc:"+indexResponse.getId());
}
/**
* 查询文档
* @throws IOException
*/
@Test
public void test007queryDoc() throws IOException {
GetRequest getRequest = new GetRequest("person_index", "2");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println("queryDoc:"+getResponse.getSourceAsString());
}
/**
* 更新文档
*/
@Test
public void test008updateDoc() {
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("updated", new Date());
jsonMap.put("car", "BMW");
jsonMap.put("message", "daily update");
UpdateRequest request = new UpdateRequest("person_index", "2").doc(jsonMap);
try {
UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
System.out.println("updateDoc:");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文档
*/
@Test
public void test009deleteDoc() {
DeleteRequest request = new DeleteRequest("person_index", "a06KsXEBRH_WDXoYCFbn");
try {
DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
System.out.println("文档删除成功");
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test010deleteIndex2() {
//deleteIndex();
}
}