Elasticsearch之使用RestClient实现日期和数值的范围查询操作

Posted 你是小KS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch之使用RestClient实现日期和数值的范围查询操作相关的知识,希望对你有一定的参考价值。

当前版本elasticsearch 7.13.4

1. 声明

当前内容主要为学习和了解Elasticsearch的操作,主要为数值的范围查询操作,主要参考:官方文档

主要为

  1. 数值类型的范围查询
  2. 日期类型的范围查询

pom依赖

<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>elasticsearch-rest-client-sniffer</artifactId>
	<version>7.13.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.76</version>
</dependency>

2.基本demo

创建实体类:Book

public class Book {
	private Integer id;
	private String bookName;
	private Double price;
	private Integer count;
	private String publishDate;
	// 省略get set toString 、无参有参构造函数
}

开始编写代码

public static void main(String[] args) throws IOException {
		RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
		// Sniffer 默认为5分钟一次更新节点信息(主要为控制节点,判断节点是否可以访问)
		// sniffer的主要作用就是按照指定的时间间隔方式修改restClient的setNodes方法
		Sniffer sniffer = Sniffer.builder(restClient).build();
		// 手动设置为1分钟更新一次节点
		// Sniffer.builder(restClient).setSniffIntervalMillis(60*1000);
		try {
			// 1. 添加数据
			 addData(restClient);
			// 2. 查询操作:简单条件分页查询操作(区间查询操作)
			// selectDataUsingConditionPageNumberRange(restClient);
			// 3. 查询操作:简单的条件查询操作(区间查询,日期区间查询)
			selectDataUsingConditionPageDateRange(restClient);
			
		} finally {
			// 注意关闭顺序
			sniffer.close();
			restClient.close();
		}
}

/**
	 * 
	 * @author hy
	 * @createTime 2021-07-31 11:08:03
	 * @description 基本的数据添加操作
	 * @param restClient
	 * @throws IOException
	 *
	 */
	private static void addData(RestClient restClient) throws IOException {
		Request request = new Request("PUT", "/book/java/1");
		// 默认publishDate变成了数值类型
		//Book book = new Book(1, "java", 18.5, 5,new Date(2021, 07, 31));
		Book book = new Book(1, "java", 18.5, 5,"2021-07-31 12:12:12");
		String bookJson = JSON.toJSONString(book);
		request.setJsonEntity(bookJson);
		Response response = restClient.performRequest(request);
		int statusCode = response.getStatusLine().getStatusCode();
		System.out.println(response);
		// Response{requestLine=PUT /book/java/1 HTTP/1.1, host=http://localhost:9200,
		// response=HTTP/1.1 201 Created}
	}
