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);
}
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;
}
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;
}
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;
}
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;
}
}
Aggregations