java的elasticsearch做高亮显示

2021-03-04 07:26

阅读:509

标签:ext   asm   mapper   int   设置   tostring   catch   gre   div   

public SearchResult search(String keyWord, Integer page) {
        PageRequest pageRequest = PageRequest.of(page - 1, ROWS); //设置分页参数

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.multiMatchQuery(keyWord, "title", "title.pinyin"
                ).operator(Operator.AND)) // match查询
                .withPageable(pageRequest)
                .withHighlightFields(new HighlightBuilder.Field("title")) // 设置高亮
                .build();

        AggregatedPage housePage =
                this.elasticsearchTemplate.queryForPage(searchQuery,
                        HouseData.class, new SearchResultMapper() {
                            @Override
                            public  AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) {

                                if (response.getHits().totalHits == 0) {
                                    return new AggregatedPageImpl(Collections.emptyList(), pageable, 0L);
                                }

                                List list = new ArrayList();
                                for (SearchHit searchHit : response.getHits().getHits()) {
                                    T obj = (T) ReflectUtils.newInstance(clazz);

                                    try {
                                        FieldUtils.writeField(obj, "id", searchHit.getId(), true);
                                    } catch (IllegalAccessException e) {
                                        e.printStackTrace();
                                    }

                                    // 非高亮字段的数据写入
                                    for (Map.Entry entry : searchHit.getSourceAsMap().entrySet()) {

                                        Field field = FieldUtils.getField(clazz, entry.getKey(), true);
                                        if (null == field) {
                                            continue;
                                        }

                                        try {
                                            FieldUtils.writeField(obj, entry.getKey(), entry.getValue(), true);
                                        } catch (IllegalAccessException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    for (Map.Entry entry : searchHit.getHighlightFields().entrySet()) {
                                        StringBuilder sb = new StringBuilder();
                                        Text[] fragments = entry.getValue().getFragments();
                                        for (Text fragment : fragments) {
                                            sb.append(fragment.toString());
                                        }

                                        // 写入高亮的内容
                                        try {
                                            FieldUtils.writeField(obj, entry.getKey(), sb.toString(), true);
                                        } catch (IllegalAccessException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    list.add(obj);
                                }

                                return new AggregatedPageImpl(list, pageable, response.getHits().totalHits);
                            }
                        });

        return new SearchResult(housePage.getTotalPages(), housePage.getContent(), null);

    }

 

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "haoke", type = "house", createIndex = false)
public class HouseData {

    @Id
    private String id;
    private String title;
    private String rent;
    private String floor;
    private String image;
    private String orientation;
    private String houseType;
    private String rentMethod;
    private String time;

}

 

java的elasticsearch做高亮显示

标签:ext   asm   mapper   int   设置   tostring   catch   gre   div   

原文地址:https://www.cnblogs.com/zhian/p/14368652.html


评论


亲,登录后才可以留言!