码迷,mamicode.com
首页 > 其他好文 > 详细

es xxx_by_query

时间:2018-08-23 00:16:33      阅读:3321      评论:0      收藏:0      [点我收藏+]

标签:col   evel   new   rate   utf8   http   bsp   span   dice   

xxx_by_query包括_delete_by_query和_update_by_query,下面分开讲

_delete_by_query

相当于sql中的delete from a where 。。。删除满足条件的文档

在Dev Tools执行的格式是

POST teacher/_doc/_delete_by_query
{
	"query": {
		"range": {
			"updated_date": {
				"from": 1534007400000,
				"to": 1534007700000
			}
		}
	}
}

值得注意的是,Java High Level REST Client截止到6.3.2版本(elasticsearch-rest-high-level-client-6.3.2.jar),尽管有DeleteByQueryRequest类,可以通过DeleteByQueryRequest(SearchRequest search)构造器构造一个实例,但是RestHighLevelClient实例没有合适的方法来操作DeleteByQueryRequest实例(如RestHighLevelClient实例可以用index(XxxRequest xxxRequest, Header... headers)实例方法来操作索引的文档,可以用indices()实例方法获取IndicesClient实例,进而通过其各种实例方法来操作索引,但是没有方法的入参是DeleteByQueryRequest实例)。

想在java代码中实现delete_by_query功能,有2种替代方法:

1.先用searchRequest筛选出符合条件的文档的id集合,然后根据id用deleteRequest来删除。

2.调用RestHighLevelClient实例的getLowLevelClient()方法获取org.elasticsearch.client.RestClient实例,即low-level client,然后调用其performRequest(String method, String endpoint, Map<String, String> params, HttpEntity entity, Header... headers)方法,其中method是"POST",endpoint是"/index/doc/_delete_by_query",代码示例如下:

    public static void main(String[] args) throws Exception {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.56.11", 9200, "http"),
                        new HttpHost("192.168.56.12", 9201, "http")
                )
        );
        try {
            HttpEntity entity = new NStringEntity(generateQueryString(), ContentType.APPLICATION_JSON);
            Response response = client.getLowLevelClient().performRequest("POST", "/teacher/_doc/_delete_by_query", Collections.emptyMap(), entity);
            Map responseMap = JSON.parseObject(IOUtils.toString(response.getEntity().getContent(), "UTF-8"));
            LOGGER.info(responseMap);
        } catch (Exception e) {
            LOGGER.error("client delete_by_query exception", e);
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }

    public static String generateQueryString() {
        IndexRequest indexRequest = new IndexRequest();
        XContentBuilder builder;
        try {
            long begin = 1534015700000L;
            long end = 1534016700000L;
            builder = JsonXContent.contentBuilder()
                    .startObject()
                    .startObject("query")
                    .startObject("range")
                    .startObject("updated_date").field("from", begin).field("to", end)
                    .endObject()
                    .endObject()
                    .endObject()
                    .endObject();
            indexRequest.source(builder);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return indexRequest.source().utf8ToString();
    }

 

es xxx_by_query

标签:col   evel   new   rate   utf8   http   bsp   span   dice   

原文地址:https://www.cnblogs.com/koushr/p/9520811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!