一、数据准备
1. 客户端对象client创建核心配置文件:ElasticSearchConfig.class
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
private String host;
private int port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Bean
public RestHighLevelClient client(){
return new RestHighLevelClient(RestClient.builder(
new HttpHost(
host,
port,
"http"
)
));
}
}
2. 配置文件:application.yml
elasticsearch:
host: 192.168.23.129
port: 9200
二、查询
1. 查询全部:matchAll
// 查询所有
@Test
public void matchAll() throws IOException {
// 2.创建查询请求,并且指定查询的索引库
SearchRequest searchRequest = new SearchRequest("goods");
// 4. 创建查询条件对象
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 5.指定具体的查询条件
QueryBuilder query = QueryBuilders.matchAllQuery();
sourceBuilder.query(query);
// 指定分页条件
sourceBuilder.from(0);
sourceBuilder.size(100);
// 3. 设置文档查询条件,需要查询条件对象sourceBuilder
searchRequest.source(sourceBuilder);
// 1. 使用客户端对象client执行查询操作,需要一个查询请求
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//6. 获取查询结果
// 6.1 获取第一层hits,得到查询总数
SearchHits hits = searchResponse.getHits();
long total = hits.getTotalHits().value;
System.out.println(total);
// 6.2 获取第二层hits,得到具体的数据
SearchHit[] hits1 = hits.getHits();
// 7. 创建集合,用于存放转换的goods对象
List<Goods> goodsList = new ArrayList<>();
// 6.3 将查询到的数据数组,转换为json字符串,并转换为goods对象
for (SearchHit hit : hits1) {
String sourceAsString = hit.getSourceAsString();
// 转为java对象
Goods goods = JSON.parseObject(sourceAsString, Goods.class);
// 添加到集合
goodsList.add(goods);
}
// 遍历集合,输出查询结果
for (Goods goods : goodsList) {
System.out.println(goods);
}
}
2. term词条查询
// 5.指定具体的查询条件
QueryBuilder query = QueryBuilders.termQuery("title", "华为");
sourceBuilder.query(query);
// 指定分页条件
sourceBuilder.from(0);
sourceBuilder.size(100);
3. match词条分词查询
// 5.指定具体的查询条件
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "华为手机");
// 定义交并集
matchQueryBuilder.operator(Operator.AND);
sourceBuilder.query(matchQueryBuilder);
4. 模糊查询:wildcard查询
// 5.指定具体的查询条件
QueryBuilder query = QueryBuilders.wildcardQuery("title", "华*");
sourceBuilder.query(query);
5. 模糊查询:正则查询
// 5.指定具体的查询条件
QueryBuilder query = QueryBuilders.regexpQuery("title", "\\w(.)*");
sourceBuilder.query(query);
6. 模糊查询:前缀查询
// 5.指定具体的查询条件
QueryBuilder query = QueryBuilders.prefixQuery("brandName", "三");
sourceBuilder.query(query);
7. 范围查询:rangeQuery
// 5.指定具体的查询条件 范围查询
RangeQueryBuilder query = QueryBuilders.rangeQuery("price");
// 指定下限
query.gte(2000);
// 指定上限
query.lte(3000);
sourceBuilder.query(query);
// 对查询结果进行排序
sourceBuilder.sort("price", SortOrder.ASC);
8. 多字段查询:queryStringQuery
// 5.指定具体的查询条件 范围查询
QueryStringQueryBuilder query = QueryBuilders
.queryStringQuery("华为手机")
.field("title")
.field("categoryName")
.field("brandName")
.defaultOperator(Operator.AND);
sourceBuilder.query(query);
9. 多条件查询
// 5.指定具体的查询条件 bool查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "华为");
boolQueryBuilder.must(termQueryBuilder);
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "手机");
boolQueryBuilder.filter(matchQueryBuilder);
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price");
rangeQueryBuilder.gte(2000);
rangeQueryBuilder.lte(3000);
boolQueryBuilder.filter(rangeQueryBuilder);
sourceBuilder.query(boolQueryBuilder);
10. 聚合查询:
// 5.指定具体的查询条件 聚合查询
// 5.1 查询
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "手机");
sourceBuilder.query(matchQueryBuilder);
// 5.2 聚合
AggregationBuilder aggregation = AggregationBuilders.terms("goods_brands")
.field("brandName").size(100);
sourceBuilder.aggregation(aggregation);
//6. 获取查询结果
// 6.1 获取第一层hits,得到查询总数
SearchHits hits = searchResponse.getHits();
long total = hits.getTotalHits().value;
System.out.println(total);
// 6.2 获取第二层hits,得到具体的数据
SearchHit[] hits1 = hits.getHits();
// 7. 创建集合,用于存放转换的goods对象
List<Goods> goodsList = new ArrayList<>();
// 6.3 将查询到的数据数组,转换为json字符串,并转换为goods对象
for (SearchHit hit : hits1) {
String sourceAsString = hit.getSourceAsString();
// 转为java对象
Goods goods = JSON.parseObject(sourceAsString, Goods.class);
// 添加到集合
goodsList.add(goods);
}
// 遍历集合,输出查询结果
for (Goods goods : goodsList) {
System.out.println(goods);
}
// 获取聚合结果
Aggregations aggregations = searchResponse.getAggregations();
// 将json字符串转成map对象
Map<String, Aggregation> aggregationMap = aggregations.asMap();
Terms goods_brands = (Terms) aggregationMap.get("goods_brands");
List<? extends Terms.Bucket> buckets = goods_brands.getBuckets();
List brands = new ArrayList();
for (Terms.Bucket bucket : buckets) {
Object key = bucket.getKey();
brands.add(key);
}
for (Object brand : brands) {
System.out.println(brand);
}
11. 高亮查询
// 5.1 查询
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "手机");
sourceBuilder.query(matchQueryBuilder);
// 5.2 设置高亮
HighlightBuilder highlighter = new HighlightBuilder();
// 设置三要素
highlighter.field("title");
highlighter.preTags("<font color='red'>");
highlighter.postTags("</font>");
sourceBuilder.highlighter(highlighter);
// 6.3 将查询到的数据数组,转换为json字符串,并转换为goods对象
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
// 转为java对象
Goods goods = JSON.parseObject(sourceAsString, Goods.class);
// 获取高亮结果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
// 根据关键字title获得数组
HighlightField highlightField = highlightFields.get("title");
// 获取片段??
Text[] fragments = highlightField.fragments();
// 替换
goods.setTitle(fragments[0].toString());
// 添加到集合
goodsList.add(goods);
}