es xxx_by_query
Posted koushr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es xxx_by_query相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
用于模糊的 OpenGL ES 2.0 片段着色器速度慢且质量低
“ES7 React/Redux/GraphQL/React-Native 片段”不适用于 javascript 文件。除了安装它,我还需要配置其他东西吗?