前言
本节主要实现条件查询中的组合查询,范围查询
实现
编写组合查询 ESTest_Doc_Cond_Query_Com.java
package com.zwy.es;
import org.apache.http.HttpHost;
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.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class ESTest_Doc_Cond_Query_Com {
public static void main(String[] args) throws IOException {
//
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 组合查询数据
SearchRequest request = new SearchRequest();
request.indices("users");
SearchSourceBuilder builer = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));
boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "男"));
// boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex", "女"));
builer.query(boolQueryBuilder);
request.source(builer);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits()); // 打印
System.out.println(response.getTook()); // 打印
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
// 关闭ES客户端
esClient.close();
}
}
编写范围查询 ESTest_Doc_Cond_Query_Range.java
package com.zwy.es;
import org.apache.http.HttpHost;
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.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class ESTest_Doc_Cond_Query_Range {
public static void main(String[] args) throws IOException {
//
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 范围查询数据
SearchRequest request = new SearchRequest();
request.indices("users");
SearchSourceBuilder builer = new SearchSourceBuilder();
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age"); // 设定年龄范围
rangeQuery.gte(30);
rangeQuery.lte(40);
builer.query(rangeQuery);
request.source(builer);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits()); // 打印
System.out.println(response.getTook()); // 打印
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
// 关闭ES客户端
esClient.close();
}
}
文件结构
运行
运行ESTest_Doc_Cond_Query_Com.java
运行ESTest_Doc_Cond_Query_Range.java
|