add (ES native) aggregations to search result

This commit is contained in:
Jörg Prante 2021-06-08 16:33:59 +02:00
parent db6d67a91a
commit 7f8edfab0f
4 changed files with 31 additions and 12 deletions

View file

@ -1,5 +1,7 @@
package org.xbib.elx.api;
import org.elasticsearch.search.aggregations.Aggregations;
import java.util.List;
public interface SearchResult {
@ -9,4 +11,6 @@ public interface SearchResult {
long getTook();
List<SearchDocument> getDocuments();
Aggregations getAggregations();
}

View file

@ -165,7 +165,9 @@ public abstract class AbstractSearchClient extends AbstractBasicClient implement
}
return isempty ?
Optional.empty() :
Optional.of(new DefaultSearchResult(searchResponse.getHits(), searchResponse.getTook().getMillis()));
Optional.of(new DefaultSearchResult(searchResponse.getHits(),
searchResponse.getAggregations(),
searchResponse.getTook().getMillis()));
}
@Override
@ -249,7 +251,9 @@ public abstract class AbstractSearchClient extends AbstractBasicClient implement
condition, lastAction), false)
.onClose(responseStream::close)
.flatMap(searchResponse ->
new DefaultSearchResult(searchResponse.getHits(), searchResponse.getTook().getMillis()).getDocuments().stream());
new DefaultSearchResult(searchResponse.getHits(),
searchResponse.getAggregations(),
searchResponse.getTook().getMillis()).getDocuments().stream());
}
@Override

View file

@ -2,6 +2,7 @@ package org.xbib.elx.common;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregations;
import org.xbib.elx.api.SearchDocument;
import org.xbib.elx.api.SearchResult;
@ -12,13 +13,31 @@ public class DefaultSearchResult implements SearchResult {
private final SearchHits searchHits;
private final Aggregations aggregations;
private final long took;
public DefaultSearchResult(SearchHits searchHits, long took) {
public DefaultSearchResult(SearchHits searchHits,
Aggregations aggregations,
long took) {
this.searchHits = searchHits;
this.aggregations = aggregations;
this.took = took;
}
@Override
public List<SearchDocument> getDocuments() {
List<SearchDocument> list = new ArrayList<>();
for (SearchHit searchHit : searchHits.getHits()) {
list.add(new DefaultSearchDocument(searchHit));
}
return list;
}
public Aggregations getAggregations() {
return aggregations;
}
@Override
public long getTotal() {
return searchHits.getTotalHits().value;
@ -29,12 +48,4 @@ public class DefaultSearchResult implements SearchResult {
return took;
}
@Override
public List<SearchDocument> getDocuments() {
List<SearchDocument> list = new ArrayList<>();
for (SearchHit searchHit : searchHits.getHits()) {
list.add(new DefaultSearchDocument(searchHit));
}
return list;
}
}

View file

@ -1,6 +1,6 @@
group = org.xbib
name = elx
version = 7.10.2.14
version = 7.10.2.15
gradle.wrapper.version = 6.6.1
xbib-metrics.version = 2.2.0