基于Spring-Boot框架的Elasticsearch搜索服务器配置

一、相关包maven配置

  <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>

spring-boot  1.3.8.RELEASE能支持配置注解故,elasticsearch的配置可以在 xxx.properties文件中配置

# ELASTICSEARCH (ElasticsearchProperties)
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.9.8.222:9300
spring.data.elasticsearch.repositories.enabled=true

这样可以通过框架自动注入实例化类

    @Autowired
private ElasticsearchTemplate elasticsearchTemplate;

二、处理ES返回数据

获取搜索内容:

     // 空条件查询默认匹配全部
NativeSearchQueryBuilder nsbIndex = new NativeSearchQueryBuilder();
MatchAllQueryBuilder qbIndex = QueryBuilders.matchAllQuery(); // 设置分页参数
Pageable pageable = new PageRequest(page, limit);
nsbIndex.withPageable(pageable);
nsbIndex.withQuery(qbIndex);
SearchQuery searchQuery = nsbIndex.build();
Page<IndexSearch> pageIndexes = null;
try {
pageIndexes = elasticsearchTemplate.queryForPage(searchQuery, IndexSearch.class, new ElasticsearchResultMapper(heightFields));
} catch (Exception e) {
throw new YMLibWebApplicationException("搜索条件为空,查询全部文献出错,出错原因:" + e.getMessage(), e);
}
List<IndexSearch> indexes = pageIndexes.getContent();
.......

处理个性化设置类,如高亮显示属性:

上图红色部分类是一个个性化设置类,继承重写了SearchResultMapper 的mapResults方法

 public class ElasticsearchResultMapper implements SearchResultMapper {
private static Logger log = LoggerFactory.getLogger(ElasticsearchResultMapper.class); private final List<String> heightFields = new ArrayList<String>(); public ElasticsearchResultMapper(){ } public ElasticsearchResultMapper(List<String> heightFields){
this.heightFields.addAll(heightFields);
} @Override
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
// TODO Auto-generated method stub
// 将返回的结果标记指定的属性高亮显示
List<Object> chunk = setHighlightFieldsForSearchHits(response, heightFields);
// 该搜索条件总共命中多少条数据
long totalHits = response.getHits().totalHits();
// System.out.println("totalHits = "+totalHits);
return new FacetedPageImpl((List<T>) chunk, pageable, totalHits);
} /**
*
* @remark:[]
* @author:hoojjack
* @Description:TODO(对返回的结果设置高亮属性)
*/
public List<Object> setHighlightFieldsForSearchHits(@NotNull SearchResponse response,
@NotNull final List<String> heightFields) {
if (0 == heightFields.size()) {
System.out.println("heightFields不能为空");
return null;
}
List<Object> chunk = new ArrayList<Object>();
for (SearchHit searchHit : response.getHits()) {
// 打印出_score
// System.out.println("searchHitScore = " + searchHit.getScore() + "searchHitSource=" + searchHit.getSource());
if (response.getHits().getHits().length <= 0) {
log.info("未命中搜索结果");
return null;
}
Map<String, Object> entityMap = searchHit.getSource();
if (null == entityMap) {
log.error("搜索返回的结果中无_source属性值");
entityMap = new HashMap<String, Object>();
}
for (String highName : heightFields) {
//枚举类型属性不能标亮显示,标亮显示会多久一些标识,导致找不到相对应的枚举类型
if("literClassfication".equals(highName)){
continue;
}
if (null != searchHit.getHighlightFields().get(highName)) {
Text text[] = searchHit.getHighlightFields().get(highName).fragments();
if (text.length > 0) {
String highValue = searchHit.getHighlightFields().get(highName).fragments()[0].toString();
entityMap.put(highName, highValue);
}
}
}
chunk.add(entityMap);
}
return chunk;
} }

以上只是学习的一部分记录,随着学习的深入,再继续更新!

上一篇:centos 搭建php环境


下一篇:luogu1084 [NOIp2012]疫情控制 (二分答案+倍增+dfs序)