Elasticsearch之使用RestClient实现null和非null的查询操作
Posted 你是小KS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch之使用RestClient实现null和非null的查询操作相关的知识,希望对你有一定的参考价值。
版本:elasticsearch 7.13.4
1. 声明
当前内容主要为使用RestClient实现对Elasticsearch的null字段和非null字段的查询
当前内容基于前面的博文
2. 更新字段并设置值为null
更新属性并设置值为null
之前的内容
private static void updateDataSetBookNameEqNull(RestClient restClient) throws IOException {
Request request = new Request("POST", "/book/java/1/_update");
String json = "{\\"doc\\":{\\"bookName\\":null}}";
request.setJsonEntity(json);
Response response = restClient.performRequest(request);
System.out.println(response);
}
执行后
3. 开始执行对非null的查询(exisits)
/**
*
* @author hy
* @createTime 2021-07-31 16:06:44
* @description 查询当前bookName不是null的数据
* @param restClient
* @throws IOException
*
*/
private static void selectDataWhereBookNameIsNotNull(RestClient restClient) throws IOException {
String encode = URLEncoder.encode("_exists_:bookName", "UTF-8");
Request request = new Request("GET", "/book/java/_search?q="+encode);
Response response = restClient.performRequest(request);
System.out.println(response);
String result = getResponseContent(response);
System.out.println(result);
}
查询结果
八月 01, 2021 11:59:47 上午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/_nodes/http?timeout=1000ms] returned 1 warnings: [299 Elasticsearch-7.13.4-c5f60e894ca0c61cdbae4f5a686d9f08bcefc942 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security."]
八月 01, 2021 11:59:47 上午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/book/java/_search?q=_exists_%3AbookName] returned 2 warnings: [299 Elasticsearch-7.13.4-c5f60e894ca0c61cdbae4f5a686d9f08bcefc942 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.13.4-c5f60e894ca0c61cdbae4f5a686d9f08bcefc942 "[types removal] Specifying types in search requests is deprecated."]
Response{requestLine=GET /book/java/_search?q=_exists_%3AbookName HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"max_score":null,"hits":[]}}
没有任何数据匹配!
主要使用q=_exisits_:字段
方式查询非null的字段
4. 开始执行对null的查询
/**
*
* @author hy
* @createTime 2021-07-31 16:06:44
* @description 查询当前bookName=null的数据
* @param restClient
* @throws IOException
*
*/
private static void selectDataWhereBookNameIsNull(RestClient restClient) throws IOException {
// 错误,无法查询
// String encode = URLEncoder.encode("bookName:null", "UTF-8");
Request request = new Request("GET", "/book/java/_search");
request.setJsonEntity( "{\\"query\\": {\\"bool\\": {\\"must_not\\": {\\"exists\\": {\\"field\\": \\"bookName\\"}}}}}");
Response response = restClient.performRequest(request);
System.out.println(response);
String result = getResponseContent(response);
System.out.println(result);
}
结果
八月 01, 2021 11:57:47 上午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/_nodes/http?timeout=1000ms] returned 1 warnings: [299 Elasticsearch-7.13.4-c5f60e894ca0c61cdbae4f5a686d9f08bcefc942 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security."]
八月 01, 2021 11:57:48 上午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/book/java/_search] returned 2 warnings: [299 Elasticsearch-7.13.4-c5f60e894ca0c61cdbae4f5a686d9f08bcefc942 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.13.4-c5f60e894ca0c61cdbae4f5a686d9f08bcefc942 "[types removal] Specifying types in search requests is deprecated."]
Response{requestLine=GET /book/java/_search HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
{"took":359,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.0,"hits":[{"_index":"book","_type":"java","_id":"1","_score":0.0,"_source":{"bookName":null,"id":2,"price":66.6}}]}}
可以查询到
以上是关于Elasticsearch之使用RestClient实现null和非null的查询操作的主要内容,如果未能解决你的问题,请参考以下文章
elasticsearch基本操作之--使用QueryBuilders进行查询
java使用elasticsearch进行模糊查询之must使用