Spring Data Elasticsearch高亮查询实现


Spring Data Elasticsearch和Spring Data JPA类似,封装了很多易用的查询API,大大简化了我们从elasticsearch集群查询数据的操作,只需实现 ElasticsearchCrudRepository接口即可和JPA使用方式一样。但是其默认的结果结果映射类DefaultResultMapper不支持高亮,因此,本文的重点是介绍如何扩展自定义扩展DefaultResultMapper实现高亮显示功能。

Maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--工具包 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

application.yml配置

此处index-name为自定义参数,因为spring data elasticsearch支持动态索引配置,具体参考文章(https://blog.csdn.net/u014229347/article/details/88896459)

spring:
  data:
    elasticsearch:
      cluster-name: myescluster
      cluster-nodes: ip1:9300, ip2:9300, ip3:9300
      index-name: test

自定义ResultMapper

思路:自己新增一个设置高亮内容的方法,在mapResults方法中调用即可
//mapResults方法中新增高亮设置调用
this.populateHighLightedFields(result, hit.getHighlightFields());
新增方法:populateHighLightedFields() 和 textToStr()
其余方法都来自DefaultResultMapper的源码实现,代码可参考如下,

package com.ylp729.config;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.apache.commons.beanutils.PropertyUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.ScriptedField;
import o
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Data Elasticsearch 5 提供了对查询结果的支持。要实现,需要使用Elasticsearch的highlighting API。 在Spring Data Elasticsearch中,可以使用`HighlightBuilder`类来构建查询,然后将其添加到查询构建器中。以下是一个简单的示例: ```java HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.field("title"); //设置要的字段 NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.matchQuery("title", "spring data")) .withHighlightBuilder(highlightBuilder) //将HighlightBuilder添加到查询构建器中 .build(); SearchHits<Article> searchHits = elasticsearchRestTemplate.search(searchQuery, Article.class); for (SearchHit<Article> hit : searchHits) { String title = hit.getHighlightFields().get("title").get(0); //获取结果 // ... } ``` 在上面的示例中,`HighlightBuilder`对象设置了要的字段,然后将其添加到`NativeSearchQuery`对象中。执行搜索后,可以使用`SearchHit`对象获取结果。 值得注意的是,要使生效,必须在索引中启用设置。在创建索引时,可以通过设置`mapping`来启用。例如: ```json { "mappings": { "properties": { "title": { "type": "text", "analyzer": "standard" }, "content": { "type": "text", "analyzer": "standard" }, "publishDate": { "type": "date" } }, "highlight": { "fields": { "title": {}, "content": {} } } } } ``` 在上面的示例中,`highlight`对象定义了要的字段。将此映射应用于索引后,就可以在搜索时使用了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值