实验1、查询所有索引数据
package demo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
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.QueryBuilder;
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 Demo07 {
public static void main(String[] args) throws IOException {
//创建客户端对象
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());
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配度分值:"+hits.getMaxScore());
System.out.println("hits===============================>>");
for(SearchHit hit : hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<<===============================");
//关闭客户端连接
client.close();
}
}
运行结果:
实验2、term查询,查询条件为关键字
package demo;
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.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class Demo08 {
public static void main(String[] args) throws IOException {
//创建客户端对象
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);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配分度值:"+hits.getMaxScore());
System.out.println("hits=================>>");
for(SearchHit hit: hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
//关闭客户端连接
client.close();
}
}
运行结果:
实验3、分页查询
package demo;
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.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class Demo09 {
public static void main(String[] args) throws IOException {
//创建客户端对象
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.from(0);
//每页显示的条数
sourceBuilder.size(2);
request.source(sourceBuilder);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配分度值:"+hits.getMaxScore());
System.out.println("hits=================>>");
for(SearchHit hit: hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
//关闭客户端连接
client.close();
}
}
运行结果:
实验4、数据排序
package demo;
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.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
public class Demo10 {
public static void main(String[] args) throws IOException {
//创建客户端对象
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.ASC);
request.source(sourceBuilder);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配分度值:"+hits.getMaxScore());
System.out.println("hits=================>>");
for(SearchHit hit: hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
//关闭客户端连接
client.close();
}
}
运行结果:
实验5、过滤字段
package demo;
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.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
public class Demo11 {
public static void main(String[] args) throws IOException {
//创建客户端对象
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);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配分度值:"+hits.getMaxScore());
System.out.println("hits=================>>");
for(SearchHit hit: hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
//关闭客户端连接
client.close();
}
}
运行结果:
实验6、Bool 查询
package demo;
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 Demo12 {
public static void main(String[] args) throws IOException {
//创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
//构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
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);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配分度值:"+hits.getMaxScore());
System.out.println("hits=================>>");
for(SearchHit hit: hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
//关闭客户端连接
client.close();
}
}
运行结果:
实验7、范围查询
package demo;
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 Demo13 {
public static void main(String[] args) throws IOException {
//创建客户端对象
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);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配分度值:"+hits.getMaxScore());
System.out.println("hits=================>>");
for(SearchHit hit: hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
//关闭客户端连接
client.close();
}
}
运行结果:
实验8、模糊查询
package demo;
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.common.unit.Fuzziness;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
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 Demo14 {
public static void main(String[] args) throws IOException {
//创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
//构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
FuzzyQueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("name", "zhansan").fuzziness(Fuzziness.ONE);
sourceBuilder.query(queryBuilder);
request.source(sourceBuilder);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配分度值:"+hits.getMaxScore());
System.out.println("hits=================>>");
for(SearchHit hit: hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
//关闭客户端连接
client.close();
}
}
运行结果:
实验9、高亮查询
package demo;
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.common.unit.Fuzziness;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import java.io.IOException;
import java.util.Map;
public class Demo15 {
public static void main(String[] args) throws IOException {
//创建客户端对象
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);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//查询匹配
SearchHits hits = response.getHits();
System.out.println("查询花费时间,单位毫秒:"+response.getTook());
System.out.println("是否超时:"+response.isTimedOut());
System.out.println("总数:"+hits.getTotalHits());
System.out.println("匹配分度值:"+hits.getMaxScore());
System.out.println("hits=================>>");
for(SearchHit hit: hits){
//输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
//输出高亮结果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
System.out.println(highlightFields);
}
//关闭客户端连接
client.close();
}
}
运行结果:
实验10、聚合查询
10.1、最大年龄
package demo;
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.common.unit.Fuzziness;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class Demo16 {
public static void main(String[] args) throws IOException {
//创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
//构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("maxAge").field("age");
//应用查询
sourceBuilder.aggregation(maxAggregationBuilder);
//应用请求体
request.source(sourceBuilder);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//打印响应结果
System.out.println(response);
//关闭客户端连接
client.close();
}
}
运行结果:
10.2、分组统计
package demo;
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.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class Demo17 {
public static void main(String[] args) throws IOException {
//创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
//构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("age_groupby").field("age");
//应用查询
sourceBuilder.aggregation(aggregationBuilder);
//应用请求体
request.source(sourceBuilder);
//获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//打印响应结果
System.out.println(response);
//关闭客户端连接
client.close();
}
}
运行结果: