https://www.elastic.co/cn/
https://www.elastic.co/guide/index.html
选择Elasticsearch: Store, Search, and Analyze区域的Elasticsearch Clients链接,如下图:
选择Java REST Client [7.13] — other versions,然后选择自己需要的版本就可以了,如下图:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.8/index.html
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Before;
import org.junit.Test;
public class DemoElasticSearchTest {
private RestHighLevelClient client = null;
@Before
public void init() {
client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
}
@Test
public void createIndex() {
System.out.println(client);
}
}
@Test
public void createIndex() throws IOException {
// 创建请求
CreateIndexRequest request = new CreateIndexRequest("tb_hero");
// 设置索引参数
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
);
//
// 设置映射
Map<String, Object> properties = new HashMap<>();
//-- name
Map<String, Object> name = new HashMap<>();
name.put("type", "text");
properties.put("name", name);
//-- gender
Map<String, Object> gender = new HashMap<>();
gender.put("type", "text");
properties.put("gender", gender);
//-- age
Map<String, Object> age = new HashMap<>();
age.put("type", "integer");
properties.put("age", age);
//-- work
Map<String, Object> work = new HashMap<>();
work.put("type", "text");
properties.put("work", work);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
client.close();
System.out.println("-- end");
}
@Test
public void indexExits() throws IOException {
GetIndexRequest request = new GetIndexRequest("tb_hero");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
public void updateIndexSetting() throws IOException {
UpdateSettingsRequest request = new UpdateSettingsRequest("tb_hero");
String settingKey = "index.number_of_replicas";
int settingValue = 0;
Settings settings =
Settings.builder()
.put(settingKey, settingValue)
.build();
request.settings(settings);
AcknowledgedResponse updateSettingsResponse =
client.indices().putSettings(request, RequestOptions.DEFAULT);
System.out.println(updateSettingsResponse);
}
@Test
public void appendIndexMapping() throws IOException {
PutMappingRequest request = new PutMappingRequest("tb_hero");
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("message", message);
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("properties", properties);
request.source(jsonMap);
AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
System.out.println(putMappingResponse);
}
@Test
public void putIndexMapping() throws IOException {
PutMappingRequest request = new PutMappingRequest("tb_hero");
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
message.put("analyzer", "ik_smart");
Map<String, Object> properties = new HashMap<>();
properties.put("message2", message);
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("properties", properties);
request.source(jsonMap);
AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
System.out.println(putMappingResponse);
}
@Test
public void getIndexMapping() throws IOException {
GetMappingsRequest request = new GetMappingsRequest();
request.indices("tb_hero");
GetMappingsResponse getMappingResponse = client.indices().getMapping(request, RequestOptions.DEFAULT);
Map<String, MappingMetadata> allMappings = getMappingResponse.mappings();
MappingMetadata indexMapping = allMappings.get("tb_hero");
Map<String, Object> mapping = indexMapping.sourceAsMap();
System.out.println(mapping);
}
public void deleteIndex() throws IOException {
// 删除
DeleteIndexRequest request = new DeleteIndexRequest("tb_hero");
AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
// 检测
GetIndexRequest getRequest = new GetIndexRequest("tb_hero");
boolean exists = client.indices().exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}