其实就是等值查询,只不过在里面加入了分页、排序、超时、路由等等设置,并且在查询结果里面增加了一些处理。
private static void genSearch() throws IOException { String type = "_doc"; String index = "test1"; // 查询指定的索引库 SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 设置查询条件 sourceBuilder.query(QueryBuilders.termQuery("uid", "1234")); // 设置起止和结束 sourceBuilder.from(0); sourceBuilder.size(5); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); // 设置路由 // searchRequest.routing("routing"); // 设置索引库表达式 searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); // 查询选择本地分片,默认是集群分片 searchRequest.preference("_local"); // 排序 // 根据默认值进行降序排序 // sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); // 根据字段进行升序排序 // sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC)); // 关闭suorce查询 // sourceBuilder.fetchSource(false); String[] includeFields = new String[]{"title", "user", "innerObject.*"}; String[] excludeFields = new String[]{"_type"}; // 包含或排除字段 // sourceBuilder.fetchSource(includeFields, excludeFields); searchRequest.source(sourceBuilder); System.out.println("普通查询的DSL语句:"+sourceBuilder.toString()); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // HTTP状态代码、执行时间或请求是否提前终止或超时 RestStatus status = searchResponse.status(); TimeValue took = searchResponse.getTook(); Boolean terminatedEarly = searchResponse.isTerminatedEarly(); boolean timedOut = searchResponse.isTimedOut(); // 供关于受搜索影响的切分总数的统计信息,以及成功和失败的切分 int totalShards = searchResponse.getTotalShards(); int successfulShards = searchResponse.getSuccessfulShards(); int failedShards = searchResponse.getFailedShards(); // 失败的原因 for (ShardSearchFailure failure : searchResponse.getShardFailures()) { // failures should be handled here } // 结果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); System.out.println("普通查询的结果:" + map); }); System.out.println("\n=================\n"); } 或查询其实这个或查询也是bool查询中的一种,这里的查询语句相当于SQL语句中的
SELECT * FROM test1 where (uid = 1 or uid =2) and phone = 12345678919
代码示例:
private static void orSearch() throws IOException { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("test1"); searchRequest.types("_doc"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder(); /** * SELECT * FROM test1 where (uid = 1234 or uid =12345) and phone = 12345678909 * */ boolQueryBuilder2.should(QueryBuilders.termQuery("uid", 1234)); boolQueryBuilder2.should(QueryBuilders.termQuery("uid", 12345)); boolQueryBuilder.must(boolQueryBuilder2); boolQueryBuilder.must(QueryBuilders.termQuery("phone", "12345678909")); searchSourceBuilder.query(boolQueryBuilder); System.out.println("或查询语句:" + searchSourceBuilder.toString()); searchRequest.source(searchSourceBuilder); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); searchResponse.getHits().forEach(documentFields -> { System.out.println("查询结果:" + documentFields.getSourceAsMap()); }); } 模糊查询相当于SQL语句中的like查询。
private static void likeSearch() throws IOException { String type = "_doc"; String index = "test1"; SearchRequest searchRequest = new SearchRequest(); searchRequest.indices(index); searchRequest.types(type); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); /** * SELECT * FROM p_test where message like '%xu%'; * */ boolQueryBuilder.must(QueryBuilders.wildcardQuery("message", "*xu*")); searchSourceBuilder.query(boolQueryBuilder); System.out.println("模糊查询语句:" + searchSourceBuilder.toString()); searchRequest.source(searchSourceBuilder); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); searchResponse.getHits().forEach(documentFields -> { System.out.println("模糊查询结果:" + documentFields.getSourceAsMap()); }); System.out.println("\n=================\n"); } 多值查询也就是相当于SQL语句中的in查询。
private static void inSearch() throws IOException { String type = "_doc"; String index = "test1"; // 查询指定的索引库 SearchRequest searchRequest = new SearchRequest(index,type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); /** * SELECT * FROM p_test where uid in (1,2) * */ // 设置查询条件 sourceBuilder.query(QueryBuilders.termsQuery("uid", 1, 2)); searchRequest.source(sourceBuilder); System.out.println("in查询的DSL语句:"+sourceBuilder.toString()); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 结果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); String string = hit.getSourceAsString(); System.out.println("in查询的Map结果:" + map); System.out.println("in查询的String结果:" + string); }); System.out.println("\n=================\n"); } 存在查询