es (7.7.1)与springboot整合
2020-06-27 本文已影响0人
jianshuqiang
使用es的原因
es 基于内存的倒排索引算法,能够快速进行数据的索引
使用场景
1、本人在测试中实现了(vue) 下拉框的高亮显示以及快速查询
2、文本数据的快速索引,主要是文档(诗词内容的查询)
3、诗词中高频字词的汇总查询
pom 文件的引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
加粗显示,以及合并(and)查询
@Override
public LayuiResponse query(Integer page, Integer limit, String title, String author, String paragraphs) {
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.postTags("</strong>");
highlightBuilder.preTags("<strong>");
highlightBuilder.field("author");
BoolQueryBuilder query = QueryBuilders.boolQuery();
PageRequest pageRequest = PageRequest.of(page - 1, limit);
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withPageable(pageRequest);
if (!StringUtils.isEmpty(title)) {
//query.must(matchQuery("title", title));
query.must(termQuery("title", title.trim()));
// nativeSearchQueryBuilder.withQuery(matchQuery("title", title));
}
if (!StringUtils.isEmpty(author)) {
query.must(matchQuery("author", author.trim()));
}
if (!StringUtils.isEmpty(paragraphs)) {
query.must(matchQuery("paragraphs", paragraphs.trim()));
}
NativeSearchQuery build = nativeSearchQueryBuilder
//查询中必须要有 nativeSearchQueryBuilder.withQuery(matchQuery("author", author)); 与之相对应才能高亮查询
.withHighlightBuilder(highlightBuilder)
.withQuery(query)
.build();
SearchHits<SongDoc> search = elasticsearchTemplate.search(build, SongDoc.class);
Stream<SearchHit<SongDoc>> searchHitStream = search.get();
List<SearchHit<SongDoc>> collect = searchHitStream.collect(Collectors.toList());
List<SongDoc> list = new ArrayList<>();
if (!CollectionUtils.isEmpty(collect)) {
collect.forEach(songDocSearchHit -> {
Map<String, List<String>> highlightFields = songDocSearchHit.getHighlightFields();
System.out.println("highlightFields:" + highlightFields.size());
SongDoc content = songDocSearchHit.getContent();
if (null != highlightFields.get("author")) {
content.setAuthor(highlightFields.get("author").get(0).toString());
}
list.add(content);
});
}
LayuiResponse layuiResponse = new LayuiResponse();
layuiResponse.setStatus(Status.SUCCESS);
layuiResponse.setCount((int) search.getTotalHits());
layuiResponse.setData(list);
return layuiResponse;
}
高频词查询
@Override
public LayuiResponse songhighfrequencylist(Integer count) {
PageRequest of = PageRequest.of(1, 1);
LayuiResponse layuiResponse = new LayuiResponse();
// FilterAggregationBuilder.
//Aggregations aggregations=new Aggregations();
//ValueCountAggregationBuilder result = AggregationBuilders.count("key_word").field("author");
TermsAggregationBuilder field = AggregationBuilders.terms("group_name_keyword")
.field("paragraphs")
// .executionHint("map")
.size(count);
//.includeExclude(IncludeExclude.parseInclude()))
NativeSearchQuery build = new NativeSearchQueryBuilder()
//.withQuery(matchQuery("author", "宋太宗"))
.withPageable(of)
.addAggregation(field).build();
long beginTime = System.currentTimeMillis();
//SearchHits<SongDoc> search2 = elasticsearchTemplate.search(build, SongDoc.class);
SearchHits<SongDoc> search = elasticsearchTemplate.search(build, SongDoc.class);
long endTime = System.currentTimeMillis();
System.out.println("时间(s)" + (endTime - beginTime));
List<Integer> seriesDataList = new ArrayList<>();
List<String> xAxisDataList = new ArrayList<>();
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
ParsedStringTerms aggregation = (ParsedStringTerms) aggregations.asMap().get("group_name_keyword");
List<? extends Terms.Bucket> buckets = aggregation.getBuckets();
buckets.forEach(bucket -> {
seriesDataList.add((int) bucket.getDocCount());
xAxisDataList.add(bucket.getKey().toString());
System.out.println(bucket.getKey() + "\t" + bucket.getDocCount());
});
aggregation.getMetaData();
// List<StringTerms.Bucket> buckets = group_name.getBuckets();
// System.out.println(buckets.size());
// Map<String, Object> metaData = group_name.getMetaData();
// System.out.println(metaData);
}
EchartsDataVO echartsDataVO = new EchartsDataVO();
echartsDataVO.setSeriesData(seriesDataList);
echartsDataVO.setXAxisData(xAxisDataList);
layuiResponse.setData(echartsDataVO);
layuiResponse.setCode(20000);
Stream<SearchHit<SongDoc>> searchHitStream = search.get();
List<SearchHit<SongDoc>> collect = searchHitStream.collect(Collectors.toList());
System.out.println(collect.size());
return layuiResponse;
}
其它查询
@Override
public LayuiResponse queryByKeywords(String keywords) {
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.postTags("</strong>");
highlightBuilder.preTags("<strong>");
highlightBuilder.field("name");
// NativeSearchQuery query =new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("name", keywords)).withHighlightBuilder(highlightBuilder).build();
//NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("name", keywords).operator(MatchQueryBuilder.o)).build();
// NativeSearchQuery query =new NativeSearchQueryBuilder().withQuery(matchQuery("name", keywords)).build();
NativeSearchQuery query = new NativeSearchQueryBuilder()
//短语匹配:短语匹配意味着不仅仅是词要匹配,并且词的顺序也要一致
//.withQuery(matchPhraseQuery("name", keywords))
//可以指定词之间的逻辑关系 张三 李四 张三and 李四
//.withQuery(matchQuery("name", keywords).operator(Operator.AND))
//相关度查询
// .withQuery(matchQuery("name", keywords).minimumShouldMatch("80%"))
//设置权重查询
// .withQuery(matchQuery("name", keywords).boost(10))
.withQuery(matchQuery("name", keywords))
.withHighlightBuilder(highlightBuilder).build();
SearchHits<AuthorSelectDoc> searchResult = elasticsearchRestTemplate.search(query, AuthorSelectDoc.class);
Stream<SearchHit<AuthorSelectDoc>> searchHitStream = searchResult.get();
List<SearchHit<AuthorSelectDoc>> collect = searchHitStream.collect(Collectors.toList());
LayuiResponse layuiResponse = new LayuiResponse();
layuiResponse.setData(collect);
return layuiResponse;
}