确定引入版本
Spring 项目中专门有链接ES的组件 ,可以方便快速的链接ES。官方文档一般都会有版本对照的说明 版本对照
同时也提供了更为方便的“Spring Boot Starter Data Elasticsearch” 。
在这里我们使用的是“Spring Boot Starter Data Elasticsearch » 2.6.15” 这里要注意结合自己项目版本选择正确的“Spring Boot Starter Data Elasticsearch“版本
引入到项目即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.6.15</version>
</dependency>
编写启动类
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Value("${elasticsearch-config.connected}")
private String connected;
@Value("${elasticsearch-config.userName}")
private String userName;
@Value("${elasticsearch-config.password}")
private String password;
@Override
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
// ES ip:端口
.connectedTo(connected)
// ES 账号密码
.withBasicAuth(userName, password)
.withConnectTimeout(15000L)
.withSocketTimeout(3000L)
.build();
return RestClients.create(clientConfiguration).rest();
}
}
编写映射
我们仅需简单的注解配置即可完成java 实体和 ES文档的映射
这里推荐一个 大佬的博文
import lombok.*;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
// 这里indexName 要与 ES索引名对应,别名也行。
@Document(indexName = "product_spu", createIndex = false)
public class ProductSpuEntity extends BaseEntity {
@Field(type = FieldType.Text)
private String name;
@Field(type = FieldType.Text, name = "name_pinyin")
private String namePinyin;
@Field(type = FieldType.Text, name = "product_code")
private String productCode;
@Field(type = FieldType.Text, name = "spec_code")
private String specCode;
@Field(type = FieldType.Text, name = "product_spec")
private String productSpec;
@Field(type = FieldType.Text, name = "sku_number")
private String skuNumber;
@Field(type = FieldType.Integer, name = "product_type")
private Integer productType;
@Field(type = FieldType.Integer)
private Integer status;
@Field(type = FieldType.Text)
private String remark;
}
编写请求示例
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.xxxx.component.base.config.ElasticsearchBaseConfig;
import com.xxxx.domain.dto.foundation.pagehelp.entity.PageBO;
import com.xxxx.domain.dto.foundation.pagehelp.util.PageUtil;
import com.xxxx.foundation.base.ElasticsearchHighlightContext;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
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.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Component;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Component
public class ProductQueryManager {
@Autowired
private ElasticsearchOperations elasticsearchOperations;
@Autowired
private ElasticsearchHighlightContext elasticsearchHighlightContext;
@Autowired
private ElasticsearchBaseConfig elasticsearchBaseConfig;
public <T> List<T> spuQuery(Long corpId, String keyWord, Integer pageNum, Integer pageSize,
Boolean hideStop, Boolean needProductBundle,
Class<T> clazz) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("corp_id", corpId.toString()));
if (BooleanUtils.isTrue(hideStop)) {
boolQueryBuilder.must(QueryBuilders.termQuery("status", 1));
}
if (BooleanUtils.isFalse(needProductBundle)) {
boolQueryBuilder.must(QueryBuilders.termQuery("product_type", 1));
}
// boost 设置权重 caseInsensitive 忽略大小写
boolQueryBuilder.should(QueryBuilders.termQuery("name", keyWord).boost(elasticsearchBaseConfig.getProductNameTermBoost()).caseInsensitive(true));
boolQueryBuilder.should(QueryBuilders.termQuery("sku_number", keyWord).boost(elasticsearchBaseConfig.getSkuNumberTermBoost()).caseInsensitive(true));
if (isPureLetters(keyWord)) {
// 若是纯英文字符则查询拼音
boolQueryBuilder.should(QueryBuilders.termQuery("name.pinyin", keyWord).boost(elasticsearchBaseConfig.getProductNameTermBoost()));
}
boolQueryBuilder.should(QueryBuilders.termQuery("product_code", keyWord).boost(elasticsearchBaseConfig.getProductCodeTermBoost()).caseInsensitive(true));
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyWord)
.field("name", elasticsearchBaseConfig.getProductNameFuzzyBoost())
.field("name.ngram", elasticsearchBaseConfig.getProductNameFuzzyBoost())
.field("product_code")
.field("product_code.ngram")
.field("sku_number", elasticsearchBaseConfig.getProductSkuNumberFuzzyBoost())
.field("sku_number.ngram", elasticsearchBaseConfig.getProductSkuNumberFuzzyBoost())
.field("product_spec", elasticsearchBaseConfig.getProductSpecFuzzyBoost())
.field("product_spec.ngram", elasticsearchBaseConfig.getProductSpecFuzzyBoost())
.field("spec_code", elasticsearchBaseConfig.getSpecCodeFuzzyBoost())
.field("spec_sku_number", elasticsearchBaseConfig.getProductSkuNumberFuzzyBoost())
.field("spec_sku_number.ngram", elasticsearchBaseConfig.getProductSkuNumberFuzzyBoost())
.field("spec_code.ngram");
if (isPureLetters(keyWord)) {
multiMatchQueryBuilder.field("name.pinyin", elasticsearchBaseConfig.getProductNameFuzzyBoost())
.field("name_pinyin", elasticsearchBaseConfig.getProductNameFuzzyBoost())
.field("sku_number.pinyin", elasticsearchBaseConfig.getProductSkuNumberFuzzyBoost())
.field("spec_sku_number.pinyin", elasticsearchBaseConfig.getProductSkuNumberFuzzyBoost())
.field("product_spec.pinyin", elasticsearchBaseConfig.getProductSpecFuzzyBoost());
}
boolQueryBuilder.should(multiMatchQueryBuilder);
HighlightBuilder highlightBuilder = new HighlightBuilder()
.field("name")
.field("name.ngram")
.field("product_code")
.field("product_code.ngram")
.field("sku_number")
.field("sku_number.ngram")
.field("product_spec")
.field("product_spec.ngram")
.field("spec_code")
.field("spec_code.ngram");
if (isPureLetters(keyWord)) {
highlightBuilder.field("name.pinyin")
.field("sku_number.pinyin")
.field("product_spec.pinyin");
}
// 至少需要满足一个should条件
boolQueryBuilder.minimumShouldMatch(1);
return nativeSearchQuery(boolQueryBuilder, highlightBuilder, pageNum, pageSize, clazz);
}
public <T> List<T> nativeSearchQuery(QueryBuilder queryBuilder, HighlightBuilder highlightBuilder, Integer pageNum, Integer pageSize, Class<T> clazz) {
PageBO page = new PageBO();
page.setPageNum(pageNum);
page.setPageSize(pageSize);
// es分页从0开始的
pageNum = pageNum - 1;
if (pageNum < 0) {
pageNum = 0;
}
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
// 查询设置
.withQuery(queryBuilder)
// 高亮设置
.withHighlightBuilder(highlightBuilder)
// 分页设置
.withPageable(PageRequest.of(pageNum, pageSize))
.build();
Set<String> highlightSet = Sets.newHashSet();
SearchHits<T> searchHits = elasticsearchOperations.search(nativeSearchQuery, clazz);
// 处理高亮数据
List<SearchHit<T>> searchHitList = searchHits.stream().collect(Collectors.toList());
for (SearchHit<T> searchHit : searchHitList) {
Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
if (MapUtils.isNotEmpty(highlightFields)) {
highlightFields.values().forEach(data -> {
data.forEach(highlightValues -> {
highlightSet.addAll(extractHighlightValue(highlightValues));
});
});
}
}
page.setTotal(searchHits.getTotalHits());
PageUtil.setPage(page);
// 将高亮数据放入本地线程变量,elasticsearchHighlightContext 是一个 TransmittableThreadLocal,自己建的
elasticsearchHighlightContext.setContext(Lists.newArrayList(highlightSet));
return searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
}
public Set<String> extractHighlightValue(String str) {
String regex = "<em>(.*?)</em>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
Set<String> extractedChars = new HashSet<>();
while (matcher.find()) {
String extractedChar = matcher.group(1);
if (StringUtils.isBlank(extractedChar)) {
continue;
}
extractedChars.add(extractedChar);
}
return extractedChars;
}
public boolean isPureLetters(String str) {
return str.matches("[a-zA-Z]+");
}
}