后端查询es数据

一、构建es连接

本次我们通过RestClien来连接es服务端,代码很简单,就不废话了,直接上代码:

 		HttpHost httpHost1 = new HttpHost("192.168.1.100", 8000, "http");
        HttpHost httpHost2 = new HttpHost("192.168.1.101", 8000, "http");
        HttpHost httpHost3 = new HttpHost("192.168.1.102", 8000, "http");
        HttpHost httpHost4 = new HttpHost("192.168.1.103", 8000, "http");
        HttpHost httpHost5 = new HttpHost("192.168.1.104", 8000, "http");

        // 创建es链接
        RestClientBuilder builder = RestClient.builder(httpHost1, httpHost2, httpHost3, httpHost4, httpHost5);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);

备注:以上连接的是es集群,如果连接单个es服务端,只需要创建一个HttpHost即可

二、创建查询条件

同样很简单,老规矩上代码:

		// 开始查询数据
        SearchRequest searchRequest = new SearchRequest("index-2021.06.08");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 构造查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 查询所有数据
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();

        boolQueryBuilder.must(matchAllQueryBuilder);
        searchSourceBuilder.query(boolQueryBuilder);
        // 查询10条
        searchSourceBuilder.size(10);
        searchRequest.source(searchSourceBuilder);
三、查询es数据

这个过程更简单,只需要把创建好的条件传入我们之前构建的es连接即可,代码如下:

		try {
            // 条件构造完毕,开始查询es
            SearchResponse search = restHighLevelClient.search(searchRequest);
            LOGGER.info("查询原始数据为:{}", search);

            //取出具体业务数据
            List<String> list = Arrays.asList(search.getHits().getHits())
                    .stream()
                    .map(SearchHit::getSourceAsString)
                    .collect(Collectors.toList());
            LOGGER.info("业务数据大小:{}  业务数据:{}", list.size(), list);
        } catch (Exception e) {
            LOGGER.error("es查询异常:{}", e.getMessage());
        }
四、数据验证

通过以上三个步骤我们就可以拿到我们放到es服务端的数据啦,所有不贴运行结果的代码都是耍流氓,所以请看:
验证结果
完整代码:

package com.kingmouse.es.book;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @auther kingMouse
 * @date 2021/6/8 10:31 上午
 * Description: 后端查询es服务端数据demo
 */

public class Esdemo {

    private static final Logger LOGGER = LoggerFactory.getLogger(Esdemo.class);

    public static void main(String[] args) {
        HttpHost httpHost1 = new HttpHost("192.168.1.100", 8000, "http");
        HttpHost httpHost2 = new HttpHost("192.168.1.101", 8000, "http");
        HttpHost httpHost3 = new HttpHost("192.168.1.102", 8000, "http");
        HttpHost httpHost4 = new HttpHost("192.168.1.103", 8000, "http");
        HttpHost httpHost5 = new HttpHost("192.168.1.104", 8000, "http");

        // 创建es链接
        RestClientBuilder builder = RestClient.builder(httpHost1, httpHost2, httpHost3, httpHost4, httpHost5);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);

        // 开始查询数据
        SearchRequest searchRequest = new SearchRequest("index-2021.06.08");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 构造查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 查询所有数据
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();

        boolQueryBuilder.must(matchAllQueryBuilder);
        searchSourceBuilder.query(boolQueryBuilder);
        // 查询10条
        searchSourceBuilder.size(10);
        searchRequest.source(searchSourceBuilder);

        try {
            // 条件构造完毕,开始查询es
            SearchResponse search = restHighLevelClient.search(searchRequest);
            LOGGER.info("查询原始数据为:{}", search);

            //取出具体业务数据
            List<String> list = Arrays.asList(search.getHits().getHits())
                    .stream()
                    .map(SearchHit::getSourceAsString)
                    .collect(Collectors.toList());
            LOGGER.info("业务数据大小:{}  业务数据:{}", list.size(), list);
        } catch (Exception e) {
            LOGGER.error("es查询异常:{}", e.getMessage());
        }
    }
}

什么?你要带条件的查询?ok,满足你们,上代码:

		// 开始查询数据
        SearchRequest searchRequest = new SearchRequest("index-2021.06.08");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 构造查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
      
        // 查询请求时间在2021-06-08 08:00:00~2021-06-08 09:00:00之间的数据
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("reqTime.keyword")
                .gt("2021-06-08 08:00:00").lt("2021-06-08 09:00:00");
        // 查询状态是success的数据
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("status.keyword","success");

        // must表示各条件之间是and的关系
        boolQueryBuilder.must(rangeQueryBuilder);
        boolQueryBuilder.must(matchQueryBuilder);
        searchSourceBuilder.query(boolQueryBuilder);
        // 查询10条
        searchSourceBuilder.size(10);
        searchRequest.source(searchSourceBuilder);

查询结果:
带条件的查询

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值