ElasticSearch初步使用

spring来管理 RestHighLevelClient

@Configuration
public class ElasticRestClientConf {
    @Value("${elasticsearch.ip}")
    private String ip;

    @Value("${elasticsearch.port}")
    private Integer port;

    @Bean
    public RestHighLevelClient elasticRestClient() {
        return new RestHighLevelClient(RestClient.builder(new HttpHost(ip, port, "http")));
    }
}

简单的查询

    public List<SourceLogEntity> getLog(String index, String serviceType, String startTime) {
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //.keyword表示全字匹配
        boolQueryBuilder.must(QueryBuilders.matchQuery("service_type.keyword", serviceType));
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("@timestamp").format(TIME_FORMAT).gte(startTime));

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.sort("@timestamp", SortOrder.ASC);
        searchSourceBuilder.query(boolQueryBuilder);
        //默认只查询10条,设为最大值10000
        searchSourceBuilder.size(10000);

        SearchRequest request = new SearchRequest();
        request.indices(index);
        request.source(searchSourceBuilder);

        try {
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);

            List<SourceLogEntity> logs = new ArrayList<>((int) response.getHits().getTotalHits());
            for (SearchHit hit : response.getHits().getHits()) {
                logs.add(SourceLogMapToEntity.convertToEntity(hit.getSourceAsMap()));
            }

            return logs;
        } catch (Exception e) {
        	log.error();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值