Java封装Elasticsearch8常用接口方法(三)

上一篇 Java封装Elasticsearch8常用接口方法(二)

3.1 查询方法

    /**
     * 查询全部数据
     *
     * @param indices
     * @return Object
     * @throws Exception
     */
    public Object advancedQueryFromAllData(String indices) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> obj = client.search(e -> e.index(indices).query(q -> q.matchAll(m -> m)),
                Object.class);
        HitsMetadata<Object> hits = obj.hits();
        TotalHits total = hits.total();
        List<Hit<Object>> hits1 = hits.hits();
        Object source = hits1.get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.1.1 term匹配

/**
     * term匹配 多次匹配
     *
     * @param index
     * @param field
     * @param fieldValues
     * @return Object
     * @throws Exception
     */
    public Object advancedQueryByTerm(String index, String field,  List<FieldValue> fieldValues) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> user_test = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .terms(t -> t
                                        .field("field")
                                        .terms(terms -> terms
                                                .value(fieldValues)
                                        )
                                )
                        )
                        .query(q -> q
                                .matchAll(m -> m))
                , Object.class);
        HitsMetadata<Object> hits = user_test.hits();
        TotalHits total = hits.total();
        List<Hit<Object>> hits1 = hits.hits();
        Object source = hits1.get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

    /**
     * term匹配 单次匹配
     *
     * @param index
     * @param field
     * @param value
     * @return Object
     * @throws Exception
     */
    public Object advancedQueryByTerm(String index, String field, long value) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> user_test = client.search(e -> e
                        .index(index)
                        .query(q -> q.term(t -> t.field(field).value(value)))
                        .query(q -> q
                                .matchAll(m -> m))
                , Object.class);
        HitsMetadata<Object> hits = user_test.hits();
        TotalHits total = hits.total();
        List<Hit<Object>> hits1 = hits.hits();
        Object source = hits1.get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.1.2 分页查询

    /**
     * 分页查询
     *
     * @param index
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object advancedQueryByPage(String index, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .matchAll(m -> m)
                        )
                        .from(from)
                        .size(size)
                , Object.class);
//        HitsMetadata<Object> hits = searchResponse.hits();
//        List<Hit<Object>> hits1 = hits.hits();
//        Object source = hits1.get(0).source();
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;

    }

3.1.3 分页查询

   /**
     * 排序查询
     *
     * @param index
     * @param field
     * @param order SortOrder.Desc/SortOrder.Asc
     * @return
     * @throws Exception
     */
    public Object advancedQueryBySort(String index, String field, SortOrder order) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .matchAll(m -> m)
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(field)
                                        .order(order)
                                )
                        )
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.1.4 排序

    /**
     * 排序查询
     *
     * @param index
     * @param field
     * @param order SortOrder.Desc/SortOrder.Asc
     * @return
     * @throws Exception
     */
    public Object advancedQueryBySort(String index, String field, SortOrder order) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .matchAll(m -> m)
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(field)
                                        .order(order)
                                )
                        )
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.1.5 must should匹配

    /**
     * must should匹配
     *
     * @param matchQueryBuilderList
     * @param indices
     * @return
     * @throws Exception
     */
    /**
     * @param index
     * @param field
     * @param order
     * @return
     * @throws Exception
     */
    public Object advancedQueryByShould(String index, String field, SortOrder order) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .bool(b -> b
                                        .must(must -> must
                                                .match(m -> m
                                                        .field("age")
                                                        .query(30)
                                                )
                                        )
                                        .must(must -> must
                                                .match(m -> m
                                                        .field("sex")
                                                        .query("男")
                                                )
                                        )
                                        .should(should -> should
                                                .match(m -> m
                                                        .field("age")
                                                        .query(30)
                                                )
                                        )
                                        .should(should -> should
                                                .match(m -> m
                                                        .field("age")
                                                        .query(40)
                                                )
                                        )
                                )
                        )
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.1.6 filter过滤

    /**
     * filter过滤查询
     *
     * @param index
     * @param field
     * @param includes includes代表白名单,只返回指定的字段
     * @param excludes excludes代表黑名单,不返回指定的字段
     * @param order
     * @return
     * @throws Exception
     */
    public Object advancedQueryByFilter(String index, String field, String includes, String excludes, SortOrder order) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .matchAll(m -> m)
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(field)
                                        .order(order)
                                )
                        )
                        .source(source -> source
                                .filter(f -> f
                                        .includes(includes)
                                        .excludes(excludes)
                                )
                        )
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.1.7 模糊查询

    /**
     * 模糊查询
     *
     * @param index
     * @param field
     * @param value
     * @param fuzziness fuzziness代表可以与关键词有误差的字数,可选值为0、1、2这三项
     * @return
     * @throws Exception
     */
    public Object advancedQueryByLike(String index, String field, String value, String fuzziness) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .fuzzy(f -> f
                                        .field(field)
                                        .value(value)
                                        .fuzziness(fuzziness))
                        )
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.1.8 高亮查询

    /**
     * 高亮 默认黄色
     *
     * @param index
     * @param field
     * @param value
     * @return
     * @throws Exception
     */
    public Object advancedQueryByHighLight(String index, String field, String value) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .term(t -> t
                                        .field(field)
                                        .value(value)
                                )
                        )
                        .highlight(h -> h
                                .fields(field, f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                , Object.class);

        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

    /**
     * 高亮 自定义颜色
     *
     * @param index
     * @param field
     * @param value
     * @param color
     * @return
     * @throws Exception
     */
    public Object advancedQueryByHighLight(String index, String field, String value, Color color) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .term(t -> t
                                        .field(field)
                                        .value(value)
                                )
                        )
                        .highlight(h -> h
                                .fields(field, f -> f
                                        .preTags("<font color='" + color + "'>")
                                        .postTags("</font>")
                                )
                        )
                , Object.class);

        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }
    
    /**
     * 自定义高亮
     *
     * @param index
     * @param field
     * @param value
     * @param highlightMap
     * @return
     * @throws Exception
     */
    public Object advancedQueryByHighLight(String index, String field, String value, Map<String, HighlightField> highlightMap) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .query(q -> q
                                .term(t -> t
                                        .field(field)
                                        .value(value)
                                )
                        )
                        .highlight(h -> h
                                .fields(highlightMap)
                        )
                , Object.class);

        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.1.9 极值聚合查询

    /**
     * 极值聚合查询
     *
     * @param index
     * @param field
     * @param maxField
     * @param highlightMap
     * @return
     * @throws Exception
     */
    public Object advancedQueryByMax(String index, String field, String maxField, Map<String, HighlightField> highlightMap) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .aggregations(maxField, a -> a
                                .max(m -> m
                                        .field(field)
                                )
                        )
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        for (Map.Entry<String, Aggregate> entry : searchResponse.aggregations().entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue().max().value());
        }
        return source;
    }

