一、相关包maven配置

1  <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
2         <dependency>
3             <groupId>org.springframework.data</groupId>
4             <artifactId>spring-data-elasticsearch</artifactId>
5             <version>1.3.6.RELEASE</version>
6         </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返回数据

获取搜索内容:

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

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

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

 1 public class ElasticsearchResultMapper implements SearchResultMapper {
 2     private static Logger log = LoggerFactory.getLogger(ElasticsearchResultMapper.class);
 3     
 4     private final List<String> heightFields = new ArrayList<String>();
 5     
 6     public ElasticsearchResultMapper(){
 7         
 8     }
 9     
10     public ElasticsearchResultMapper(List<String> heightFields){
11         this.heightFields.addAll(heightFields);
12     }
13 
14     @Override
15     public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
16         // TODO Auto-generated method stub
17         // 将返回的结果标记指定的属性高亮显示
18         List<Object> chunk = setHighlightFieldsForSearchHits(response, heightFields);
19         // 该搜索条件总共命中多少条数据
20         long totalHits = response.getHits().totalHits();
21         // System.out.println("totalHits = "+totalHits);
22         return new FacetedPageImpl((List<T>) chunk, pageable, totalHits);
23     }
24     
25     
26     /**
27      * 
28      * @remark:[]
29      * @author:hoojjack
30      * @Description:TODO(对返回的结果设置高亮属性)
31      */
32     public List<Object> setHighlightFieldsForSearchHits(@NotNull SearchResponse response,
33             @NotNull final List<String> heightFields) {
34         if (0 == heightFields.size()) {
35             System.out.println("heightFields不能为空");
36             return null;
37         }
38         List<Object> chunk = new ArrayList<Object>();
39         for (SearchHit searchHit : response.getHits()) {
40             // 打印出_score
41 //            System.out.println("searchHitScore = " + searchHit.getScore() + "searchHitSource=" + searchHit.getSource());
42             if (response.getHits().getHits().length <= 0) {
43                 log.info("未命中搜索结果");
44                 return null;
45             }
46             Map<String, Object> entityMap = searchHit.getSource();
47             if (null == entityMap) {
48                 log.error("搜索返回的结果中无_source属性值");
49                 entityMap = new HashMap<String, Object>();
50             }
51             for (String highName : heightFields) {
52                 //枚举类型属性不能标亮显示,标亮显示会多久一些标识,导致找不到相对应的枚举类型
53                 if("literClassfication".equals(highName)){
54                     continue;
55                 }
56                 if (null != searchHit.getHighlightFields().get(highName)) {
57                     Text text[] = searchHit.getHighlightFields().get(highName).fragments();
58                     if (text.length > 0) {
59                         String highValue = searchHit.getHighlightFields().get(highName).fragments()[0].toString();
60                         entityMap.put(highName, highValue);
61                     }
62                 }
63             }
64             chunk.add(entityMap);
65         }
66         return chunk;
67     }
68 
69 }

 

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