目录
实现思路
代码
全依赖
参数对象
搜索实现代码全代码
日志
重点
权重
分页
入参高亮数据处理
返回出参数据处理
实现思路
构建请求
请求体设置搜索字段
返回数据解析获取高亮 高亮通过设置标签和class 前端设置class字体颜色 也可直接写在后端
代码
全依赖
package com.esbase.fintech.ais;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
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.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
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.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.histogram.*;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
参数对象
/**
* 根据站内规则设置参数对象
* @author jianghaoyu
*/
@Data
@ToString
static class CourseSearchParam{
String keyword;//关键词
String mt; //一级分类
String st;//二级分类
String grade;//难度等级
Float priceMin;//价格区间
Float priceMax;
String sort;//排序字段
String filter;//过滤字段
}
搜索实现代码全代码
@Test
public void jh6() throws IOException {
CourseSearchParam courseSearchParam = new CourseSearchParam();
courseSearchParam.setKeyword("红色");
query(1, 20, courseSearchParam);
}
public void query(int page, int size, CourseSearchParam courseSearchParam) throws IOException {
if (courseSearchParam == null) {
courseSearchParam = new CourseSearchParam();
}
SearchRequest searchRequest = new SearchRequest("tvs");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//过滤字段
String[] sArr = "brand,color,price,sold_date".split(",");
searchSourceBuilder.fetchSource(sArr, new String[]{});
//查询
BoolQueryBuilder bqb = QueryBuilders.boolQuery();
if (StringUtils.isNotBlank(courseSearchParam.getKeyword())) {//如果关键词存在进入
//如果这几个字段中匹配达到70% 如果name中有提升10倍
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(courseSearchParam.getKeyword(),
"brand", "color").minimumShouldMatch("20%").field("brand", 10);//如果name 中有提示10倍
bqb.must(multiMatchQueryBuilder);
}
//增加字段级别条件
if (StringUtils.isNotBlank(courseSearchParam.getMt())) {//一级分类
bqb.filter(QueryBuilders.termQuery("mt", courseSearchParam.getMt()));
}
if (StringUtils.isNotBlank(courseSearchParam.getSt())) {//二级分类
bqb.filter(QueryBuilders.termQuery("st", courseSearchParam.getSt()));
}
if (StringUtils.isNotBlank(courseSearchParam.getGrade())) {//难度等级
bqb.filter(QueryBuilders.termQuery("grade", courseSearchParam.getGrade()));
}
searchSourceBuilder.query(bqb);
//分页
searchSourceBuilder.from((page - 1) * size);
searchSourceBuilder.size(size);
//设置 高亮 前端使用对应的class
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font class='esLight'>");
highlightBuilder.postTags("</font>");
//高亮字段
highlightBuilder.fields().add(new HighlightBuilder.Field("color"));
searchSourceBuilder.highlighter(highlightBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//
//看结果
SearchHits hits = searchResponse.getHits();
log.info(hits.getTotalHits().value + "");
for (SearchHit key : hits.getHits()) {
Map<String, Object> sourceAsMap = key.getSourceAsMap();
log.info(key.getId() + key.getSourceAsMap().get("brand") + key.getSourceAsMap().get("color"));
String brand = (String) sourceAsMap.get("brand");
//设置高亮
//取出高亮字name
Map<String, HighlightField> highlightFields = key.getHighlightFields();
if (highlightFields != null) {
HighlightField hfn = highlightFields.get("color");
if (hfn != null) {
Text[] fragments = hfn.fragments();
StringBuffer stringBuffer = new StringBuffer();
for (Text text : fragments) {
stringBuffer.append(text);
}
log.info("高亮:"+stringBuffer.toString());
brand = stringBuffer.toString();
}
}
log.info("brand = " + brand);
}
}
日志
23-06-07.16:29:07.024 [main ] INFO SpringBootEsApplicationTestsGj - 3
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - 1天虹红色
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - 高亮:<font class='esLight'>红色</font>
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - brand = <font class='esLight'>红色</font>
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - 3天虹红色
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - 高亮:<font class='esLight'>红色</font>
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - brand = <font class='esLight'>红色</font>
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - 4天虹红色
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - 高亮:<font class='esLight'>红色</font>
23-06-07.16:29:07.038 [main ] INFO SpringBootEsApplicationTestsGj - brand = <font class='esLight'>红色</font>
重点
权重
BoolQueryBuilder bqb = QueryBuilders.boolQuery();
if (StringUtils.isNotBlank(courseSearchParam.getKeyword())) {//如果关键词存在进入
//如果这几个字段中匹配达到70% 如果name中有提升10倍
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(courseSearchParam.getKeyword(),
"brand", "color").minimumShouldMatch("20%").field("brand", 10);//如果name 中有提示10倍
bqb.must(multiMatchQueryBuilder);
}
分页
//分页
searchSourceBuilder.from((page - 1) * size);
searchSourceBuilder.size(size);
入参高亮数据处理
//设置 高亮 前端使用对应的class
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font class='esLight'>");
highlightBuilder.postTags("</font>");
//高亮字段
highlightBuilder.fields().add(new HighlightBuilder.Field("color"));
searchSourceBuilder.highlighter(highlightBuilder);
返回出参数据处理
String brand = (String) sourceAsMap.get("brand");
//设置高亮
//取出高亮字name
Map<String, HighlightField> highlightFields = key.getHighlightFields();
if (highlightFields != null) {
HighlightField hfn = highlightFields.get("color");
if (hfn != null) {
Text[] fragments = hfn.fragments();
StringBuffer stringBuffer = new StringBuffer();
for (Text text : fragments) {
stringBuffer.append(text);
}
log.info("高亮:"+stringBuffer.toString());
brand = stringBuffer.toString();
}
}
log.info("brand = " + brand);
ok文章来源:https://www.toymoban.com/news/detail-784173.html
持续更新文章来源地址https://www.toymoban.com/news/detail-784173.html
到了这里,关于Es elasticsearch 二十 站内搜索示例 高亮内容 java springboot 实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!