elasticsearch复杂检索,match、matchAll、matchPhrase、term、多条件查询、multiMatch多字段查询、分页查询、排序、指定查询字段查询等

前言:

简单查询请参考:https://mp.csdn.net/mp_blog/creation/editor/141529910

1.match字段查询

@Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
        // match查询
        SearchResponse<Produce> search = elasticsearchClient.search(s -> s.index("new_product")
                .query(q -> q.match(
                        m -> m.field("name").query("铁锤")
                )), Produce.class);
        search.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));

        });
    }

2.matchAll查询

    @Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
        //matchAll查询
        SearchResponse<Produce> search = elasticsearchClient.search(s->s
                .index("new_product")
                .query(q->q
                        .matchAll(m->m)),Produce.class);
        search.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));

        });
    }

3.matchPhrase短句查询

    @Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
        //matchPhrase短句查询
        SearchResponse<Produce> search = elasticsearchClient.search(s -> s.index("new_product")
                .query(q -> q.matchPhrase(
                        m->m.field("name").query("漂亮的大铁锤")
                )), Produce.class);
        search.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));
        });
    }

4.term 不分词查询

    @Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
        //term查询
        SearchResponse<Produce> search = elasticsearchClient.search(s -> s.index("new_product")
                .query(q -> q.term(t -> t.field("stock").value("666"))), Produce.class);
        System.out.printf("------");
        search.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));
        });
    }

5.多条件查询

使用bool关键字配合must,should,must_not

        must:所有条件必须同时成立

        must_not:所有条件必须同时不成立

        should:所有条件中成立一个即可

    @Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
        SearchResponse<Produce> search = elasticsearchClient.search(s -> s.index("new_product")
                .query(q -> q
                        .bool(builder -> builder
                                .must(m -> m
                                        .term(t -> t
                                                .field("name").value("漂亮的大铁锤")))
                                .must(m -> m
                                        .term(t -> t
                                                .field("intro").value("大铁锤"))))), Produce.class
        );
        search.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));
        });
    }

6.multiMatch多字段查询

Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
        // multiMatch多字段查询
        SearchResponse<Produce> new_product = elasticsearchClient.search(s -> s.index("new_product")
                .query(q->q
                        .multiMatch(m->m
                                .query("铁锤").fields("name","intro"))), Produce.class);
        new_product.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));
        });
    }

7.分页查询

from为页码

Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
        // 分页查询
        SearchResponse<Produce> new_product = elasticsearchClient.search(s -> s
                .index("new_product").query(q -> q
                        .matchAll(v -> v))
                .size(10)
                .from(0), Produce.class);
        new_product.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));
        });
    }

8. 排序

Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
SearchResponse<Produce> search = elasticsearchClient.search(s -> s
                .index("new_product")
                .query(q -> q.matchAll(v -> v))
                .sort(so -> so.field(f -> f.field("name").order(SortOrder.Asc))), Produce.class);
        search.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));
        });
    }

9.指定查询字段查询

Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
// 指定查询字段查询
        SearchResponse<Produce> search = elasticsearchClient.search(s -> s
                .index("new_product")
                .query(q -> q.matchAll(v -> v))
                .source(sou -> sou.filter(f -> f.includes("name", "intro"))), Produce.class);
        search.hits().hits().forEach(produceHit -> {
            System.out.printf(String.valueOf(produceHit.source()));
        });
    }

10.聚合查询

Test
    public void contextLoads() throws IOException {
        ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
// 指定查询字段查询
        SearchResponse<Object> search = elasticsearchClient.search(s -> s
                .index("new_product")
                .query(q -> q.matchAll(v -> v))
                .aggregations("stockAgg", a -> a.terms(t -> t.field("stock"))), Object.class);
        Aggregate stockAgg = search.aggregations().get("stockAgg");
        List<LongTermsBucket> array = stockAgg.lterms().buckets().array();
    }

 

持续更新中》〉》〉》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值