Elasticsearch Delete/UpdateByQuery case sharing
This article involves technical points:
- DeleteByQuery/UpdateByQuery
- Count Document Statistics Api
1. Preparations
Refer to the document " Introduction to the use of high-performance elasticsearch ORM development library " to import and configure es client
2. Write the method to create the index table and initialize the data
The implementation of DocumentCRUD , the component that creates index tables and initializes data, will not be highlighted in this article. Please visit the video tutorial to learn:
/** * Create index table and import test data */ public void initIndiceAndData(){ DocumentCRUD documentCRUD = new DocumentCRUD(); documentCRUD.testCreateIndice();//Create an index table documentCRUD.testBulkAddDocument();//Import test data }
3. Define the Dsl script corresponding to DeleteByQuery/UpdateByQuery
New configuration file - esmapper/byquery.xml
<properties>
<!--
updateByquery
deleteByquery
dsl配置之文件
-->
<property name="updateByQuery">
<![CDATA[
{
"query": {
"bool": {
"filter": [
{ ## 多值检索,查找多个应用名称对应的文档记录
"terms": {
"applicationName.keyword": [#[applicationName1],#[applicationName2]]
}
},
{ ## 时间范围检索,返回对应时间范围内的记录,接受long型的值
"range": {
"agentStarttime": {
"gte": #[startTime],##统计开始时间
"lt": #[endTime] ##统计截止时间
}
}
}
]
}
}
}
]]>
</property>
<property name="deleteByQuery">
<![CDATA[
{
"query": {
"bool": {
"filter": [
{ ## 多值检索,查找多个应用名称对应的文档记录
"terms": {
"applicationName.keyword": [#[applicationName1],#[applicationName2]]
}
},
{ ## 时间范围检索,返回对应时间范围内的记录,接受long型的值
"range": {
"agentStarttime": {
"gte": #[startTime],##统计开始时间
"lt": #[endTime] ##统计截止时间
}
}
}
]
}
}
}
]]>
</property>
</properties>
4. Implement DeleteByQuery function
Define a method that implements the DeleteByQuery function
/**
* 验证DeleteByQuery功能
* @throws ParseException
*/
public void deleteByQuery() throws ParseException {
ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/byquery.xml");
//设定DeleteByQuery查询条件,通过map传递变量参数值,key对于dsl中的变量名称
//dsl中有四个变量
// applicationName1
// applicationName2
// startTime
// endTime
Map<String,Object> params = new HashMap<String,Object>();
//设置applicationName1和applicationName2两个变量的值
params.put("applicationName1","blackcatdemo2");
params.put("applicationName2","blackcatdemo3");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//设置时间范围,时间参数接受long值
params.put("startTime",dateFormat.parse("2017-09-02 00:00:00").getTime());
params.put("endTime",new Date().getTime());
//通过count api先查询数据是否存在
long totalSize = clientUtil.count("demo","deleteByQuery",params);
System.out.println("---------------------------------删除前查询,验证数据是否存在:totalSize="+totalSize);
if(totalSize > 0) {//如果有数据,则通过by query删除之,为了实时查看删除效果,启用了强制刷新机制
//执行DeleteByQuery操作
String result = clientUtil.deleteByQuery("demo/_delete_by_query?refresh", "deleteByQuery", params);
System.out.println(result);
//删除后再次查询,验证数据是否被删除成功
totalSize = clientUtil.count("demo","deleteByQuery",params);
System.out.println("---------------------------------删除后再次查询,验证数据是否被删除:totalSize="+totalSize);
}
}
5. Execute the DeleteByQuery junit unit test method
@Test
public void testDeleteByQuery() throws ParseException {
DeleteUdateByQuery deleteUdateByQuery = new DeleteUdateByQuery();
deleteUdateByQuery.initIndiceAndData();//初始化索引表和测试数据
deleteUdateByQuery.deleteByQuery();//执行deleteByquery
}
6. Define the updatebyquery method
Define two updatebyquery methods, one with query dsl and one without query dsl:
/**
* 验证simpleUpdateByQuery功能
*/
public void simpleUpdateByQuery(){
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
String result = clientUtil.updateByQuery("demo/_update_by_query?conflicts=proceed");
System.out.println("******************simpleUpdateByQuery result:"+result);
}
/**
* 验证带查询条件UpdateByQuery功能
* @throws ParseException
*/
public void updateByQueryWithCondition() throws ParseException {
ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/byquery.xml");
//设定查询条件,通过map传递变量参数值,key对于dsl中的变量名称
//dsl中有四个变量
// applicationName1
// applicationName2
// startTime
// endTime
Map<String,Object> params = new HashMap<String,Object>();
//设置applicationName1和applicationName2两个变量的值
params.put("applicationName1","blackcatdemo2");
params.put("applicationName2","blackcatdemo3");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//设置时间范围,时间参数接受long值
params.put("startTime",dateFormat.parse("2017-09-02 00:00:00").getTime());
params.put("endTime",new Date().getTime());
String result = clientUtil.updateByQuery("demo/_update_by_query?conflicts=proceed","updateByQuery",params);
System.out.println("******************updateByQueryWithCondition result:"+result);
}
7. Execute the updatebyquery junit unit test method
@Test public void testUpdateByQuery() throws ParseException { DeleteUdateByQuery deleteUdateByQuery = new DeleteUdateByQuery(); deleteUdateByQuery.initIndiceAndData();//Initialize the index table and test data deleteUdateByQuery.simpleUpdateByQuery(); deleteUdateByQuery.updateByQueryWithCondition(); }
8. Reference Documentation
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html
The source code project address corresponding to the sample code in this article:
https://gitee.com/bboss/eshelloword
The java file corresponding to this article:
The dsl configuration file corresponding to this article:
https://gitee.com/bboss/eshelloword/blob/master/src/main/resources/esmapper/byquery.xml