3.1.10 分组查询

    /**
     * 分组查询
     *
     * @param index
     * @param field
     * @return
     * @throws Exception
     */
    public Aggregate searchStationLineChart(String index, String field) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(index)
                        .size(100)
                        .aggregations("group", a -> a
                                .terms(t -> t
                                        .field(field)
                                )
                        )
                , Object.class);
        System.out.println(searchResponse.took());
        System.out.println(searchResponse.hits().total().value());
        searchResponse.hits().hits().forEach(e -> {
            System.out.println(e.source().toString());
        });
        Aggregate aggregate = searchResponse.aggregations().get("group");
        LongTermsAggregate lterms = aggregate.lterms();
        Buckets<LongTermsBucket> buckets = lterms.buckets();
        for (LongTermsBucket b : buckets.array()) {
            System.out.println(b.key() + " : " + b.docCount());
        }

        //Object source = searchResponse.hits().hits().get(0).source();
        //ESClientPool.returnClient(client);

        return aggregate;
    }

3.2 复杂查询

根据实际业务需求用户特定场景。结合上述查询的写法,基本都加上了高亮,分页,排序等

3.2.1 term/terms匹配

单词多词匹配

    /**
     * Term匹配
     * @param indexName
     * @param field
     * @param value
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryTerm(String indexName, String field, Object value, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .term(t -> t
                                        .field(field)
                                        .value(String.valueOf(value))
                                )
                        )
                        .highlight(h -> h
                                .fields(String.valueOf(value), f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(field)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

    /**
     * Terms匹配
     * @param indexName
     * @param field
     * @param values
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryTerms(String indexName, String field, List<FieldValue> values, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .terms(t -> t
                                        .field("field")
                                        .terms(terms -> terms
                                                .value(values)
                                        )
                                )
                        )
                        .highlight(h -> h
                                .fields(field, f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(field)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.2.2 matchAll全部

    /**
     * matchAll全部
     * @param indexName
     * @param field
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryMatchAll(String indexName, String field, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .matchAll(m -> m)
                        )
                        .highlight(h -> h
                                .fields(field, f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(field)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.2.3 multiMatch多field匹配

    /**
     * multiMatch多field匹配
     * @param indexName
     * @param fields
     * @param query
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryMultiMatch(String indexName, List<String> fields, String query, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .multiMatch(m -> m
                                        .fields(fields)
                                        .query(query)
                                )
                        )
                        .highlight(h -> h
                                .fields(query, f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.2.4 matchPhrase短语匹配

    /**
     * matchPhrase短语匹配
     * @param indexName
     * @param field
     * @param query
     * @param slop 间隔字符数量
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryMatchPhrase(String indexName, String field, String query, Integer slop, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .matchPhrase(m -> m
                                        .field(field)
                                        .query(query)
                                        .slop(slop)
                                )
                        )
                        .highlight(h -> h
                                .fields(query, f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.2.5 must、mustNot、should匹配

    /**
     * must必须匹配
     * @param indexName
     * @param field
     * @param query
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryBoolMust(String indexName, String field, Object query, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .bool(bool -> bool
                                        .must(must -> must
                                                .match(match -> match
                                                        .field(field)
                                                        .query(String.valueOf(query))
                                                )
                                        )
                                )
                        )
                        .highlight(h -> h
                                .fields(String.valueOf(query), f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

    /**
     * mustNot必须不匹配
     * @param indexName
     * @param field
     * @param query
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryBoolMustNot(String indexName, String field, Object query, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .bool(bool -> bool
                                        .mustNot(mustNot -> mustNot
                                                .match(match -> match
                                                        .field(field)
                                                        .query(String.valueOf(query))
                                                )
                                        )
                                )
                        )
                        .highlight(h -> h
                                .fields(String.valueOf(query), f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

    /**
     * should选择性匹配
     * @param indexName
     * @param field
     * @param query
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryBoolShould(String indexName, String field, Object query, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .bool(bool -> bool
                                        .should(should -> should
                                                .match(match -> match
                                                        .field(field)
                                                        .query(String.valueOf(query))
                                                )
                                        )
                                )
                        )
                        .highlight(h -> h
                                .fields(String.valueOf(query), f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.2.6 filter过滤子句,必须匹配

    /**
     * filter过滤子句,必须匹配
     * @param indexName
     * @param field
     * @param query
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryBoolFilter(String indexName, String field, Object query, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .bool(bool -> bool
                                        .filter(filter -> filter
                                                .match(match -> match
                                                        .field(field)
                                                        .query(String.valueOf(query))
                                                )
                                        )
                                )
                        )
                        .highlight(h -> h
                                .fields(String.valueOf(query), f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.2.7 Range范围匹配

    /**
     * Range范围匹配
     * @param indexName
     * @param field
     * @param gt 大于
     * @param lt 小于
     * @param gte 大于等于
     * @param lte 小于等于
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryBoolRange(String indexName, String field, Object gt, Object lt, Object gte, Object lte, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .bool(b -> b
                                        .filter(f -> f
                                                .range(r -> r
                                                        .field(field)
                                                        .gt((Objects.isNull(gt) ? null : JsonData.of(gt)))
                                                        .lt((Objects.isNull(lt) ? null : JsonData.of(lt)))
                                                        .gte((Objects.isNull(gte) ? null : JsonData.of(gte)))
                                                        .lte((Objects.isNull(lte) ? null : JsonData.of(lte)))
                                                )
                                        )
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.2.8 filter过滤

    /**
     * filter过滤
     * @param indexName
     * @param excludes
     * @param includes
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryFilter(String indexName, List<String> excludes, List<String> includes, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .matchAll(m -> m)
                        )
                        .source(s -> s
                                .filter(f -> f
                                        .excludes(excludes)
                                        .includes(includes)
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.2.9 fuzzy模糊匹配

    /**
     * fuzzy模糊匹配
     * @param indexName
     * @param field
     * @param value
     * @param fuzziness
     * @param sortField
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryFuzzy(String indexName, String field, String value, String fuzziness, String sortField, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(q -> q
                                .fuzzy(f -> f
                                        .field(field)
                                        .value(value)
                                        .fuzziness(fuzziness))
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(sortField)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

3.3 原生查询

两种原生的写法,查询方式可以通过参数传递

    /**
     * SearchRequest原生查询
     * @param searchRequest
     * @return
     * @throws Exception
     */
    public Object queryOriginal(SearchRequest searchRequest) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        //Query of = Query.of(q -> q.bool(b -> b.must(m -> m.match(match -> match.field("").query("")))));
        SearchResponse<Object> searchResponse = client.search(searchRequest, Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

    /**
     * Query原生查询
     * @param indexName
     * @param field
     * @param query
     * @param order
     * @param from
     * @param size
     * @return
     * @throws Exception
     */
    public Object queryOriginal(String indexName, String field, Query query, SortOrder order, Integer from, Integer size) throws Exception {
        ElasticsearchClient client = ESClientPool.getClient();
        //Query of = Query.of(q -> q.bool(b -> b.must(m -> m.match(match -> match.field("").query("")))));
        SearchResponse<Object> searchResponse = client.search(e -> e
                        .index(indexName)
                        .query(query)
                        .highlight(h -> h
                                .fields(field, f -> f
                                        .preTags("<font color='yellow'>")
                                        .postTags("</font>")
                                )
                        )
                        .sort(sort -> sort
                                .field(f -> f
                                        .field(field)
                                        .order(order)
                                )
                        )
                        .from(from)
                        .size(size)
                , Object.class);
        Object source = searchResponse.hits().hits().get(0).source();
        ESClientPool.returnClient(client);
        return source;
    }

无了~~~~
有错误、有问题大家交流呀~

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