Search in sources :

Example 76 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project starcoin-search by starcoinorg.

the class BlockService method getBlocksStartWith.

public Result<Block> getBlocksStartWith(String network, long start_height, int page, int count) {
    SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.BLOCK_IDS_INDEX));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    if (start_height == 0) {
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    } else {
        searchSourceBuilder.query(QueryBuilders.rangeQuery("header.number").lt(start_height));
    }
    // page size
    searchSourceBuilder.size(count);
    // begin offset
    int offset;
    if (page > 1) {
        offset = (page - 1) * count;
        if (offset >= ELASTICSEARCH_MAX_HITS) {
            searchSourceBuilder.searchAfter(new Object[] { offset });
        } else {
            searchSourceBuilder.from(offset);
        }
    }
    searchSourceBuilder.sort("header.number", SortOrder.DESC);
    searchSourceBuilder.trackTotalHits(true);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse;
    try {
        searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        logger.error("get range block error:", e);
        return null;
    }
    return ServiceUtils.getSearchResult(searchResponse, Block.class);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 77 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project starcoin-search by starcoinorg.

the class BlockService method getBlockByHeight.

public Block getBlockByHeight(String network, long height) {
    SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.BLOCK_IDS_INDEX));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("header.number", height);
    searchSourceBuilder.query(termQueryBuilder);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse;
    try {
        searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        logger.error("get block by height error:", e);
        return null;
    }
    Result<Block> result = ServiceUtils.getSearchResult(searchResponse, Block.class);
    List<Block> blocks = result.getContents();
    if (blocks.size() == 1) {
        return getBlockByHash(network, blocks.get(0).getHeader().getBlockHash());
    } else {
        logger.warn("get block by height is null, network: {}, : {}", network, height);
    }
    return null;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) UncleBlock(org.starcoin.bean.UncleBlock) Block(org.starcoin.bean.Block) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 78 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project starcoin-search by starcoinorg.

the class TransferHandle method getRangeTransfers.

public Result getRangeTransfers(TransferOffset transferOffset, int count) {
    SearchRequest searchRequest = new SearchRequest(ServiceUtils.getIndex(network, Constant.TRANSFER_INDEX));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // page size
    searchSourceBuilder.size(count);
    searchSourceBuilder.from(0);
    searchSourceBuilder.sort("timestamp", SortOrder.ASC);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(QueryBuilders.rangeQuery("timestamp").gt(transferOffset.getTimestamp()));
    searchSourceBuilder.query(boolQueryBuilder);
    searchRequest.source(searchSourceBuilder);
    searchSourceBuilder.trackTotalHits(true);
    SearchResponse searchResponse;
    try {
        searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        logger.error("get transfer error:", e);
        return Result.EmptyResult;
    }
    // set ids
    ResultWithId<Transfer> result = ServiceUtils.getSearchResultWithIds(searchResponse, Transfer.class);
    List<Transfer> transferList = result.getContents();
    List<String> ids = result.getIds();
    if (!transferList.isEmpty()) {
        for (int i = 0; i < transferList.size(); i++) {
            transferList.get(i).setId(ids.get(i));
        }
    }
    return result;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Transfer(org.starcoin.bean.Transfer) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 79 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project starcoin-search by starcoinorg.

the class TokenService method getTokenMarketCap.

public BigDecimal getTokenMarketCap(String network, String token) {
    Result<TokenStatistic> result2 = new Result<>();
    SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.MARKET_CAP_INDEX));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    queryBuilder.must(QueryBuilders.termsQuery("type_tag.keyword", token));
    searchSourceBuilder.query(queryBuilder);
    searchSourceBuilder.from(0);
    searchSourceBuilder.trackTotalHits(true);
    searchRequest.source(searchSourceBuilder);
    searchSourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
    try {
        result2 = ServiceUtils.getSearchResult(client.search(searchRequest, RequestOptions.DEFAULT), TokenStatistic.class);
    } catch (IOException e) {
        logger.error("get token market cap error:", e);
    }
    // get holder
    TokenStatistic tokenStatistic3 = new TokenStatistic();
    searchRequest = new SearchRequest(getIndex(network, Constant.ADDRESS_INDEX));
    searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.termQuery("type_tag.keyword", token));
    searchSourceBuilder.aggregation(AggregationBuilders.count("address_holders").field("address.keyword"));
    searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS));
    searchRequest.source(searchSourceBuilder);
    try {
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        tokenStatistic3.setAddressHolder(searchResponse.getHits().getTotalHits().value);
    } catch (IOException e) {
        logger.error("get token holder error:", e);
    }
    if (!result2.getContents().isEmpty()) {
        TokenStatistic tokenStatistic2 = result2.getContents().get(0);
        return BigDecimal.valueOf(tokenStatistic2.getMarketCap());
    }
    return BigDecimal.ZERO;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TokenStatistic(org.starcoin.bean.TokenStatistic) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) IOException(java.io.IOException) TimeValue(org.elasticsearch.common.unit.TimeValue) Result(org.starcoin.api.Result) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 80 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project starcoin-search by starcoinorg.

the class TokenService method tokenHolderList.

public Result<TokenStatistic> tokenHolderList(String network, int page, int count) {
    SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.ADDRESS_INDEX));
    int offset = (page - 1) * count;
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("holders").field("type_tag.keyword").order(BucketOrder.aggregation("address_holders", false)).subAggregation(AggregationBuilders.count("address_holders").field("address.keyword")).subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", null).from(offset).size(count));
    searchSourceBuilder.aggregation(aggregationBuilder);
    searchSourceBuilder.trackTotalHits(true);
    searchRequest.source(searchSourceBuilder);
    searchSourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
    try {
        return searchStatistic(client.search(searchRequest, RequestOptions.DEFAULT), StatisticType.AddressHolder);
    } catch (IOException e) {
        logger.error("get token stat error:", e);
        return null;
    }
}
Also used : BucketSortPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.BucketSortPipelineAggregationBuilder) SearchRequest(org.elasticsearch.action.search.SearchRequest) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IOException(java.io.IOException) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)655 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)453 SearchResponse (org.elasticsearch.action.search.SearchResponse)392 IOException (java.io.IOException)223 SearchHit (org.elasticsearch.search.SearchHit)122 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)107 ArrayList (java.util.ArrayList)73 SearchHits (org.elasticsearch.search.SearchHits)64 Test (org.junit.jupiter.api.Test)55 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)49 Test (org.junit.Test)47 TimeValue (org.elasticsearch.common.unit.TimeValue)45 HashMap (java.util.HashMap)41 List (java.util.List)41 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)40 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)39 Aggregations (org.elasticsearch.search.aggregations.Aggregations)38 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)38 Map (java.util.Map)36 IndexRequest (org.elasticsearch.action.index.IndexRequest)33