es 库 elasticsearch-rest-high-level-client

注意不同es版本的兼容可能不同

pom文件引入依赖

 		<dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.4.0</version>
        </dependency>

RestHighLevelClient

RestHighLevelClient是操作es库的核心api,可以在项目中定义为全局bean,注意RestHighLevelClient使用的是htpp协议,对应es默认的http端口是9200

/**
 * @author :lujia
 * @date :2019/7/31  9:00
 */
@Configuration
public class EsRestConfig {


    @Value("${es.address}")
    private String esAddress;
    @Value("${es.protocol}")
    private String protocol;
    @Value("${es.port}")
    private int port;
    @Bean
    public RestHighLevelClient getHighClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        // 如果有多个从节点可以持续在内部new多个HttpHost,参数1是ip,参数2是HTTP端口,参数3是通信协议
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(esAddress, port, protocol)));

    }


}

查询

private SearchRequest buildSearchRequest(Long start, int from) {
        //查询index
        SearchRequest searchRequest = new SearchRequest("indes");
        //type
        searchRequest.types("doc");
        //构建查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 添加 match_all 查询
        //searchSourceBuilder.query(QueryBuilders.rangeQuery("offset").from(0).to(2));
        // 添加 match_all 查询
        // searchSourceBuilder.query(QueryBuilders.matchQuery("type","queenNEWS-app-api11"));
        //查询半小时之前的记录                                                  1564649210647
        searchSourceBuilder.query(QueryBuilders.rangeQuery("time").gte(start));
        //短语匹配查询
        // searchSourceBuilder.query(QueryBuilders.termQuery("message", "1564566001791"));
        // 将 SearchSourceBuilder  添加到 SeachRequest 中
        searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.ASC));
    	//es默认是返回10条数据的
        //分页,这个分页好像有问题,不知道是不是我使用方法不对,
        searchSourceBuilder.from(from).size(pageSize);
        searchSourceBuilder.fetchSource("time", "");
        searchRequest.source(searchSourceBuilder);
        return searchRequest;
    }

	 SearchResponse searchResponse=restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

//第一页数据
            SearchHit[] searchHits = searchResponse.getHits().getHits();

使用 scroll滚屏分页 -推荐

//初始化scroll //设定滚动时间间隔
final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
SearchRequest searchRequest = new SearchRequest("index");
//type
searchRequest.types("type");
searchRequest.scroll(scroll);
// SearchSourceBuilder 可以添加各种查询条件
 //查询条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//rangeQuery 范围查询
searchSourceBuilder.query(QueryBuilders.rangeQuery("time").gte(dateTime.getTime()));
//每页大小
searchSourceBuilder.size(pageSize);
 //请求
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//scroll id
String scrollId = searchResponse.getScrollId();
 //第一页数据
SearchHit[] searchHits = searchResponse.getHits().getHits();
 log.info("-----第一页size-----:{}", searchHits.length);
//遍历搜索命中的数据,直到没有数据
            while (searchHits != null && searchHits.length > 0) {
                SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
                scrollRequest.scroll(scroll);
                searchResponse = restHighLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
                scrollId = searchResponse.getScrollId();
                searchHits = searchResponse.getHits().getHits();
                if (searchHits != null && searchHits.length > 0) {
                    log.info("-----下一页size-----:{}", searchHits.length);
                }
            }
//清除滚屏
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(scrollId);
restHighLevelClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);

其他操作参考API

//添加 
/**
     * Index a document using the Index API.
     *
     * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html">Index API on elastic.co</a>
     * @deprecated Prefer {@link #index(IndexRequest, RequestOptions)}
     */
    @Deprecated
    public final IndexResponse index(IndexRequest indexRequest, Header... headers) throws IOException {
        return performRequestAndParseEntity(indexRequest, RequestConverters::index, IndexResponse::fromXContent, emptySet(), headers);
    }
//删除

    /**
     * Deletes a document by id using the Delete API.
     * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html">Delete API on elastic.co</a>
     * @param deleteRequest the reuqest
     * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
     * @return the response
     * @throws IOException in case there is a problem sending the request or parsing back the response
     */
    public final DeleteResponse delete(DeleteRequest deleteRequest, RequestOptions options) throws IOException {
        return performRequestAndParseEntity(deleteRequest, RequestConverters::delete, options, DeleteResponse::fromXContent,
                singleton(404));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值