fixed aggregations according to text/keyword type

This commit is contained in:
Michele Artini 2020-07-29 14:15:28 +02:00
parent 5b7f94c27a
commit 506f77ed60
2 changed files with 12 additions and 10 deletions

View File

@ -53,22 +53,22 @@ public class EventStatsManager {
public List<BrowseEntry> browseTopics() { public List<BrowseEntry> browseTopics() {
final String term = "topic"; final String term = "topic.keyword";
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchAllQuery()) .withQuery(QueryBuilders.matchAllQuery())
.withSearchType(SearchType.DEFAULT) .withSearchType(SearchType.DEFAULT)
.addAggregation(AggregationBuilders.terms(term).field(term).size(1000).minDocCount(1)) .addAggregation(AggregationBuilders.terms(term).field(term).size(1000).minDocCount(1))
.build(); .build();
final SearchHits<Event> hits = esOperations.search(searchQuery, Event.class, IndexCoordinates.of(elasticSearchProperties.getEventsIndexName())); final SearchHits<Event> hits = esOperations.search(searchQuery, Event.class, IndexCoordinates.of(elasticSearchProperties.getEventsIndexName()));
final Aggregations aggregations = hits.getAggregations(); final Aggregations aggregations = hits.getAggregations();
return ((ParsedStringTerms) aggregations.asMap().get(term)).getBuckets() return ((ParsedStringTerms) aggregations.asMap().get(term)).getBuckets()
.stream() .stream()
.map(b -> new BrowseEntry(b.getKeyAsString(), b.getDocCount())) .map(b -> new BrowseEntry(b.getKeyAsString(), b.getDocCount()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@ -101,17 +101,19 @@ public class OpenaireBrokerController extends AbstractLbsController {
@GetMapping("/topicsForDatasource") @GetMapping("/topicsForDatasource")
public List<BrowseEntry> findTopicsForDatasource(@RequestParam final String ds) { public List<BrowseEntry> findTopicsForDatasource(@RequestParam final String ds) {
final String term = "topic.keyword";
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.nestedQuery("map", QueryBuilders.matchQuery("map.targetDatasourceName", ds), ScoreMode.None)) .withQuery(QueryBuilders.nestedQuery("map", QueryBuilders.matchQuery("map.targetDatasourceName", ds), ScoreMode.None))
.withSearchType(SearchType.DEFAULT) .withSearchType(SearchType.DEFAULT)
.addAggregation(AggregationBuilders.terms("topic").field("topic").size(1000).minDocCount(1)) .addAggregation(AggregationBuilders.terms(term).field(term).size(1000).minDocCount(1))
.build(); .build();
final SearchHits<Event> hits = esOperations.search(searchQuery, Event.class, IndexCoordinates.of(props.getEventsIndexName())); final SearchHits<Event> hits = esOperations.search(searchQuery, Event.class, IndexCoordinates.of(props.getEventsIndexName()));
final Aggregations aggregations = hits.getAggregations(); final Aggregations aggregations = hits.getAggregations();
return ((ParsedStringTerms) aggregations.asMap().get("topic")).getBuckets() return ((ParsedStringTerms) aggregations.asMap().get(term)).getBuckets()
.stream() .stream()
.map(b -> new BrowseEntry(b.getKeyAsString(), b.getDocCount())) .map(b -> new BrowseEntry(b.getKeyAsString(), b.getDocCount()))
.collect(Collectors.toList()); .collect(Collectors.toList());