/**
	 * 
	 * @author hy
	 * @createTime 2021-07-31 13:57:48
	 * @description 使用条件分页查询操作(且使用区间查询操作)
	 * @throws IOException 
	 *
	 */
	private static void selectDataUsingConditionPageNumberRange(RestClient restClient) throws IOException {
		// 这个使用RestClient方式操作,会出现错误
				
		// request.setJsonEntity("{\\"from\\":0,\\"size\\":10,\\"id\\":2}"); // 这里出现错误,无法获取key id,
		// {"error":{"root_cause":[{"type":"parsing_exception","reason":"Unknown key for a VALUE_NUMBER in [id].","line":1,"col":26}],"type":"parsing_exception","reason":"Unknown key for a VALUE_NUMBER in [id].","line":1,"col":26},"status":400}
		// 注意最大size不能超过10000,否则查询报错
		//String encode = URLEncoder.encode("price:<100");
		System.out.println("查询价格小于66.6的=====》");
		// 使用大于查询
		String encode = URLEncoder.encode("price:<66.6", "UTF-8");
		// 注意使用特殊字符需要加码
		Request request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		Response response = restClient.performRequest(request);
		System.out.println(response);
		String result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询价格小于66.6的=====》");
		// 查询价格小于66.6的,两边都不包含
		encode = URLEncoder.encode("price:{* TO 66.6}", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询价格小于等于66.6的=====》");
		// 查询小于66.6的
		encode = URLEncoder.encode("price:(* TO 66.6)", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询价格小于等于66.6的=====》");
		// 查询小于等于66.6的
		encode = URLEncoder.encode("price:[* TO 66.6]", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		
		System.out.println("查询价格全部的=====》");
		// 查询小于等于66.6的
		encode = URLEncoder.encode("price:(>=10 OR <=100)", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		// 注意OR AND可以用于(内部)
		// {} 表示两边都是开区间
		// () 和[]都表示闭区间
		// 左开右闭{]	左闭右开[}	
		// TO 表示从什么到什么
		// * 表示通配符
		// > >= < <= 
		// ^数值	表示对该值感兴趣的程度,默认为1,越大表示关注度越高
		
	}
	
	/**
	 * 
	 * @author hy
	 * @createTime 2021-07-31 15:09:41
	 * @description 条件查询,并使用日期区间查询
	 * @param restClient
	 * @throws IOException
	 *
	 */
	private static void selectDataUsingConditionPageDateRange(RestClient restClient) throws IOException {
		System.out.println("查询出版日期在2020年以后的=====》");
		// 使用大于等于查询(这个可以查询出来的,插入的值变成数值了,但是可以查询出来)
		String encode = URLEncoder.encode("publishDate:[2020 TO *]", "UTF-8");
		// 注意使用特殊字符需要加码
		Request request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		Response response = restClient.performRequest(request);
		System.out.println(response);
		String result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询出版日期在2020年06月以后的=====》");
		// 使用大于等于查询(这个可以查询出来的,插入的值变成数值了,但是不可以查询出来)
		// 下面这个也是可以查询出来的
		encode = URLEncoder.encode("publishDate:[2021-06 TO *]", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询出版日期在2020年06月01日以后的=====》");
		// 使用大于等于查询(这个可以查询出来的,插入的值变成数值了,但是不可以查询出来)
		encode = URLEncoder.encode("publishDate:[2021-06-01 TO *]", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询出版日期在2020年06月01日到2021-08-21的数据=====》");
		// 存储日期为2021-07-31 12:12:12
		// 使用大于等于查询(这个不可以查询出来的,插入的值变成数值了,但是不可以查询出来)
		encode = URLEncoder.encode("publishDate:[2021-06-01 TO 2021-08-21]", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询出版日期在2021-08-21以前的数据=====》");
		// 存储日期为2021-07-31 12:12:12
		// 使用大于等于查询(这个可以查询出来的)
		encode = URLEncoder.encode("publishDate:[* TO 2021-07-31]", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询出版日期在2021-07-31(不包含7-31)以前的数据=====》");
		// 存储日期为2021-07-31 12:12:12
		// 使用大于等于查询(这个可以查询出来的,这个应该有问题)
		encode = URLEncoder.encode("publishDate:[* TO 2021-07-31}", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		System.out.println("查询出版日期在2021-07-31(不包含7-31)以前的数据=====》");
		// 存储日期为2021-07-31 12:12:12
		// 使用大于等于查询(这个不可以查询出来的,这个应该有问题)
		encode = URLEncoder.encode("publishDate:{2021-06-21 TO 2021-08-31}", "UTF-8");
		// 注意使用特殊字符需要加码
		request = new Request("GET", "/book/java/_search?q="+encode);
		request.setJsonEntity("{\\"from\\":0,\\"size\\":10}");
		response = restClient.performRequest(request);
		System.out.println(response);
		result = getResponseContent(response);
		System.out.println(result);
		
		
	}
		/**
	 * 
	 * @author hy
	 * @createTime 2021-07-31 13:51:11
	 * @description 获取带有返回值的响应数据,例如select查询操作
	 * @param response
	 * @return
	 * @throws UnsupportedOperationException
	 * @throws IOException
	 *
	 */
	private static String getResponseContent(Response response) throws UnsupportedOperationException, IOException {
		if (response == null) {
			return null;
		}
		HttpEntity entity = response.getEntity();
		StringBuilder builder = new StringBuilder();
		if (entity != null) {
			InputStream content = entity.getContent();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				builder.append(line);
			}
		}
		return builder.toString();
	}



3.查看结果

1. 数值查询

查询价格小于66.6的=====》
七月 31, 2021 3:49:33 下午 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."]
七月 31, 2021 3:49:33 下午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/book/java/_search?q=price%3A%3C66.6] 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=price%3A%3C66.6 HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"book","_type":"java","_id":"1","_score":1.0,"_source":{"bookName":"java","count":5,"id":1,"price":18.5,"publishDate":"2021-07-31 12:12:12"}}]}}
查询价格小于66.6的=====》
七月 31, 2021 3:49:33 下午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/book/java/_search?q=price%3A%7B*+TO+66.6%7D] 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=price%3A%7B*+TO+66.6%7D HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"book","_type":"java","_id":"1","_score":1.0,"_source":{"bookName":"java","count":5,"id":1,"price":18.5,"publishDate":"2021-07-31 12:12:12"}}]}}
查询价格小于等于66.6的=====》
七月 31, 2021 3:49:33 下午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/book/java/_search?q=price%3A%28*+TO+66.6%29] 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=price%3A%28*+TO+66.6%29 HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"book","_type":"java","_id":"1","_score":1.0,"_source":{"bookName":"java","count":5,"id":1,"price":18.5,"publishDate":"2021-07-31 12:12:12"}}]}}
查询价格小于等于66.6的=====》
七月 31, 2021 3:49:33 下午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/book/java/_search?q=price%3A%5B*+TO+66.6%5D] 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=price%3A%5B*+TO+66.6%5D HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"book","_type":"java","_id":"1","_score":1.0,"_source":{"bookName":"java","count":5,"id":1,"price":18.5,"publishDate":"2021-07-31 12:12:12"}}]}}
查询价格全部的=====》
七月 31, 2021 3:49:33 下午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://127.0.0.1:9200/book/java/_search?q=price%3A%28%3E%3D10+OR+%3C%3D100%29] 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=price%3A%28%3E%3D10+OR+%3C%3D100%29 HTTP/1.1, host=http://127.0.0.1:9200, response=HTTP/1.1 200 OK}
{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":2.0,"hits":[{"_index":"book","_type":"java","_id":"1","_score":2.0,"_source":{"bookName":"java","count":5,"id":1,"price":18.5,"publishDate":"2021-07-31 12:12:12"}}]}}

其中()和[]对等,{}表示两边都不包含,AND和OR表示并且和或者,*表示通配符

2.日期范围查询

查询出版日期在2020年以后的=====》
七月 31, 2021 3:51:36 下午 org.elasticsearch.client.RestClient logResponse
警告: request [GET http://localhost:9200/book/java/_search?q=publishDate%3A%5B2020+TO+*%5D] 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."]
七月 31, 2021 3:51:36 下午 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."]
Response{requestLine=GET /book/java/_search?q=publishDate%3A%5B2020+TO+*%5D HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
{"took":4,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"tot

以上是关于Elasticsearch之使用RestClient实现日期和数值的范围查询操作的主要内容,如果未能解决你的问题,请参考以下文章

elasticsearch基本操作之--使用QueryBuilders进行查询

搜索引擎之laravel中使用elasticsearch

Elasticsearch系列(12)Query之复合查询

java使用elasticsearch进行模糊查询之must使用

[Docker] - 使用 Kitematic 安装 elasticsearch 失败 之解决

ELK 架构之 Elasticsearch 和 Kibana 安装配置