From f651d7f463798f21f342e22b150c898367cb74fd Mon Sep 17 00:00:00 2001 From: Angel Borroy Date: Mon, 10 Aug 2020 17:27:11 +0200 Subject: [PATCH] SEARCH-2126: Address formatting issues. --- .../component/AlfrescoSearchHandler.java | 91 +++++++++---------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/search-services/alfresco-search/src/main/java/org/apache/solr/handler/component/AlfrescoSearchHandler.java b/search-services/alfresco-search/src/main/java/org/apache/solr/handler/component/AlfrescoSearchHandler.java index 0c51f1e7d..4237fe838 100644 --- a/search-services/alfresco-search/src/main/java/org/apache/solr/handler/component/AlfrescoSearchHandler.java +++ b/search-services/alfresco-search/src/main/java/org/apache/solr/handler/component/AlfrescoSearchHandler.java @@ -610,78 +610,73 @@ public class AlfrescoSearchHandler extends RequestHandlerBase implements } - /** - * Facet SOLR Queries are omitting facet results with count equals to 0 as general rule. - * SOLR Queries executed on a Shard environment, don't include these results but the same - * query executed on a single server environment are adding these elements to the response. - * - * This method removes every facet query having count equals to 0 to provide the same - * behaviour in both cases. - * - * A query like the following, will return only Facet Queries having elements: - * - * "facetQueries" : [ - * { "query" : "content.size:[0 TO 102400]", "label" : "small"}, - * { "query" : "content.size:[102400 TO 1048576]", "label" : "medium"}, - * { "query" : "content.size:[1048576 TO 16777216]", "label" : "large"} - * ] - * - * For instance, if there are only results with "small" key, the result will be: - * - * "facetQueries": [ - * { - * "label": "small", - * "filterQuery": "content.size:[0 TO 102400]", - * "count": 5 - * } - * - * @param rsp - */ - public static final String FACET_COUNTS_KEY = "facet_counts"; - public static final String FACET_QUERIES_KEY = "facet_queries"; - public static final String FACET_CONTEXT_KEY = "_facet.context"; - @SuppressWarnings("unchecked") + /** + * Facet SOLR Queries are omitting facet results with count equals to 0 as general rule. + * SOLR Queries executed on a Shard environment, don't include these results but the same + * query executed on a single server environment are adding these elements to the response. + * This method removes every facet query having count equals to 0 to provide the same + * behaviour in both cases. + * + * A query like the following, will return only Facet Queries having elements: + * "facetQueries" : [ + * { "query" : "content.size:[0 TO 102400]", "label" : "small"}, + * { "query" : "content.size:[102400 TO 1048576]", "label" : "medium"}, + * { "query" : "content.size:[1048576 TO 16777216]", "label" : "large"} + * ] + * + * For instance, if there are only results with "small" key, the result will be: + * "facetQueries": [ + * { + * "label": "small", + * "filterQuery": "content.size:[0 TO 102400]", + * "count": 5 + * } + * ] + * + */ + public static final String FACET_COUNTS_KEY = "facet_counts"; + public static final String FACET_QUERIES_KEY = "facet_queries"; + public static final String FACET_CONTEXT_KEY = "_facet.context"; + + @SuppressWarnings("unchecked") private void removeFacetQueriesWithCountZero(SolrQueryResponse rsp) - { - + { NamedList facetCounts = (NamedList) rsp.getValues().get(FACET_COUNTS_KEY); - if (facetCounts != null) { - NamedList facetQueries = (NamedList) facetCounts.get(FACET_QUERIES_KEY); - if (facetQueries != null) { List keyCountsToRemove = new ArrayList<>(); - + facetQueries.forEach(facetQuery -> { if ((Integer) facetQuery.getValue() == 0) { keyCountsToRemove.add(facetQuery.getKey()); } }); - + if (!keyCountsToRemove.isEmpty()) { keyCountsToRemove.forEach(key -> facetQueries.remove(key)); - - ((NamedList)rsp.getValues().get(FACET_COUNTS_KEY)).remove(FACET_QUERIES_KEY); - ((NamedList)rsp.getValues().get(FACET_COUNTS_KEY)).add(FACET_QUERIES_KEY, facetQueries); - + + ((NamedList) rsp.getValues().get(FACET_COUNTS_KEY)).remove(FACET_QUERIES_KEY); + ((NamedList) rsp.getValues().get(FACET_COUNTS_KEY)).add(FACET_QUERIES_KEY, + facetQueries); + BasicResultContext result = (BasicResultContext) rsp.getResponse(); - FacetContext facetContext = (FacetContext) result.getRequest().getContext().get(FACET_CONTEXT_KEY); - facetContext.getAllQueryFacets().removeIf(queryFacet -> - keyCountsToRemove.contains(queryFacet.getKey()) - ); + FacetContext facetContext = (FacetContext) result.getRequest().getContext() + .get(FACET_CONTEXT_KEY); + facetContext.getAllQueryFacets() + .removeIf(queryFacet -> keyCountsToRemove.contains(queryFacet.getKey())); result.getRequest().getContext().put(FACET_CONTEXT_KEY, facetContext); - + log.debug("In SOLR query '" + result.getRequest() + "', Facet Queries results having labels " + keyCountsToRemove + " have been removed from results"); } } } - } + } // ////////////////////// SolrInfoMBeans methods //////////////////////