elasticsearch-java-api 8.10.X使用示例

发布时间:2023年12月28日

1.获取??ElasticsearchClient 实例? 具体操作参考本人之前的文章?spring boot3.2 集成 es 8.x 版本工具类 支持认证与非认证的方式( jdk21)-CSDN博客

2.Query DSL 使用样例如下

 public void testhttps() {

        String host = "17X.20.5X.XXX";

        int port = XXX;

        String login = "xxx";

        String password = "xxx@123";

        // Create the transport and the API client
        EsRestClientUtil esRestClientUtil = EsRestClientUtil.getInstance();
        try (esRestClientUtil) {
            var esClient = esRestClientUtil.getElasticsearchHttpClient(host, port, login, password);
            JSONObject condition = JSONObject.of("xm.keyword", "张柳");
            JSONObject condition1 = JSONObject.of("gmsfhm.keyword", "610102190012120016");
            List<JSONObject> esCondition = new ArrayList<>();
            List<Query> queries = new ArrayList<>();
            condition1.forEach((k, v) -> {
                Query byName = MatchQuery.of(m -> m
                        .field(k)
                        .query(v.toString())
                )._toQuery();
                queries.add(byName);
            });
            // 模糊查询  支持如下四种格式
            // Changing a character (box → fox)
            //Removing a character (black → lack)
            //Inserting a character (sic → sick)
            //Transposing two adjacent characters (act → cat)
            Query fquzzy = FuzzyQuery.of(fq-> fq.field("user").value("suer1").fuzziness("AUTO"))._toQuery();
            // 年龄段查询示例
            Query rksj = RangeQuery.of(mq -> mq.field("age").gt(JsonData.of("18")).lte(JsonData.of("40")))._toQuery();
            Query rksj1 = RangeQuery.of(mq -> mq.field("rksj").gt(JsonData.of("2020-10-15 14:28:10")).lte(JsonData.of("2022-10-15 14:38:14")).format("yyyy-MM-dd HH:mm:ss"))._toQuery();
            // text 类型的时间范围查询示例
            Query rksj2 = RangeQuery.of(mq -> mq.field("gxsj.keyword").gt(JsonData.of("2020-10-15 14:28:10")).lte(JsonData.of("2022-10-15 14:38:14")))._toQuery();
            // termQuery 写法    类比SQl  =
            Query termQuery = TermQuery.of(mq -> mq.field("xm.keyword").value("王五"))._toQuery();
            // TermsQuery 写法  类比SQL  in
            List<FieldValue> fieldValues = new ArrayList<>();
            fieldValues.add(FieldValue.of("张柳"));
            fieldValues.add(FieldValue.of("王五"));
            Query query = TermsQuery.of(mq -> mq.field("xm.keyword").terms(ts -> ts.value(fieldValues)))._toQuery();
            Query query1 = TermsQuery.of(mq -> mq.field("xm.keyword").terms(ts -> ts.value(List.of(FieldValue.of("张柳"),FieldValue.of("王五")))))._toQuery();
            queries.add(termQuery);
            SearchResponse<JSONObject> response = esClient.search(es -> es
                            .index("ads_tag_rk_dd")
                            .query(q -> q
                                    .bool(b -> b
//                                            .should(s-> s.bool(b1 -> b1.must(b2->b2.bool(s2->s2.should(queries))).must(termQuery)))
                                            .must(fquzzy)
//                                            .must(rksj1)
//                                            .must(rksj2)
//                                            .must(termQuery)
//                                            .


                                    )

                            )
                            // 排序写法
                            .sort(SortOptions.of(so->so.field(FieldSort.of(fs -> fs.field("rksj").order(SortOrder.Desc)))))
                            .from(0)
                            .size(3),
                    JSONObject.class);
            List<Hit<JSONObject>> hits = response.hits().hits();
            for (Hit<JSONObject> hit : hits) {
                JSONObject product = hit.source();
                esCondition.add(product);
            }
            log.info("数据为{}",esCondition);
            log.info("总数为{}",response.hits().total().value());
        } catch (Exception e) {e.printStackTrace();
        }

    }

文章来源:https://blog.csdn.net/m0_37939214/article/details/135275427
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。