Java操作ElasticSearch-索引-文档
Posted 永旗狍子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java操作ElasticSearch-索引-文档相关的知识,希望对你有一定的参考价值。
目录
一.Java操作ElasticSearch
1.Java连接ES
创建Maven工程
导入依赖
<dependencies>
<!-- 1. elasticsearch-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.5.4</version>
</dependency>
<!-- 2. elasticsearch的高级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>
<!-- 5. jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
创建测试类,连接ES
/**
* @author: qiuyongqi
* @time: 15:50 2021/6/7
* @description: 创建测试类,连接ES
*/
public class EsClient {
public static RestHighLevelClient getClient(){
// 创建HttpHost对象
HttpHost httpHost = new HttpHost("192.168.247.128",9200);
// 创建RestClientBuilder
RestClientBuilder clientBuilder = RestClient.builder(httpHost);
// 创建RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
// 返回
return client;
}
}
2.Java操作索引
2.1创建索引
代码如下
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
}
/**
* @author: qiuyongqi
* @time: 16:53 2021/6/7
* @description: 索引
*/
public class demo1 {
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type = "man";
/**
* @author: qiuyongqi
* @time: 17:03 2021/6/7
* @description: 创建索引
*/
@Test
public void createIndex() throws IOException {
//1. 准备关于索引的settings
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 3)
.put("number_of_replicas", 1);
//2. 准备关于索引的结构mappings
XContentBuilder mapppings =
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封装到一个Request对象
CreateIndexRequest request = new CreateIndexRequest(index)
.settings(settings)
.mapping(type, mapppings);
//4. 通过client对象去连接ES并执行创建索引
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
//5.输出
System.out.println("respose:" + response);
}
}
2.2检查索引是否存在
代码如下
/**
* @author: qiuyongqi
* @time: 17:20 2021/6/7
* @description: 检查索引是否存在
*/
@Test
public void exists() throws IOException {
//1. 准备request对象
GetIndexRequest request = new GetIndexRequest();
request.indices(index);
//2. 通过client去操作
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
//3.输出
System.out.println(exists);
}
2.3删除索引
代码如下
/**
* @author: qiuyongqi
* @time: 17:23 2021/6/7
* @description: 删除索引
*/
@Test
public void delete() throws IOException {
//1. 准备request对象
DeleteIndexRequest request = new DeleteIndexRequest();
request.indices(index);
//2. 通过client对象执行
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
//3.输出
System.out.println(response.isAcknowledged());
}
3.Java操作文档
3.1添加文档操作
代码如下
/**
* @author: qiuyongqi
* @time: 17:27 2021/6/7
* @description: 文档操作
*/
public class demo2 {
ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type = "man";
/**
* @author: qiuyongqi
* @time: 17:41 2021/6/7
* @description: 创建文档
*/
@Test
public void createDoc() throws IOException {
//1. 准备一个json数据
Person person = new Person("张三", 28, new Date());
String json = mapper.writeValueAsString(person);
//2. 准备一个request对象(手动指定id)
IndexRequest request = new IndexRequest(index, type, "1");
request.source(json, XContentType.JSON);
//3. 通过client对象执行添加
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
//4. 输出返回结果
System.out.println(response.getResult().toString());
}
}
3.2修改文档
代码如下
/**
* @author: qiuyongqi
* @time: 18:57 2021/6/7
* @description: 修改文档
*/
@Test
public void updateDoc() throws IOException {
//1. 创建一个Map,指定需要修改的内容
Map<String,Object> doc=new HashMap<String, Object>();
doc.put("name","李四");
//2. 创建request对象,封装数据
UpdateRequest request = new UpdateRequest(index, type, "1");
request.doc(doc);
//3. 通过client对象执行
UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
//4. 输出返回结果
System.out.println(update.getResult().toString());
}
3.3删除文档
代码如下
/**
* @author: qiuyongqi
* @time: 19:03 2021/6/7
* @description: 删除文档
*/
@Test
public void deleteDoc() throws IOException {
//1. 封装Request对象
DeleteRequest request = new DeleteRequest(index, type, "2");
//2. client执行
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
//3. 输出结果
System.out.println(response.getResult().toString());
}
4.Java批量操作文档
4.1 批量添加
代码如下
/**
* @author: qiuyongqi
* @time: 19:07 2021/6/7
* @description: 批量操作文档
*/
public class demo3 {
ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient client = EsClient.getClient();
String index = "person";
String type = "man";
/**
* @author: qiuyongqi
* @time: 19:07 2021/6/7
* @description: 批量添加
*/
@Test
public void bulkcreateDoc() throws IOException {
//1. 准备多个json数据
Person p1 = new Person("zs", 19, new Date());
Person p2 = new Person("lisi", 29, new Date());
Person p3 = new Person("ww", 39, new Date());
String json1 = mapper.writeValueAsString(p1);
String json2 = mapper.writeValueAsString(p2);
String json3 = mapper.writeValueAsString(p3);
//2. 创建Request,将准备好的数据封装进去
BulkRequest request = new BulkRequest();
// IndexRequest c1 = new IndexRequest(index, type, "2");
// IndexRequest c2 = new IndexRequest(index, type, "3");
// IndexRequest c3 = new IndexRequest(index, type, "4");
request.add(new IndexRequest(index, type, "2").source(json1, XContentType.JSON));
request.add(new IndexRequest(index, type, "3").source(json2, XContentType.JSON));
request.add(new IndexRequest(index, type, "4").source(json3, XContentType.JSON));
//3. 用client执行
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
//4. 输出结果
System.out.println(response.toString());
}
}
4.2 批量删除
代码如下
/**
* @author: qiuyongqi
* @time: 19:33 2021/6/7
* @description: 批量删除
*/
@Test
public void bulkDeleteDoc() throws IOException {
//1. 封装Request对象
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest(index, type, "2"));
request.add(new DeleteRequest(index, type, "3"));
request.add(new DeleteRequest(index, type, "4"));
//2. client执行
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
//3.输出
System.out.println(response.toString());
}
看懂了,扣懂了
以上是关于Java操作ElasticSearch-索引-文档的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch——使用Java API实现ES中的索引映射文档操作
Elasticsearch——使用Java API实现ES中的索引映射文档操作
Elasticsearch - Java API 操作 ES7.16.0+ES8.x 索引,文档;高级搜索
Elasticsearch - Java API 操作 ES7.15.0ES7.x 索引,文档;高级搜索