基础使用es

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.util.NamedValue;
import org.example.Main;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.naming.directory.SearchResult;
import javax.naming.ldap.ExtendedRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author 陈康语
 * @Date 2024/9/16 14:21
 */
@SpringBootTest(classes = Main.class)
public class Tes1 {
    @Autowired
    private ElasticsearchClient elasticsearchClient;
    @Test
    public void e1() throws IOException {
        SearchRequest.Builder query = new SearchRequest.Builder().index("sku_info")
                .query(bl -> bl.bool(mt -> mt
                        .must(mh -> mh.match(q -> q.field("title").query("三国")))
                        .must(mh -> mh.match(q -> q.field("user_name").query("张涛")))
                        .filter(re->re.range(q->q.field("price").gt(JsonData.of(50))))
                ));

        SearchResponse<Object> search = elasticsearchClient.search(query.build(), Object.class);
        List<Hit<Object>> hits = search.hits().hits();
        for (Hit<Object> hit : hits){
            System.out.println(hit.source());
        }
    }

    /**
     * 查询
     *  1.作者是何林or张之盐
     *  2.商品的价格在10-100之间
     *  3.分页查询第0页,每页3条数据
     *  4.根据价格进行排序
     *  5.只返回title,price,user_name
     * @throws IOException
     */
    @Test
    public void e2() throws IOException {
        SearchRequest.Builder query = new SearchRequest.Builder().index("sku_info")
                .query(bl->bl.bool(sd->sd
                        .should(mh->mh.match(q->q.field("user_name").query("何林")))
                        .should(mh->mh.matchPhrase(q->q.field("user_name").query("张之盐")))
                        .filter(rg->rg.range(pe->pe.field("price").gt(JsonData.of(10)).lt(JsonData.of(100))))
                ))
                .from(0)
                .size(3)
                .sort(s->s.field(q->q.field("price").order(SortOrder.Desc)))
                .source(f->f.filter(v->v.includes("title","price","user_name")));
        SearchResponse<Object> search = elasticsearchClient.search(query.build(), Object.class);
        List<Hit<Object>> hits = search.hits().hits();
        for (Hit<Object> hit : hits){
            System.out.println(hit.source());
        }
    }

    /**
     * 根据category 进行分组查询
     * @throws IOException
     */
    @Test
    public void e3() throws IOException {
        SearchRequest.Builder query = new SearchRequest.Builder().index("sku_info");
        query.size(0)
                .aggregations("category_group",
                ts->ts.terms(fd->fd.field("category").size(2).order(NamedValue.of("_key", SortOrder.Desc))).aggregations("top_category", t->t.topHits(q->q.size(2)))
        );
        SearchResponse<Object> search = elasticsearchClient.search(query.build(), Object.class);
        //解构结果
        Map<String, Aggregate> aggregations = search.aggregations();
        Aggregate buckets = aggregations.get("category_group");
        buckets.lterms().buckets().array().forEach(b->{
            System.out.println(b.key()+"--"+b.docCount());
            b.aggregations().get("top_category").topHits().hits().hits().forEach(h->System.out.println(h.source()));

        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值