JavaRestClient操作Elasticsearch查询所有(match_all)

导包

导包可以根据 文档 里导入依赖
这里就不写依赖了

查询所有

import com.google.gson.Gson;
import com.leyou.pojo.Item;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EsdemofindAll {

    private RestHighLevelClient client;

    private Gson gson = new Gson();
    /**
     * 先执行
     */
    @Before
    public void init (){
        // 初始化HighLevel客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        HttpHost.create("http://127.0.0.1:9201"),
                        HttpHost.create("http://127.0.0.1:9202"),
                        HttpHost.create("http://127.0.0.1:9203")
                )
        );
    }

    /**
     * 查询全部
     */
    @Test
    public void findAll() throws IOException {
        // 创建搜索对象
        SearchRequest searchRequest = new SearchRequest();
        // 查询构建工具
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 添加查询条件,通过QueryBuilders获取各种查询
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        // 搜索
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        // 解析
        SearchHits hits = search.getHits();
        SearchHit[] hits1 = hits.getHits();
        for (SearchHit hit : hits1) {
            // 取出source数据
            String itemstring = hit.getSourceAsString();
            // 反序列化
            Item item = gson.fromJson(itemstring, Item.class);
            System.out.println("item ="+item);
        }
    }

    /**
     * 后执行
     */
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

打印结果

item =Item(id=2, title=坚果手机R1, category=手机, brand=锤子, price=3699.0, images=http://image.csdn.com/13123.jpg)
item =Item(id=4, title=小米Mix2S, category=手机, brand=小米, price=4299.0, images=http://image.csdn.com/13123.jpg)
item =Item(id=5, title=荣耀V10, category=手机, brand=华为, price=2799.0, images=http://image.csdn.com/13123.jpg)
item =Item(id=1, title=小米手机7, category=手机, brand=小米, price=3299.0, images=http://image.csdn.com/13123.jpg)
item =Item(id=3, title=华为META10, category=手机, brand=华为, price=4499.0, images=http://image.csdn.com/13123.jpg)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值