-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get tweet trends filter by timestamp
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
rest-api-server/src/main/java/io/zmyzheng/restapi/repository/EsOperationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.zmyzheng.restapi.repository; | ||
|
||
import io.zmyzheng.restapi.api.model.TrendRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.search.aggregations.Aggregations; | ||
import org.elasticsearch.search.aggregations.bucket.terms.Terms; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; | ||
import org.springframework.data.elasticsearch.core.ResultsExtractor; | ||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; | ||
import org.springframework.data.elasticsearch.core.query.SearchQuery; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; | ||
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; | ||
import static org.elasticsearch.search.aggregations.AggregationBuilders.range; | ||
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; | ||
|
||
/** | ||
* @Author: Mingyang Zheng | ||
* @Date: 2020-02-23 14:09 | ||
* | ||
* @Description: this class defines operations that are not covered in standard Spring Data Repositories | ||
*/ | ||
|
||
@Slf4j | ||
@Repository | ||
public class EsOperationRepository { | ||
|
||
private final ElasticsearchRestTemplate elasticsearchRestTemplate; | ||
|
||
public EsOperationRepository(ElasticsearchRestTemplate elasticsearchRestTemplate) { | ||
this.elasticsearchRestTemplate = elasticsearchRestTemplate; | ||
} | ||
|
||
public List<? extends Terms.Bucket> aggregateByField(TrendRequest trendRequest, String field) { | ||
SearchQuery searchQuery = new NativeSearchQueryBuilder() | ||
.withQuery(rangeQuery("timestamp").gte(trendRequest.getTimeFrom()).lte(trendRequest.getTimeTo())) | ||
.withIndices("streaming").withTypes("tweets") | ||
.addAggregation(terms(field).field(field).size(trendRequest.getTopN())) | ||
.build(); | ||
List<? extends Terms.Bucket> buckets = elasticsearchRestTemplate.query(searchQuery, new ResultsExtractor<List<? extends Terms.Bucket>>() { | ||
@Override | ||
public List<? extends Terms.Bucket> extract(SearchResponse response) { | ||
return ((Terms) response.getAggregations().get(field)).getBuckets(); | ||
} | ||
}); | ||
return buckets; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters