基于spring-boot-starter-data-elasticsearch整合分页和高亮查询

  1. spring-data3.1.x整合elasticsearch只支持到6.x,如果配置连接7.x
    则会报如下错误

     org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{m7cqAfz-R_WtXreSiJp6GQ}{192.168.37.100}{192.168.37.100:9300}]
    

配置环境

1.pom文件配置

这里使用的2.1.6.RELEASE的springboot,对应3.1.9.RELEASE的spring-data-elasticsearch

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2.yml配置文件配置

spring:
  data:
    elasticsearch:
        repositories:
            enabled: true
        cluster-name: elasticsearch
        cluster-nodes: localhost:9300

3.构建elasticsearch实体类

@Data
@Document(indexName = "query", type = "people_info")
public class EsQueryBean implements Serializable {
    @Id
    @Field(type = FieldType.Keyword, store = true)
    private String id;
    @Field(index = true, type = FieldType.Text, analyzer = "ik_max_word")
    private String content;
    /**
     * 身份证号
     */
    @Field(index = true, type = FieldType.Keyword)
    private String idCard;
    /**
     * 年龄
     */
    @Field(index = true, type = FieldType.Integer)
    private Integer age;
    /**
     * 性别
     */
    @Field(index = true, type = FieldType.Keyword)
    private String sex;
    /**
     * 姓名
     */
    @Field(index = true, type = FieldType.Text, analyzer = "ik_max_word")
    private String name;
}

3.创建elasticsearch查询条件辅助类
查询条件BoTermQuery生成的完整查询结构:

{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {}
                },
                {
                    "terms": {}
                },
                {
                    "match": {}
                },
                {
                    "multi_match": {
                        "fields": []
                    }
                },
                {
                    "range": {}
                }
            ],
            "should": [
                {
                    "term": {}
                },
                {
                    "terms": {}
                },
                {
                    "match": {}
                },
                {
                    "multi_match": {
                        "fields": []
                    }
                },
                {
                    "range": {}
                }
            ]
        }
    }
}

过滤条件类

@Data
public class BoTermQuery {
    /**
     * 简单条件
     */
    private List<Pair<String, ?>> termConditions = new ArrayList<>();
    /**
     * 范围条件
     */
    private Map<String, Pair<?, ?>> rangeConditions = new HashMap<>();
    /**
     * 查询条件
     */
    private List<Pair<?, String>> matchConditions = new ArrayList<>();
    /**
     * should 条件
     */
    private Should should;

    private BoTermQuery() {

    }

    /**
     * 添加简单条件
     *
     * @param field
     * @param condition list或者单个数据
     * @return
     */
    public BoTermQuery addTermCondition(String field, Object condition) {
        Pair<String, Object> pair = new Pair<>(field, condition);
        termConditions.add(pair);
        return this;
    }

    /**
     * 添加简单条件
     *
     * @param field
     * @param condition list或者单个数据
     * @return
     */
    public BoTermQuery addTermCondition(String field, Collection<?> condition) {
        Pair<String, Object> pair = new Pair<>(field, condition);
        termConditions.add(pair);
        return this;
    }

    /**
     * 一个值匹配多个字段
     *
     * @param condition
     * @param fields
     * @return
     */
    public BoTermQuery addMatchCondition(String condition, String... fields) {
        Pair<String[], String> pair = new Pair<>(fields, condition);
        matchConditions.add(pair);
        return this;
    }

    /**
     * 一个值匹配一个字段
     *
     * @param condition
     * @param field
     * @return
     */
    public BoTermQuery addMatchCondition(String field,String condition) {
        Pair<?, String> pair = new Pair<>(field, condition);
        matchConditions.add(pair);
        return this;
    }

    /**
     * 添加范围条件
     *
     * @param field
     * @param min
     * @param max
     * @return
     */
    public BoTermQuery addRangeCondition(String field, Object min, Object max) {
        Pair<Object, Object> pair = new Pair<>(min, max);
        rangeConditions.put(field, pair);
        return this;
    }

    public BoTermQuery addShouldCondition(Should should) {
        this.should = should;
        return this;
    }

    public static BoTermQuery create() {
        return new BoTermQuery();
    }

    @Data
    public static class Should {
        /**
         * 最少匹配的个数
         */
        private int minimumNumberShouldMatch = 1;
        /**
         * 简单条件
         */
        private List<Pair<String, ?>> termConditions = new ArrayList<>();
        /**
         * 范围条件
         */
        private Map<String, Pair<?, ?>> rangeConditions = new HashMap<>();
        /**
         * 查询条件
         * key是field
         * value是值
         */
        private List<Pair<?, String>> matchConditions = new ArrayList<>();

        private Should(int minimumNumberShouldMatch) {
            this.minimumNumberShouldMatch = minimumNumberShouldMatch;
        }

        /**
         * 添加简单条件
         *
         * @param field
         * @param condition
         * @return
         */
        public Should addTermCondition(String field, Object condition) {
            Pair<String, Object> pair = new Pair<>(field, condition);
            termConditions.add(pair);
            return BoTermQuery.Should.this;
        }

        /**
         * 添加简单条件
         *
         * @param field
         * @param condition
         * @return
         */
        public Should addTermCondition(String field, Collection<?> condition) {
            Pair<String, Object> pair = new Pair<>(field, condition);
            termConditions.add(pair);
            return BoTermQuery.Should.this;
        }

        /**
         * 一个值匹配多个字段
         *
         * @param condition
         * @param fields
         * @return
         */
        public Should addMatchCondition(String condition, String... fields) {
            Pair<String[], String> pair = new Pair<>(fields, condition);
            matchConditions.add(pair);
            return this;
        }

        /**
         * 一个值匹配一个字段
         *
         * @param condition
         * @param field
         * @return
         */
        public Should addMatchCondition(String field,String condition) {
            Pair<?, String> pair = new Pair<>(field, condition);
            matchConditions.add(pair);
            return this;
        }

        /**
         * 添加范围条件
         *
         * @param field
         * @param min
         * @param max
         * @return
         */
        public Should addRangeCondition(String field, Object min, Object max) {
            Pair<Object, Object> pair = new Pair<>(min, max);
            rangeConditions.put(field, pair);
            return this;
        }

        public static Should create(int minimumNumberShouldMatch) {
            return new Should(minimumNumberShouldMatch);
        }

    }
}

4.分页信息类

@Data
public class PageParam {
    private int pageNum;
    private int pageSize = 10;
}   

5.创建elasticsearch查询service类

public interface EsSearchService<T> {
    /**
     * 创建索引
     *
     * @param tClass
     * @return
     */
    boolean createIndex(Class<T> tClass);

    /**
     * 创建索引映射
     *
     * @param tClass
     * @return
     */
    boolean putMapping(Class<T> tClass);


    /**
     * 判断一个索引是否存在
     *
     * @param tClass
     * @return
     */
    boolean existIndex(Class<T> tClass);

    /**
     * 删除索引
     *
     * @param tClass
     * @return
     */
    boolean deleteIndex(Class<T> tClass);

    /**
     * 分页请求
     *
     * @param pageParam   分页参数
     * @param boTermQuery es查询对象封装
     * @param tClass      索引类
     * @return 文档对象集合
     */
    List<T> queryList(PageParam pageParam, BoTermQuery boTermQuery, Class<T> tClass);

    /**
     * 查询高亮的分页请求
     *
     * @param pageParam       分页参数
     * @param boTermQuery     es查询对象封装
     * @param highlightFields 需要高亮的字段集合
     * @param indexName       索引名称
     * @param tClass          索引类
     * @return 文档对象集合
     */
    List<T> queryListHighlight(PageParam pageParam, BoTermQuery boTermQuery, List<String> highlightFields,
                               String indexName, Class<T> tClass);
}

实现类

@Service
public abstract class EsSearchServiceImpl<T> implements EsSearchService<T> {
    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public boolean createIndex(Class<T> tClass) {
        return elasticsearchTemplate.createIndex(tClass);
    }

    @Override
    public boolean putMapping(Class<T> tClass) {
        return elasticsearchTemplate.putMapping(tClass);
    }


    @Override
    public boolean existIndex(Class<T> tClass) {
        return elasticsearchTemplate.indexExists(tClass);
    }

    @Override
    public boolean deleteIndex(Class<T> tClass) {
        return elasticsearchTemplate.deleteIndex(tClass);
    }

    /**
     * @param pageParam
     * @param boTermQuery
     * @param tClass
     * @return
     */
    @Override
    public List<T> queryList(PageParam pageParam, BoTermQuery boTermQuery, Class<T> tClass) {
        Pageable pageable = PageRequest.of(pageParam.getPageNum(), pageParam.getPageSize());
        BoolQueryBuilder queryBuilder = createQueryBuilder(boTermQuery);
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withPageable(pageable)
                .build();
        return elasticsearchTemplate.queryForPage(searchQuery, tClass).getContent();
    }

    @Override
    public List<T> queryListHighlight(PageParam pageParam, BoTermQuery boTermQuery, List<String> highlightFields, String indexName, Class<T> tClass) {
        BoolQueryBuilder queryBuilder = createQueryBuilder(boTermQuery);
        HighlightBuilder highlightBuilder = createHighlightBuilder(highlightFields);
        SearchResponse searchResponse = elasticsearchTemplate.getClient().prepareSearch(indexName)
                .setQuery(queryBuilder)
                .highlighter(highlightBuilder)
                .setFrom(pageParam.getPageNum())
                .setSize(pageParam.getPageSize())
                .get();
        SearchHits searchHits = searchResponse.getHits();
        List<T> results = new ArrayList<>();
        //组装结果
        for (SearchHit searchHit : searchHits) {
            Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
            Map<String, HighlightField> highlightFieldsResult = searchHit.getHighlightFields();
            try {
                T t = tClass.newInstance();
                Field[] declaredFields = t.getClass().getDeclaredFields();
                for (Field field : declaredFields) {
                    field.setAccessible(true);
                    String name = field.getName();
                    if (highlightFieldsResult.get(name) != null) {
                        field.set(t, (highlightFieldsResult.get(name).getFragments()[0]).toString());
                    } else {
                        Object fieldValue = sourceAsMap.get(name);
                        field.set(t, fieldValue);
                    }
                }
                results.add(t);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return results;
    }

    /**
     * 构造查询器
     *
     * @param boTermQuery
     * @return
     */
    private BoolQueryBuilder createQueryBuilder(BoTermQuery boTermQuery) {
        List<Pair<String, ?>> termConditions = boTermQuery.getTermConditions();
        Map<String, Pair<?, ?>> rangeConditions = boTermQuery.getRangeConditions();
        List<Pair<?, String>> matchConditions = boTermQuery.getMatchConditions();

        BoTermQuery.Should should = boTermQuery.getShould();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (should != null) {
            boolQueryBuilder.minimumShouldMatch(should.getMinimumNumberShouldMatch());
            //should条件
            List<Pair<String, ?>> shouldTermConditions = should.getTermConditions();
            List<Pair<?, String>> shouldMatchConditions = should.getMatchConditions();
            Map<String, Pair<?, ?>> shouldRangeConditions = should.getRangeConditions();

            if (shouldTermConditions.size() > 0) {
                for (Pair<String, ?> shouldTermPair : shouldTermConditions) {
                    if (shouldTermPair.getValue() instanceof Collection) {
                        Collection<?> formatTermPair = (Collection<?>) shouldTermPair.getValue();
                        TermsQueryBuilder termsShouldQueryBuilder = QueryBuilders.termsQuery(shouldTermPair.getKey(), formatTermPair);
                        boolQueryBuilder.should(termsShouldQueryBuilder);
                    } else {
                        TermQueryBuilder termShouldQueryBuilder = QueryBuilders.termQuery(shouldTermPair.getKey(), shouldTermPair.getValue());
                        boolQueryBuilder.should(termShouldQueryBuilder);
                    }
                }
            }
            //匹配
            if (shouldMatchConditions.size() > 0) {
                for (Pair<?, String> shouldMatchPair : shouldMatchConditions) {
                    if (shouldMatchPair.getKey() instanceof String[]) {
                        String[] formatPairKey = (String[]) shouldMatchPair.getKey();
                        MultiMatchQueryBuilder multiShouldMatchQueryBuilder = QueryBuilders
                                .multiMatchQuery(shouldMatchPair.getValue(), formatPairKey)
                                .analyzer("ik_max_word");
                        boolQueryBuilder.should(multiShouldMatchQueryBuilder);
                    } else if (shouldMatchPair.getKey() instanceof String) {
                        String formatPairKey = (String) shouldMatchPair.getKey();
                        MatchQueryBuilder shouldMatchQueryBuilder = QueryBuilders
                                .matchQuery(formatPairKey, shouldMatchPair.getValue())
                                .analyzer("ik_max_word");
                        boolQueryBuilder.should(shouldMatchQueryBuilder);
                    }
                }
            }
            //范围
            if (shouldRangeConditions.size() > 0) {
                for (Map.Entry<String, Pair<?, ?>> shouldRangeEntry : shouldRangeConditions.entrySet()) {
                    RangeQueryBuilder rangeShouldQueryBuilder = QueryBuilders.rangeQuery(shouldRangeEntry.getKey());
                    rangeShouldQueryBuilder.to(shouldRangeEntry.getValue().getValue()).from(shouldRangeEntry.getValue().getKey());
                    boolQueryBuilder.should(rangeShouldQueryBuilder);
                }
            }
        }
        //过滤条件
        if (termConditions.size() > 0) {
            for (Pair<String, ?> termPair : termConditions) {
                if (termPair.getValue() instanceof Collection) {
                    Collection<?> formatTermPair = (Collection<?>) termPair.getValue();
                    TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(termPair.getKey(), formatTermPair);
                    boolQueryBuilder.filter(termsQueryBuilder);
                } else {
                    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(termPair.getKey(), termPair.getValue());
                    boolQueryBuilder.filter(termQueryBuilder);
                }
            }
        }
        //匹配条件
        if (matchConditions.size() > 0) {
            for (Pair<?, String> matchPair : matchConditions) {
                if (matchPair.getKey() instanceof String[]) {
                    String[] formatPairKey = (String[]) matchPair.getKey();
                    MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders
                            .multiMatchQuery(matchPair.getValue(), formatPairKey)
                            .analyzer("ik_max_word");
                    boolQueryBuilder.filter(multiMatchQueryBuilder);
                } else if (matchPair.getKey() instanceof String) {
                    String formatPairKey = (String) matchPair.getKey();
                    MatchQueryBuilder matchQueryBuilder = QueryBuilders
                            .matchQuery(formatPairKey, matchPair.getValue())
                            .analyzer("ik_max_word");
                    boolQueryBuilder.filter(matchQueryBuilder);
                }
            }
        }
        //range条件
        if (rangeConditions.size() > 0) {
            for (Map.Entry<String, Pair<?, ?>> rangeEntry : rangeConditions.entrySet()) {
                RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(rangeEntry.getKey());
                rangeQueryBuilder.to(rangeEntry.getValue().getValue()).from(rangeEntry.getValue().getKey());
                boolQueryBuilder.filter(rangeQueryBuilder);
            }
        }
        return boolQueryBuilder;
    }

    /**
     * 构造高亮器
     *
     * @param fieldNames
     * @return
     */
    private HighlightBuilder createHighlightBuilder(List<String> fieldNames) {
        HighlightBuilder highlightBuilder = new HighlightBuilder()
                .preTags("<span style='color:red'>")
                .postTags("</span>");
        //设置高亮字段
        for (String fieldName : fieldNames) {
            highlightBuilder.field(fieldName);
        }
        return highlightBuilder;
    }
}

6.查询参数类

@Data
public class QueryParam {
    /**
     * 搜索内容
     */
    private String queryContent;
    /**
     * 性别
     */
    private String sex;
   /**
     * 姓名
     */
    private String name;
}

7.测试类

@Service
public class QueryServiceImpl extends EsSearchServiceImpl<EsQueryBean> {

    @Override
    public List<EsQueryBean> query(PageParam pageParam, QueryParam queryParam) {
        String queryContent = queryParam.getQueryContent();
        String name = queryParam.getName();
        String sex = queryParam.getSex();

        BoTermQuery boTermQuery = BoTermQuery.create();
        BoTermQuery.Should should = BoTermQuery.Should.create(1);
        if (!StringUtils.isEmpty(queryContent)) {
            should.addMatchCondition(queryContent, "name", "content");
            boTermQuery.addShouldCondition(should);
        }
        if (!StringUtils.isEmpty(sex)) {
            boTermQuery.addTermCondition("sex", sex);
        }
        if (name != null) {
            boTermQuery.addMatchCondition("name", name);
        }
        //List<EsQueryBean> esQueryBeans = this.queryList(pageParam, boTermQuery, EsQueryBean.class);
        List<String> highlightFields = new ArrayList<>();
        highlightFields.add("content");
        List<EsQueryBean> esQueryBeans = this.queryListHighlight(pageParam, boTermQuery, highlightFields,
                "query", EsQueryBean.class);
        return esQueryBeans;

    }

    @Override
    public BaseRecord queryDetail(EsComprehensiveQueryBean esComprehensiveQueryBean) {
        String sourceTableType = esComprehensiveQueryBean.getSourceTableName();
        EnumPeopleInfoTable tableEnum = EnumPeopleInfoTable.getEnumByTableName(sourceTableType);
        if (tableEnum == null) {
            log.info("没有此人员信息资源表:" + sourceTableType);
            throw new ExceptionBizSystem(EnumResponseCode.REQUEST_INTERFACE_PEOPLE_INFO_TABLE.code, EnumResponseCode.REQUEST_INTERFACE_PEOPLE_INFO_TABLE.message);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("idCard", esComprehensiveQueryBean.getIdCard());
        Object obj = daoPeopleInfoQuery.findByParam(param, tableEnum.clazz);
        return (BaseRecord) obj;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容