2121public class AggregationQueryBuilder {
2222
2323 private final SearchConfiguration _configs ;
24- private final Set <String > _facetFields ;
24+ private final Set <String > _defaultFacetFields ;
25+ private final Set <String > _allFacetFields ;
2526
2627 public AggregationQueryBuilder (
2728 @ Nonnull final SearchConfiguration configs ,
2829 @ Nonnull final List <SearchableAnnotation > annotations ) {
2930 this ._configs = Objects .requireNonNull (configs , "configs must not be null" );
30- this ._facetFields = getFacetFields (annotations );
31+ this ._defaultFacetFields = getDefaultFacetFields (annotations );
32+ this ._allFacetFields = getAllFacetFields (annotations );
3133 }
3234
3335 /**
@@ -44,27 +46,36 @@ public List<AggregationBuilder> getAggregations(@Nullable List<String> facets) {
4446 final Set <String > facetsToAggregate ;
4547 if (facets != null ) {
4648 facets .stream ().filter (f -> !isValidAggregate (f )).forEach (facet -> {
47- log .warn (String .format ("Provided facet for search filter aggregations that doesn't exist. Provided: %s; Available: %s" , facet , _facetFields ));
49+ log .warn (String .format ("Requested facet for search filter aggregations that isn't part of the default filters. Provided: %s; Available: %s" , facet ,
50+ _defaultFacetFields ));
4851 });
4952 facetsToAggregate = facets .stream ().filter (this ::isValidAggregate ).collect (Collectors .toSet ());
5053 } else {
51- facetsToAggregate = _facetFields ;
54+ facetsToAggregate = _defaultFacetFields ;
5255 }
5356 return facetsToAggregate .stream ().map (this ::facetToAggregationBuilder ).collect (Collectors .toList ());
5457 }
5558
5659
57- private Set <String > getFacetFields (final List <SearchableAnnotation > annotations ) {
60+ private Set <String > getDefaultFacetFields (final List <SearchableAnnotation > annotations ) {
5861 Set <String > facets = annotations .stream ()
59- .flatMap (annotation -> getFacetFieldsFromAnnotation (annotation ).stream ())
62+ .flatMap (annotation -> getDefaultFacetFieldsFromAnnotation (annotation ).stream ())
63+ .collect (Collectors .toSet ());
64+ facets .add (INDEX_VIRTUAL_FIELD );
65+ return facets ;
66+ }
67+
68+ private Set <String > getAllFacetFields (final List <SearchableAnnotation > annotations ) {
69+ Set <String > facets = annotations .stream ()
70+ .flatMap (annotation -> getAllFacetFieldsFromAnnotation (annotation ).stream ())
6071 .collect (Collectors .toSet ());
6172 facets .add (INDEX_VIRTUAL_FIELD );
6273 return facets ;
6374 }
6475
6576 private boolean isValidAggregate (final String inputFacet ) {
6677 Set <String > facets = Set .of (inputFacet .split (AGGREGATION_SEPARATOR_CHAR ));
67- return facets .size () > 0 && _facetFields .containsAll (facets );
78+ return facets .size () > 0 && _allFacetFields .containsAll (facets );
6879 }
6980
7081 private AggregationBuilder facetToAggregationBuilder (final String inputFacet ) {
@@ -97,7 +108,7 @@ private String getAggregationField(final String facet) {
97108 return ESUtils .toKeywordField (facet , false );
98109 }
99110
100- List <String > getFacetFieldsFromAnnotation (final SearchableAnnotation annotation ) {
111+ List <String > getDefaultFacetFieldsFromAnnotation (final SearchableAnnotation annotation ) {
101112 final List <String > facetsFromAnnotation = new ArrayList <>();
102113 if (annotation .isAddToFilters ()) {
103114 facetsFromAnnotation .add (annotation .getFieldName ());
@@ -107,4 +118,13 @@ List<String> getFacetFieldsFromAnnotation(final SearchableAnnotation annotation)
107118 }
108119 return facetsFromAnnotation ;
109120 }
110- }
121+
122+ List <String > getAllFacetFieldsFromAnnotation (final SearchableAnnotation annotation ) {
123+ final List <String > facetsFromAnnotation = new ArrayList <>();
124+ facetsFromAnnotation .add (annotation .getFieldName ());
125+ if (annotation .getHasValuesFieldName ().isPresent ()) {
126+ facetsFromAnnotation .add (annotation .getHasValuesFieldName ().get ());
127+ }
128+ return facetsFromAnnotation ;
129+ }
130+ }
0 commit comments