组合条件查询范围查询
组合条件查询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; public class ESTest_Doc_Query { public static void main(String[] args) throws Exception { RestHighLevelClient esClient = null; // 创建ES客户端 try { esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")) ); SearchRequest request = new SearchRequest(); request.indices("user"); // 组合条件查询 SearchSourceBuilder builder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30)); boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "男")); builder.query(boolQueryBuilder); request.source(builder); 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()); } } finally { if (esClient != null) { // 关闭ES客户端 esClient.close(); } } } }
使用BoolQueryBuilder , 查询必须sex是男, 并且 age是30的数据.
结果如下:
{“name”:“zhangsan”,“age”:30,“sex”:“男”}
修改查询条件, 查询age是30 , sex不是男性的数据
修改的代码如下:
boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30)); boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex", "男"));
查询性别不是男性, 年龄是30 或40的数据, 使用should
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex", "男")); boolQueryBuilder.should(QueryBuilders.matchQuery("age", 30)); boolQueryBuilder.should(QueryBuilders.matchQuery("age", 40));
查询结果如下:
{"name":"lisi","age":30,"sex":"女"} {"name":"wangwu1","age":40,"sex":"女"}范围查询
查询年龄 大于等于30 , 小于等于 50的数据
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.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; public class ESTest_Doc_Query { public static void main(String[] args) throws Exception { RestHighLevelClient esClient = null; // 创建ES客户端 try { esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")) ); SearchRequest request = new SearchRequest(); request.indices("user"); // 范围查询 SearchSourceBuilder builder = new SearchSourceBuilder(); RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age"); rangeQuery.gte(30); rangeQuery.lte(50); builder.query(rangeQuery); request.source(builder); 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()); } } finally { if (esClient != null) { // 关闭ES客户端 esClient.close(); } } } }
结果打印如下 :
{“name”:“zhangsan”,“age”:30,“sex”:“男”}
{“name”:“lisi”,“age”:30,“sex”:“女”}
{“name”:“wangwu”,“age”:40,“sex”:“男”}
{“name”:“wangwu1”,“age”:40,“sex”:“女”}
{“name”:“wangwu2”,“age”:50,“sex”:“男”}
{“name”:“wangwu3”,“age”:50,“sex”:“男”}
主要使用RangeQueryBuilder 来进行范围查询.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)