use of org.elasticsearch.search.aggregations.AggregationBuilder in project sonarqube by SonarSource.
the class RuleIndex method addStatusFacetIfNeeded.
private static void addStatusFacetIfNeeded(SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
if (options.getFacets().contains(FACET_STATUSES)) {
BoolQueryBuilder facetFilter = stickyFacetBuilder.getStickyFacetFilter(FIELD_RULE_STATUS);
AggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "_filter", facetFilter).subAggregation(AggregationBuilders.terms(FACET_STATUSES).field(FIELD_RULE_STATUS).includeExclude(new IncludeExclude(Joiner.on('|').join(ALL_STATUSES_EXCEPT_REMOVED), RuleStatus.REMOVED.toString())).size(ALL_STATUSES_EXCEPT_REMOVED.size()));
aggregations.put(FACET_STATUSES, AggregationBuilders.global(FACET_STATUSES).subAggregation(statuses));
}
}
use of org.elasticsearch.search.aggregations.AggregationBuilder in project vertigo by KleeGroup.
the class ESSearchRequestBuilder method appendFacetDefinition.
private static void appendFacetDefinition(final SearchQuery searchQuery, final SearchRequestBuilder searchRequestBuilder, final SearchIndexDefinition myIndexDefinition, final DtListState myListState, final boolean useHighlight) {
Assertion.checkNotNull(searchRequestBuilder);
// On ajoute le cluster, si présent
if (searchQuery.isClusteringFacet()) {
// si il y a un cluster on le place en premier
final FacetDefinition clusteringFacetDefinition = searchQuery.getClusteringFacetDefinition();
final AggregationBuilder aggregationBuilder = facetToAggregationBuilder(clusteringFacetDefinition);
final TopHitsAggregationBuilder topHitsBuilder = AggregationBuilders.topHits(TOPHITS_SUBAGGREGATION_NAME).size(myListState.getMaxRows().orElse(TOPHITS_SUBAGGREGATION_SIZE)).from(myListState.getSkipRows());
if (useHighlight) {
// .addHighlightedField("*"); HOW TO ?
topHitsBuilder.highlighter(new HighlightBuilder().numOfFragments(3));
}
if (myListState.getSortFieldName().isPresent()) {
topHitsBuilder.sort(getFieldSortBuilder(myIndexDefinition, myListState));
}
aggregationBuilder.subAggregation(topHitsBuilder);
// We fetch source, because it's our only source to create result list
searchRequestBuilder.addAggregation(aggregationBuilder);
}
// Puis les facettes liées à la query, si présent
if (searchQuery.getFacetedQuery().isPresent()) {
final FacetedQueryDefinition facetedQueryDefinition = searchQuery.getFacetedQuery().get().getDefinition();
final Collection<FacetDefinition> facetDefinitions = new ArrayList<>(facetedQueryDefinition.getFacetDefinitions());
if (searchQuery.isClusteringFacet() && facetDefinitions.contains(searchQuery.getClusteringFacetDefinition())) {
facetDefinitions.remove(searchQuery.getClusteringFacetDefinition());
}
for (final FacetDefinition facetDefinition : facetDefinitions) {
final AggregationBuilder aggregationBuilder = facetToAggregationBuilder(facetDefinition);
final BoolQueryBuilder aggsFilterBoolQueryBuilder = QueryBuilders.boolQuery();
for (final FacetDefinition filterFacetDefinition : searchQuery.getFacetedQuery().get().getDefinition().getFacetDefinitions()) {
if (filterFacetDefinition.isMultiSelectable() && !facetDefinition.equals(filterFacetDefinition)) {
// on ne doit refiltrer que les multiSelectable (les autres sont dans le filter de la request), sauf la facet qu'on est entrain de traiter
appendSelectedFacetValuesFilter(aggsFilterBoolQueryBuilder, searchQuery.getFacetedQuery().get().getSelectedFacetValues().getFacetValues(filterFacetDefinition));
}
}
if (aggsFilterBoolQueryBuilder.hasClauses()) {
final AggregationBuilder filterAggregationBuilder = AggregationBuilders.filter(facetDefinition.getName() + "_FILTER", aggsFilterBoolQueryBuilder);
filterAggregationBuilder.subAggregation(aggregationBuilder);
searchRequestBuilder.addAggregation(filterAggregationBuilder);
} else {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
}
}
use of org.elasticsearch.search.aggregations.AggregationBuilder in project vertexium by visallo.
the class ElasticsearchSearchQueryBase method getElasticsearchTermsAggregations.
protected List<AggregationBuilder> getElasticsearchTermsAggregations(TermsAggregation agg) {
List<AggregationBuilder> termsAggs = new ArrayList<>();
String fieldName = agg.getPropertyName();
if (Edge.LABEL_PROPERTY_NAME.equals(fieldName) || ExtendedDataRow.TABLE_NAME.equals(fieldName)) {
TermsAggregationBuilder termsAgg = AggregationBuilders.terms(createAggregationName(agg.getAggregationName(), "0"));
termsAgg.field(fieldName);
if (agg.getSize() != null) {
termsAgg.size(agg.getSize());
}
termsAgg.shardSize(termAggregationShardSize);
termsAggs.add(termsAgg);
} else {
PropertyDefinition propertyDefinition = getPropertyDefinition(fieldName);
for (String propertyName : getPropertyNames(fieldName)) {
boolean exactMatchProperty = isExactMatchPropertyDefinition(propertyDefinition);
String propertyNameWithSuffix;
if (exactMatchProperty) {
propertyNameWithSuffix = propertyName + Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX;
} else {
propertyNameWithSuffix = propertyName;
}
String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyNameWithSuffix);
String aggregationName = createAggregationName(agg.getAggregationName(), visibilityHash);
TermsAggregationBuilder termsAgg = AggregationBuilders.terms(aggregationName);
termsAgg.field(propertyNameWithSuffix);
if (agg.getSize() != null) {
termsAgg.size(agg.getSize());
}
termsAgg.shardSize(termAggregationShardSize);
if (exactMatchProperty && propertyDefinition.getTextIndexHints().contains(TextIndexHint.FULL_TEXT)) {
termsAgg.subAggregation(AggregationBuilders.topHits(TOP_HITS_AGGREGATION_NAME).fetchSource(new String[] { propertyName }, new String[0]).size(1));
}
for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) {
termsAgg.subAggregation(subAgg);
}
termsAggs.add(termsAgg);
}
}
return termsAggs;
}
use of org.elasticsearch.search.aggregations.AggregationBuilder in project vertexium by visallo.
the class ElasticsearchSearchQueryBase method buildQuery.
private SearchRequestBuilder buildQuery(EnumSet<ElasticsearchDocumentType> elementType, FetchHints fetchHints, boolean includeAggregations) {
if (QUERY_LOGGER.isTraceEnabled()) {
QUERY_LOGGER.trace("searching for: " + toString());
}
List<QueryBuilder> filters = getFilters(elementType, fetchHints);
QueryBuilder query = createQuery(getParameters());
query = scoringStrategy.updateQuery(query);
QueryBuilder filterBuilder = getFilterBuilder(filters);
String[] indicesToQuery = getIndexSelectionStrategy().getIndicesToQuery(this, elementType);
if (QUERY_LOGGER.isTraceEnabled()) {
QUERY_LOGGER.trace("indicesToQuery: %s", Joiner.on(", ").join(indicesToQuery));
}
SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(indicesToQuery).setTypes(getSearchIndex().getIdStrategy().getType()).setQuery(QueryBuilders.boolQuery().must(query).filter(filterBuilder)).storedFields(Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME, Elasticsearch5SearchIndex.ELEMENT_TYPE_FIELD_NAME, Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_NAME_FIELD_NAME, Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME);
if (includeAggregations) {
List<AggregationBuilder> aggs = getElasticsearchAggregations(getAggregations());
for (AggregationBuilder aggregationBuilder : aggs) {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
applySort(searchRequestBuilder);
return searchRequestBuilder;
}
use of org.elasticsearch.search.aggregations.AggregationBuilder in project vertexium by visallo.
the class ElasticsearchSearchQueryBase method getElasticsearchCalendarFieldAggregation.
private Collection<? extends AggregationBuilder> getElasticsearchCalendarFieldAggregation(CalendarFieldAggregation agg) {
List<AggregationBuilder> aggs = new ArrayList<>();
PropertyDefinition propertyDefinition = getPropertyDefinition(agg.getPropertyName());
if (propertyDefinition == null) {
throw new VertexiumException("Could not find mapping for property: " + agg.getPropertyName());
}
Class propertyDataType = propertyDefinition.getDataType();
for (String propertyName : getPropertyNames(agg.getPropertyName())) {
String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName);
String aggName = createAggregationName(agg.getAggregationName(), visibilityHash);
if (propertyDataType == Date.class) {
HistogramAggregationBuilder histAgg = AggregationBuilders.histogram(aggName);
histAgg.interval(1);
if (agg.getMinDocumentCount() != null) {
histAgg.minDocCount(agg.getMinDocumentCount());
} else {
histAgg.minDocCount(1L);
}
Script script = new Script(ScriptType.INLINE, "painless", getCalendarFieldAggregationScript(agg, propertyName), ImmutableMap.of("tzId", agg.getTimeZone().getID(), "fieldName", propertyName, "calendarField", agg.getCalendarField()));
histAgg.script(script);
for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) {
histAgg.subAggregation(subAgg);
}
aggs.add(histAgg);
} else {
throw new VertexiumException("Only dates are supported for hour of day aggregations");
}
}
return aggs;
}
Aggregations