1. 程式人生 > >和我一起打造個簡單搜索之SpringDataElasticSearch關鍵詞高亮

和我一起打造個簡單搜索之SpringDataElasticSearch關鍵詞高亮

http rms 接口 this mail ron super contex 集群

前面幾篇文章詳細講解了 ElasticSearch 的搭建以及使用 SpringDataElasticSearch 來完成搜索查詢,但是搜索一般都會有搜索關鍵字高亮的功能,今天我們把它給加上。

系列文章

  • 一、和我一起打造個簡單搜索之ElasticSearch集群搭建
  • 二、和我一起打造個簡單搜索之ElasticSearch入門
  • 三、和我一起打造個簡單搜索之IK分詞以及拼音分詞
  • 四、和我一起打造個簡單搜索之Logstash實時同步建立索引
  • 五、和我一起打造個簡單搜索之SpringDataElasticSearch入門
  • 六、和我一起打造個簡單搜索之SpringDataElasticSearch關鍵詞高亮
  • ...

環境依賴

本文以及後續 es 系列文章都基於 5.5.3 這個版本的 elasticsearch ,這個版本比較穩定,可以用於生產環境。

SpringDataElasticSearch 的基本使用可以看我的上一篇文章 和我一起打造個簡單搜索之SpringDataElasticSearch入門,本文就不再贅述。

高亮關鍵字實現

前文查詢是通過寫一個接口來繼承 ElasticsearchRepository 來實現的,但是如果要實現高亮,我們就不能這樣做了,我們需要使用到 ElasticsearchTemplate來完成。

查看這個類的源碼

public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
    ...
}

可以看到,ElasticsearchTemplate 實現了接口 ApplicationContextAware,所以這個類是被 Spring 管理的,可以在類裏面直接註入使用。

代碼如下:

@Slf4j
@Component
public class HighlightBookRepositoryTest extends EsSearchApplicationTests {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Resource
    private ExtResultMapper extResultMapper;

    @Test
    public void testHighlightQuery() {
        BookQuery query = new BookQuery();
        query.setQueryString("穿越");

        // 復合查詢
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        // 以下為查詢條件, 使用 must query 進行查詢組合
        MultiMatchQueryBuilder matchQuery = QueryBuilders.multiMatchQuery(query.getQueryString(), "name", "intro", "author");
        boolQuery.must(matchQuery);

        PageRequest pageRequest = PageRequest.of(query.getPage() - 1, query.getSize());

        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQuery)
                .withHighlightFields(
                        new HighlightBuilder.Field("name").preTags("<span style=\"color:red\">").postTags("</span>"),
                        new HighlightBuilder.Field("author").preTags("<span style=\"color:red\">").postTags("</span>"))
                .withPageable(pageRequest)
                .build();
        Page<Book> books = elasticsearchTemplate.queryForPage(searchQuery, Book.class, extResultMapper);

        books.forEach(e -> log.info("{}", e));
        // <span style="color:red">穿越</span>小道人
    }
}

註意這裏 的

 Page<Book> books = elasticsearchTemplate.queryForPage(searchQuery, Book.class, extResultMapper);

這裏返回的是分頁對象。
查詢方式和上文的差不多,只不過是是 Repository 變成了 ElasticsearchTemplate,操作方式也大同小異。

這裏用到了 ExtResultMapper,請接著看下文。

自定義ResultMapper

ResultMapper 是用於將 ES 文檔轉換成 Java 對象的映射類,因為 SpringDataElasticSearch 默認的的映射類 DefaultResultMapper 不支持高亮,因此,我們需要自己定義一個 ResultMapper。

復制 DefaultResultMapper 類,重命名為 ExtResultMapper,對構造方法名稱修改為正確的值。

新增一個方法,用於將高亮的內容賦值給需要轉換的 Java 對象內。

在 mapResults 方法內調用這個方法。

註意:這個類可以直接拷貝到你的項目中直接使用!
我寫這麽多,只是想說明為什麽這個類是這樣的。

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.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
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 org.springframework.data.elasticsearch.core.AbstractResultMapper;
import org.springframework.data.elasticsearch.core.DefaultEntityMapper;
import org.springframework.data.elasticsearch.core.EntityMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.util.*;

/**
 * 類名稱:ExtResultMapper
 * 類描述:自定義結果映射類
 * 創建人:WeJan
 * 創建時間:2018-09-13 20:47
 */
@Component
public class ExtResultMapper extends AbstractResultMapper {

    private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;

    public ExtResultMapper() {
        super(new DefaultEntityMapper());
    }

    public ExtResultMapper(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
        super(new DefaultEntityMapper());
        this.mappingContext = mappingContext;
    }

    public ExtResultMapper(EntityMapper entityMapper) {
        super(entityMapper);
    }

    public ExtResultMapper(
            MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
            EntityMapper entityMapper) {
        super(entityMapper);
        this.mappingContext = mappingContext;
    }

