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}}]}}

可以查询到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中使用Elasticsearch查询向量数据,你需要使用Elasticsearch的Java客户端库来与Elasticsearch集群进行交互。以下是一个简单的示例代码,演示了如何使用Java客户端进行向量数据查询: 首先,确保你已经安装了Elasticsearch,并在Java项目中添加了Elasticsearch Java客户端的依赖。 ```java import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; public class ElasticsearchVectorQueryExample { public static void main(String[] args) { // 创建一个RestHighLevelClient实例 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder("localhost:9200")); // 构建查询请求 SearchRequest searchRequest = new SearchRequest("your_index_name"); searchRequest.types("your_document_type"); // 替换为你的索引和文档类型 // 构建查询条件 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders .functionScoreQuery(QueryBuilders.matchAllQuery()) .add(ScoreFunctionBuilders .scriptFunction(new Script( ScriptType.INLINE, "knn", "params.queryVector = [1, 2, 3];\n" + "params.field = 'your_vector_field';\n" + "double[] vector = doc[params.field].value;\n" + "double sum = 0;\n" + "for (int i = 0; i < vector.length; i++) {\n" + " sum += Math.pow(vector[i] - params.queryVector[i], 2);\n" + "}\n" + "return 1 / (1 + Math.sqrt(sum));", null))) .boostMode("replace")); // 设置查询结果的距离单位 sourceBuilder.trackScores(true).trackTotalHits(true) .postFilter(QueryBuilders .geoDistanceQuery("location") .point(40, -70) // 中心点坐标 .distance(10, DistanceUnit.MILES)); // 距离与单位 searchRequest.source(sourceBuilder); try { // 执行查询 SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); // 处理查询结果 // ... } catch (IOException e) { e.printStackTrace(); } finally { // 关闭Elasticsearch客户端连接 try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 在上面的示例代码中,我们使用了function_score查询来执行向量相似度计算。`params.queryVector`是你要查询的向量数据,`params.field`是存储向量数据的字段名。你可以根据实际情况修改这两个参数。 注意,在执行查询之前,你需要替换`"your_index_name"`和`"your_document_type"`为你的索引名和文档类型名。此外,你还可以根据需要设置其他查询条件,例如地理位置过滤等。 希望这个示例对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值