1、 请求体查询
1.查询所有索引数据
/**
* @author zhubayi
* 请求体查询
*/
public class QueryBody {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
//构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询所有数据
sourceBuilder.query(QueryBuilders.matchAllQuery());
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took:" + response.getTook());
System.out.println("timeout:" + response.isTimedOut());
System.out.println("total:" + hits.getTotalHits());
System.out.println("MaxScore:" + hits.getMaxScore());
System.out.println("hits========>>");
for (SearchHit hit : hits) {
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果:
2.term 查询,查询条件为关键字
/**
* @author zhubayi
* term 查询,查询条件为关键字
*/
public class TermQuery {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
//构建查询体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("age", 30));
//把查询体添加到请求里
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took:" + response.getTook());
System.out.println("timeout:" + response.isTimedOut());
System.out.println("total:" + hits.getTotalHits());
System.out.println("MaxScore:" + hits.getMaxScore());
System.out.println("hits========>>");
for (SearchHit hit : hits) {
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果:
3.分页查询
/**
* @author zhubayi
* 分页查询
*/
public class PageQuery {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询体的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
//分页查询
// 当前页其实索引(第一条数据的顺序号),from
sourceBuilder.from(0);
// 每页显示多少条 size
sourceBuilder.size(2);
request.source(sourceBuilder);
//把查询体添加到请求里
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took:" + response.getTook());
System.out.println("timeout:" + response.isTimedOut());
System.out.println("total:" + hits.getTotalHits());
System.out.println("MaxScore:" + hits.getMaxScore());
System.out.println("hits========>>");
for (SearchHit hit : hits) {
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.数据排序
/**
* @author zhubayi
* 排序
*/
public class OrderQuery {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询体的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
//排序
sourceBuilder.sort("age", SortOrder.DESC);
//把查询体添加到请求里
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took:" + response.getTook());
System.out.println("timeout:" + response.isTimedOut());
System.out.println("total:" + hits.getTotalHits());
System.out.println("MaxScore:" + hits.getMaxScore());
System.out.println("hits========>>");
for (SearchHit hit : hits) {
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果:
5.过滤字段
/**
* @author zhubayi
* 过滤字段
*/
public class FilterQuery {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询体的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
//查询字段过滤
String[] excludes={};
String[] includes={"name","age"};
sourceBuilder.fetchSource(includes,excludes);
//把查询体添加到请求里
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took:" + response.getTook());
System.out.println("timeout:" + response.isTimedOut());
System.out.println("total:" + hits.getTotalHits());
System.out.println("MaxScore:" + hits.getMaxScore());
System.out.println("hits========>>");
for (SearchHit hit : hits) {
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果:
6.Bool 查询
/**
* @author zhubayi
* Bool查询
*/
public class BoolQuery {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询体的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
//查询字段过滤
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 必须包含
boolQueryBuilder.must(QueryBuilders.matchQuery("age",30));
//一定不包含
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name","zhangsan"));
//可能包含
boolQueryBuilder.should(QueryBuilders.matchQuery("sex","男"));
sourceBuilder.query(boolQueryBuilder);
//把查询体添加到请求里
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took:" + response.getTook());
System.out.println("timeout:" + response.isTimedOut());
System.out.println("total:" + hits.getTotalHits());
System.out.println("MaxScore:" + hits.getMaxScore());
System.out.println("hits========>>");
for (SearchHit hit : hits) {
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果:
7.范围查询
/**
* @author zhubayi
* 范围查询
*/
public class RangQuery {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
//构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//范围查询
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
rangeQuery.gte("30");
rangeQuery.lte("40");
sourceBuilder.query(rangeQuery);
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took:" + response.getTook());
System.out.println("timeout:" + response.isTimedOut());
System.out.println("total:" + hits.getTotalHits());
System.out.println("MaxScore:" + hits.getMaxScore());
System.out.println("hits========>>");
for (SearchHit hit : hits) {
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果:
8.模糊查询
/**
* @author zhubayi
*/
public class FuzzyQuery {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询体的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.fuzzyQuery("name","zhangsan").fuzziness(Fuzziness.ONE));
//把查询体添加到请求里
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took:" + response.getTook());
System.out.println("timeout:" + response.isTimedOut());
System.out.println("total:" + hits.getTotalHits());
System.out.println("MaxScore:" + hits.getMaxScore());
System.out.println("hits========>>");
for (SearchHit hit : hits) {
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、高亮查询
/**
* @author zhubayi
* 高亮查询
*/
public class Test01 {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//高亮查询
SearchRequest request = new SearchRequest();
request.indices("student");
//创建查询请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//构建查询方式:高亮查询
TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan");
//设置查询方式
sourceBuilder.query(termsQueryBuilder);
//构建高亮字段
HighlightBuilder highlightBuilder = new HighlightBuilder();
//设置前置标签
highlightBuilder.preTags("<font color='red'>");
//设置后置标签
highlightBuilder.postTags("</font>");
//高亮字段
highlightBuilder.field("name");
//设置高亮构建对象
sourceBuilder.highlighter(highlightBuilder);
//设置请求体
request.source(sourceBuilder);
//3.客户端发送请求,获取响应对象
SearchResponse response = null;
try {
response = client.search(request, RequestOptions.DEFAULT);
//4.打印响应结果
SearchHits hits = response.getHits();
System.out.println("took::"+response.getTook());
System.out.println("time_out::"+response.isTimedOut());
System.out.println("total::"+hits.getTotalHits());
System.out.println("max_score::"+hits.getMaxScore());
System.out.println("hits::::>>");
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
System.out.println(sourceAsString);
//打印高亮结果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
System.out.println(highlightFields);
}
System.out.println("<<::::");
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果:
3、聚合查询
1、最大年龄
/**
* @author zhubayi
*/
public class MaxAge {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询体的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//聚合查询
sourceBuilder.aggregation(AggregationBuilders.max("maxAge").field("age"));
//把查询体添加到请求里
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、分组统计
/**
* @author zhubayi
* 分组统计
*/
public class GroupQuery {
public static void main(String[] args) {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询体的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//分组查询
sourceBuilder.aggregation(AggregationBuilders.terms("age_groupby").field("age"));
//把查询体添加到请求里
request.source(sourceBuilder);
try {
//发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println("<<========");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果: