mirror of
https://github.com/Alfresco/SearchServices.git
synced 2026-09-16 18:12:56 +00:00
Merge pull request #896 from Alfresco/fix/SEARCH_2126_RemoveCountZeroQueryFacets
SEARCH-2126: Remove facet query results having count equals to zero.
This commit is contained in:
+14
-7
@@ -139,16 +139,23 @@ public class FacetedSearchTest extends AbstractSearchServicesE2ETest
|
||||
FacetFieldBucket facet = response.getContext().getFacetQueries().get(0);
|
||||
facet.assertThat().field("label").contains("small").and().field("count").isGreaterThan(0);
|
||||
facet.assertThat().field("label").contains("small").and().field("filterQuery").is("content.size:[0 TO 102400]");
|
||||
response.getContext().getFacetQueries().get(1).assertThat().field("label").contains("large")
|
||||
.and().field("count").isLessThan(1)
|
||||
.and().field("filterQuery").is("content.size:[1048576 TO 16777216]");
|
||||
response.getContext().getFacetQueries().get(2).assertThat().field("label").contains("medium")
|
||||
.and().field("count").isLessThan(1)
|
||||
.and().field("filterQuery").is("content.size:[102400 TO 1048576]");
|
||||
//We don't expect to see the FacetFields if group is being used.
|
||||
Assert.assertEquals(response.getContext().getFacetQueries().size(), 1, "Results with count=0 must be omitted");
|
||||
|
||||
// We don't expect to see the FacetFields if group is being used.
|
||||
Assert.assertNull(response.getContext().getFacetsFields());
|
||||
Assert.assertNull(response.getContext().getFacets());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify this query is returning the same results for both single server and shard environments.
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test(groups={TestGroup.CONFIG_SHARDING})
|
||||
@TestRail(section = { TestGroup.REST_API, TestGroup.SEARCH}, executionType = ExecutionType.ACCEPTANCE, description = "Checks facet queries for the Search api in Shard environments")
|
||||
public void searchWithQueryFacetingCluster() throws Exception
|
||||
{
|
||||
searchWithQueryFaceting();
|
||||
}
|
||||
|
||||
/**
|
||||
* * Perform a group by faceting, below test groups the facet by group name foo.
|
||||
|
||||
+72
@@ -68,7 +68,9 @@ import org.apache.solr.core.CloseHook;
|
||||
import org.apache.solr.core.PluginInfo;
|
||||
import org.apache.solr.core.SolrCore;
|
||||
import org.apache.solr.handler.RequestHandlerBase;
|
||||
import org.apache.solr.handler.component.FacetComponent.FacetContext;
|
||||
import org.apache.solr.request.SolrQueryRequest;
|
||||
import org.apache.solr.response.BasicResultContext;
|
||||
import org.apache.solr.response.ResultContext;
|
||||
import org.apache.solr.response.SolrQueryResponse;
|
||||
import org.apache.solr.schema.IndexSchema;
|
||||
@@ -603,7 +605,77 @@ public class AlfrescoSearchHandler extends RequestHandlerBase implements
|
||||
shardInfo.add(shardInfoName, nl);
|
||||
rsp.getValues().add(ShardParams.SHARDS_INFO, shardInfo);
|
||||
}
|
||||
|
||||
removeFacetQueriesWithCountZero(rsp);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_CONTEXT_KEY = "_facet.context";
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void removeFacetQueriesWithCountZero(SolrQueryResponse rsp)
|
||||
{
|
||||
NamedList<Object> facetCounts = (NamedList<Object>) rsp.getValues().get(FACET_COUNTS_KEY);
|
||||
if (facetCounts != null)
|
||||
{
|
||||
NamedList<Object> facetQueries = (NamedList<Object>) facetCounts.get(FacetComponent.FACET_QUERY_KEY);
|
||||
if (facetQueries != null)
|
||||
{
|
||||
List<String> 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<Object>) rsp.getValues().get(FACET_COUNTS_KEY)).remove(FacetComponent.FACET_QUERY_KEY);
|
||||
((NamedList<Object>) rsp.getValues().get(FACET_COUNTS_KEY)).add(FacetComponent.FACET_QUERY_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()));
|
||||
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 //////////////////////
|
||||
|
||||
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Search Services
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2020 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
package org.apache.solr.handler.component;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.solr.common.util.NamedList;
|
||||
import org.apache.solr.handler.component.FacetComponent.FacetBase;
|
||||
import org.apache.solr.handler.component.FacetComponent.FacetContext;
|
||||
import org.apache.solr.request.SolrQueryRequest;
|
||||
import org.apache.solr.response.BasicResultContext;
|
||||
import org.apache.solr.response.SolrQueryResponse;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link AlfrescoSearchHandler} for Facet Queries requests.
|
||||
*/
|
||||
public class AlfrescoSearchHandlerFacetQueryIT
|
||||
{
|
||||
|
||||
@Mock
|
||||
NamedList<NamedList<Object>> mockParams;
|
||||
@Mock
|
||||
SolrQueryResponse mockResponse;
|
||||
@Mock
|
||||
SolrQueryRequest mockRequest;
|
||||
@Mock
|
||||
BasicResultContext mockResultContext;
|
||||
@Mock
|
||||
FacetContext mockFacetContext;
|
||||
@Mock
|
||||
Map<Object, Object> mockContext;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
initMocks(this);
|
||||
when(mockResponse.getValues()).thenReturn(mockParams);
|
||||
when(mockResponse.getResponse()).thenReturn(mockResultContext);
|
||||
when(mockResultContext.getRequest()).thenReturn(mockRequest);
|
||||
when(mockRequest.getContext()).thenReturn(mockContext);
|
||||
when(mockContext.get(AlfrescoSearchHandler.FACET_CONTEXT_KEY)).thenReturn(mockFacetContext);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testKeysWithCountZeroAreRemoved()
|
||||
{
|
||||
|
||||
NamedList<Object> facetQueries = new NamedList<>();
|
||||
facetQueries.add("small", 1);
|
||||
facetQueries.add("medium", 0);
|
||||
facetQueries.add("big", 0);
|
||||
|
||||
NamedList<Object> facetCounts = new NamedList<>();
|
||||
facetCounts.add(FacetComponent.FACET_QUERY_KEY, facetQueries);
|
||||
|
||||
when(mockParams.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).thenReturn(facetCounts);
|
||||
|
||||
List<FacetBase> queryFacets = new ArrayList<>();
|
||||
when(mockFacetContext.getAllQueryFacets()).thenReturn(queryFacets);
|
||||
|
||||
AlfrescoSearchHandler.removeFacetQueriesWithCountZero(mockResponse);
|
||||
|
||||
assertEquals(((NamedList<Object>) ((NamedList<Object>) mockResponse.getValues()
|
||||
.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).get(FacetComponent.FACET_QUERY_KEY)).size(), 1);
|
||||
assertEquals(((NamedList<Object>) ((NamedList<Object>) mockResponse.getValues()
|
||||
.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).getVal(0)).get("small"), 1);
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testKeysWithCountNonZeroArePresent()
|
||||
{
|
||||
|
||||
NamedList<Object> facetQueries = new NamedList<>();
|
||||
facetQueries.add("small", 1);
|
||||
facetQueries.add("medium", 2);
|
||||
facetQueries.add("big", 10);
|
||||
|
||||
NamedList<Object> facetCounts = new NamedList<>();
|
||||
facetCounts.add(FacetComponent.FACET_QUERY_KEY, facetQueries);
|
||||
|
||||
when(mockParams.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).thenReturn(facetCounts);
|
||||
|
||||
List<FacetBase> queryFacets = new ArrayList<>();
|
||||
when(mockFacetContext.getAllQueryFacets()).thenReturn(queryFacets);
|
||||
|
||||
AlfrescoSearchHandler.removeFacetQueriesWithCountZero(mockResponse);
|
||||
|
||||
assertEquals(((NamedList<Object>) ((NamedList<Object>) mockResponse.getValues()
|
||||
.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).get(FacetComponent.FACET_QUERY_KEY)).size(), 3);
|
||||
assertEquals(((NamedList<Object>) ((NamedList<Object>) mockResponse.getValues()
|
||||
.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).getVal(0)).get("small"), 1);
|
||||
assertEquals(((NamedList<Object>) ((NamedList<Object>) mockResponse.getValues()
|
||||
.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).getVal(0)).get("medium"), 2);
|
||||
assertEquals(((NamedList<Object>) ((NamedList<Object>) mockResponse.getValues()
|
||||
.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).getVal(0)).get("big"), 10);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testEmptyFacetQueries()
|
||||
{
|
||||
|
||||
NamedList<Object> facetQueries = new NamedList<>();
|
||||
|
||||
NamedList<Object> facetCounts = new NamedList<>();
|
||||
facetCounts.add(FacetComponent.FACET_QUERY_KEY, facetQueries);
|
||||
|
||||
when(mockParams.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).thenReturn(facetCounts);
|
||||
|
||||
List<FacetBase> queryFacets = new ArrayList<>();
|
||||
when(mockFacetContext.getAllQueryFacets()).thenReturn(queryFacets);
|
||||
|
||||
AlfrescoSearchHandler.removeFacetQueriesWithCountZero(mockResponse);
|
||||
|
||||
assertEquals(((NamedList<Object>) ((NamedList<Object>) mockResponse.getValues()
|
||||
.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).get(FacetComponent.FACET_QUERY_KEY)).size(), 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testEmptyFacetCount()
|
||||
{
|
||||
|
||||
NamedList<Object> facetCounts = new NamedList<>();
|
||||
|
||||
when(mockParams.get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).thenReturn(facetCounts);
|
||||
|
||||
List<FacetBase> queryFacets = new ArrayList<>();
|
||||
when(mockFacetContext.getAllQueryFacets()).thenReturn(queryFacets);
|
||||
|
||||
AlfrescoSearchHandler.removeFacetQueriesWithCountZero(mockResponse);
|
||||
|
||||
assertEquals(((NamedList<Object>) mockResponse.getValues().get(AlfrescoSearchHandler.FACET_COUNTS_KEY)).size(),
|
||||
0);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user