SpringBoot 功能封装涉及ElasticSearch的文档查询、更新和删除方法约定如下:
public BulkByScrollResponse deleteDocumentByCondition(String name, Object text, String[] index);
public DeleteResponse deleteDocumentById(String index, String type, String id)
public GetResponse getDocumentById(String index, String type, String id)
public List<GetResponse> getMultiDocument(String index, String type, List<String> ids)
public UpdateResponse updateDocument(String index, String type, String id, byte[] bytes)
public UpdateResponse updateDocument(String index, String type, String id, Map map)
public UpdateResponse updateDocument(String index, String type, String id, XContentBuilder builder)
public UpdateResponse updateDocumentByScript(String index, String type, String id, String script)
在上一篇文中说到:ElasticSearch6.x 基于SpringBoot 实现ElasticSearch连接功能封装,将约定的方法填充到ElasticSearchIndexUtil.java 工具类中。
@Component
public class ElasticSearchIndexUtil {
// 引入 Ela 连接实列化对象
@Autowired
private TransportClient client;
/**
* 功能描述:删除文档
*
* @param index
* 索引名
* @param type
* 索引类型
* @param id
* 文档ID
*/
public DeleteResponse deleteDocumentById(String index, String type, String id) {
DeleteResponse response = client.prepareDelete(index, type, id).get();
return response;
}
/**
* 功能描述:根据条件删除文档
*
* @param condition
* 条件
*/
public BulkByScrollResponse deleteDocumentByCondition(String name, Object text, String[] index) {
DeleteByQueryRequestBuilder builder = DeleteByQueryAction.INSTANCE.newRequestBuilder(client);
return builder.filter(QueryBuilders.matchQuery(name, text)).source(index).get();
}
/**
* 功能描述:查询文档
*
* @param index
* 索引名
* @param type
* 索引类型
* @param id
* 文档ID
*/
public GetResponse getDocumentById(String index, String type, String id) {
GetResponse response = client.prepareGet(index, type, id).get();
return response;
}
/**
* 功能描述:根据文档ID查询
*
* @param index
* 索引名
* @param type
* 索引类型
* @param ids
* 文档IDs
* @return
*/
public List<GetResponse> getMultiDocument(String index, String type, List<String> ids) {
List<GetResponse> list = new ArrayList<GetResponse>();
if (ids != null && ids.size() > 0) {
MultiGetResponse response = client.prepareMultiGet().add(index, type, ids).get();
if (response != null) {
for (MultiGetItemResponse item : response) {
GetResponse getResponse = item.getResponse();
if (getResponse.isExists()) {
list.add(getResponse);
String source = getResponse.getSourceAsString(); // _source
JSONObject jsonObject = JSON.parseObject(source);
Set<String> sets = jsonObject.keySet();
for (String str : sets) {
System.out.println("key -> " + str);
System.out.println("value -> " + jsonObject.get(str));
System.out.println("===============");
}
}
}
}
}
return list;
}
/**
* 功能描述:根据条件更新文档
*
* @param index
* 索引名
* @param type
* 索引类型
* @param id
* 文档ID
* @param map
* 更新文档
* @throws ExecutionException
* @throws InterruptedException
*/
public UpdateResponse updateDocument(String index, String type, String id, Map map)
throws InterruptedException, ExecutionException {
UpdateRequest update = new UpdateRequest();
update.index(index);
update.type(type);
update.id(id);
update.doc(map);
return client.update(update).get();
}
/**
* 功能描述:根据条件更新文档
*
* @param index
* 索引名
* @param type
* 索引类型
* @param id
* 文档ID
* @param json
* 更新文档
* @throws ExecutionException
* @throws InterruptedException
*/
public UpdateResponse updateDocument(String index, String type, String id, String json)
throws InterruptedException, ExecutionException {
UpdateRequest update = new UpdateRequest();
update.index(index);
update.type(type);
update.id(id);
update.doc(json);
return client.update(update).get();
}
/**
* 功能描述:根据条件更新文档
*
* @param index
* 索引名
* @param type
* 索引类型
* @param id
* 文档ID
* @param bytes
* 更新文档
* @throws ExecutionException
* @throws InterruptedException
*/
public UpdateResponse updateDocument(String index, String type, String id, byte[] bytes)
throws InterruptedException, ExecutionException {
UpdateRequest update = new UpdateRequest();
update.index(index);
update.type(type);
update.id(id);
update.doc(bytes);
return client.update(update).get();
}
/**
* 功能描述:根据条件更新文档
*
* @param index
* 索引名
* @param type
* 索引类型
* @param id
* 文档ID
* @param builder
* 更新文档
* @throws ExecutionException
* @throws InterruptedException
*/
public UpdateResponse updateDocument(String index, String type, String id, XContentBuilder builder)
throws InterruptedException, ExecutionException {
UpdateRequest update = new UpdateRequest();
update.index(index);
update.type(type);
update.id(id);
update.doc(builder);
return client.update(update).get();
}
/**
* 功能描述:根据条件更新文档
*
* @param index
* 索引名
* @param type
* 索引类型
* @param id
* 文档ID
* @param script
* 文档脚本
* @throws ExecutionException
* @throws InterruptedException
*/
public UpdateResponse updateDocumentByScript(String index, String type, String id, String script)
throws InterruptedException, ExecutionException {
UpdateRequest update = new UpdateRequest(index, type, id);
update.script(new Script(script));
return client.update(update).get();
}
编写测试工具类Test.java ,测试相关封装的功能代码:
@RunWith(SpringRunner.class)
@SpringBootTest
// 由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration
public class Test {
@Autowired
private ElasticSearchIndexUtil util;
@org.junit.Test
public void deleteDocumentByCondition() {
BulkByScrollResponse response = util.deleteDocumentByCondition("user", (Object) "zzg", new String[] { "book" });
long number = response.getDeleted();
System.out.println("文档删除数量:" + number);
}
@org.junit.Test
public void deleteDocumentById() {
DeleteResponse response = util.deleteDocumentById("book", "library", "kxvlA2wBcCFYGDKDNpMc");
System.out.println(response.toString());
// 删除状态
RestStatus status = response.status();
if (status.OK.getStatus() == 200) {
System.out.println("文档删除成功");
} else {
System.out.println("文档删除失败");
}
}
@org.junit.Test
public void getDocumentById() {
GetResponse response = util.getDocumentById("book", "library", "kBsoA2wBcCFYGDKD55Pg");
// 文档属性
System.out.println(response.toString());
Map<String, Object> map = response.getSource();
Set<String> keySet = map.keySet();
for (String str : keySet) {
Object o = map.get(str);
System.out.println(o.toString());
}
}
@org.junit.Test
public void getMultiDocument() {
List<String> ids = Arrays.asList(new String[] { "lBsOBGwBcCFYGDKDy5Nk" });
List<GetResponse> response = util.getMultiDocument("book", "library", ids);
}
@org.junit.Test
public void updateDocument() {
Map<String, Object> source = new HashMap<String, Object>();
source.put("user", "wz");
source.put("postDate", "2019-07-17");
try {
UpdateResponse response = util.updateDocument("book", "library", "lBsOBGwBcCFYGDKDy5Nk", source);
// 更新文档属性
System.out.println(response.toString());
Map<String, Object> map = response.getGetResult().getSource();
Set<String> keySet = map.keySet();
for (String str : keySet) {
Object o = map.get(str);
System.out.println(o.toString());
}
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@org.junit.Test
public void updateDocumentScript() {
try {
UpdateResponse response = util.updateDocumentByScript("book", "library", "lBsOBGwBcCFYGDKDy5Nk",
"ctx._source.user = \"zzg\"");
System.out.println(response.toString());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}