    @Override
    public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
        long totalHits = response.getHits().totalHits();
        List<T> results = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            if (hit != null) {
                T result = null;
                if (StringUtils.hasText(hit.sourceAsString())) {
                    result = mapEntity(hit.sourceAsString(), clazz);
                } else {
                    result = mapEntity(hit.getFields().values(), clazz);
                }
                setPersistentEntityId(result, hit.getId(), clazz);
                setPersistentEntityVersion(result, hit.getVersion(), clazz);
                populateScriptFields(result, hit);
               
               // 高亮查詢
                populateHighLightedFields(result, hit.getHighlightFields());
                results.add(result);
            }
        }

        return new AggregatedPageImpl<T>(results, pageable, totalHits, response.getAggregations(), response.getScrollId());
    }

    private <T>  void populateHighLightedFields(T result, Map<String, HighlightField> highlightFields) {
        for (HighlightField field : highlightFields.values()) {
            try {
                PropertyUtils.setProperty(result, field.getName(), concat(field.fragments()));
            } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
                throw new ElasticsearchException("failed to set highlighted value for field: " + field.getName()
                        + " with value: " + Arrays.toString(field.getFragments()), e);
            }
        }
    }

    private String concat(Text[] texts) {
        StringBuffer sb = new StringBuffer();
        for (Text text : texts) {
            sb.append(text.toString());
        }
        return sb.toString();
    }

    private <T> void populateScriptFields(T result, SearchHit hit) {
        if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) {
            for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) {
                ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
                if (scriptedField != null) {
                    String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
                    SearchHitField searchHitField = hit.getFields().get(name);
                    if (searchHitField != null) {
                        field.setAccessible(true);
                        try {
                            field.set(result, searchHitField.getValue());
                        } catch (IllegalArgumentException e) {
                            throw new ElasticsearchException("failed to set scripted field: " + name + " with value: "
                                    + searchHitField.getValue(), e);
                        } catch (IllegalAccessException e) {
                            throw new ElasticsearchException("failed to access scripted field: " + name, e);
                        }
                    }
                }
            }
        }
    }

    private <T> T mapEntity(Collection<SearchHitField> values, Class<T> clazz) {
        return mapEntity(buildJSONFromFields(values), clazz);
    }

    private String buildJSONFromFields(Collection<SearchHitField> values) {
        JsonFactory nodeFactory = new JsonFactory();
        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
            generator.writeStartObject();
            for (SearchHitField value : values) {
                if (value.getValues().size() > 1) {
                    generator.writeArrayFieldStart(value.getName());
                    for (Object val : value.getValues()) {
                        generator.writeObject(val);
                    }
                    generator.writeEndArray();
                } else {
                    generator.writeObjectField(value.getName(), value.getValue());
                }
            }
            generator.writeEndObject();
            generator.flush();
            return new String(stream.toByteArray(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public <T> T mapResult(GetResponse response, Class<T> clazz) {
        T result = mapEntity(response.getSourceAsString(), clazz);
        if (result != null) {
            setPersistentEntityId(result, response.getId(), clazz);
            setPersistentEntityVersion(result, response.getVersion(), clazz);
        }
        return result;
    }

    @Override
    public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
        LinkedList<T> list = new LinkedList<>();
        for (MultiGetItemResponse response : responses.getResponses()) {
            if (!response.isFailed() && response.getResponse().isExists()) {
                T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
                setPersistentEntityId(result, response.getResponse().getId(), clazz);
                setPersistentEntityVersion(result, response.getResponse().getVersion(), clazz);
                list.add(result);
            }
        }
        return list;
    }

    private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {

        if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {

            ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
            ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();

            // Only deal with String because ES generated Ids are strings !
            if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
                persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
            }

        }
    }

    private <T> void setPersistentEntityVersion(T result, long version, Class<T> clazz) {
        if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {

            ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
            ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();

            // Only deal with Long because ES versions are longs !
            if (versionProperty != null && versionProperty.getType().isAssignableFrom(Long.class)) {
                // check that a version was actually returned in the response, -1 would indicate that
                // a search didn‘t request the version ids in the response, which would be an issue
                Assert.isTrue(version != -1, "Version in response is -1");
                persistentEntity.getPropertyAccessor(result).setProperty(versionProperty, version);
            }
        }
    }
}

註意這裏使用到了 PropertyUtils ,需要引入一個 Apache 的依賴。

<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
</dependency>

自定義 ResultMapper 寫好之後,添加 @Component 註解,表示為 Spring 的一個組件,在類中進行註入使用即可。

最後

本文示例項目地址:https://github.com/Mosiki/SpringDataElasticSearchQuickStartExample

有疑問?

歡迎來信,給我寫信

和我一起打造個簡單搜索之SpringDataElasticSearch關鍵詞高亮