mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged searchapi (5.2.1) to 5.2.N (5.2.1)
130147 gjames: SEARCH-121: Implementing facet fields git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130306 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*-
|
||||
* #%L
|
||||
* Alfresco Remote API
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2016 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.alfresco.rest.api.search.context;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The results of a Field Facetting
|
||||
*/
|
||||
public class FacetFieldContext
|
||||
{
|
||||
private final String label;
|
||||
private final List<Bucket> buckets;
|
||||
|
||||
public FacetFieldContext(String label, List<Bucket> buckets)
|
||||
{
|
||||
this.label = label;
|
||||
this.buckets = buckets;
|
||||
}
|
||||
|
||||
public String getLabel()
|
||||
{
|
||||
return label;
|
||||
}
|
||||
|
||||
public List<Bucket> getBuckets()
|
||||
{
|
||||
return buckets;
|
||||
}
|
||||
|
||||
public static class Bucket
|
||||
{
|
||||
private final String label;
|
||||
private final int count;
|
||||
|
||||
public Bucket(String label, int count)
|
||||
{
|
||||
this.label = label;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public String getLabel()
|
||||
{
|
||||
return label;
|
||||
}
|
||||
|
||||
public int getCount()
|
||||
{
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
@@ -35,8 +35,9 @@ public class SearchContext
|
||||
private final Consistency consistency;
|
||||
private final List<FacetQueryContext> facetQueries;
|
||||
private final SpellCheckContext spellCheck;
|
||||
private final FacetFieldContext facetsFields;
|
||||
|
||||
public SearchContext(long lastTxId, List<FacetQueryContext> facetQueries, SpellCheckContext spellCheck)
|
||||
public SearchContext(long lastTxId, List<FacetQueryContext> facetQueries, FacetFieldContext facetsFields, SpellCheckContext spellCheck)
|
||||
{
|
||||
this.spellCheck = spellCheck;
|
||||
if (lastTxId > 0)
|
||||
@@ -48,6 +49,7 @@ public class SearchContext
|
||||
consistency = null;
|
||||
}
|
||||
this.facetQueries = facetQueries;
|
||||
this.facetsFields = facetsFields;
|
||||
}
|
||||
|
||||
public Consistency getConsistency()
|
||||
@@ -65,6 +67,11 @@ public class SearchContext
|
||||
return spellCheck;
|
||||
}
|
||||
|
||||
public FacetFieldContext getFacetsFields()
|
||||
{
|
||||
return facetsFields;
|
||||
}
|
||||
|
||||
public class Consistency
|
||||
{
|
||||
private final long lastTxId;
|
||||
|
@@ -30,6 +30,8 @@ import org.alfresco.repo.search.impl.lucene.SolrJSONResultSet;
|
||||
import org.alfresco.rest.api.Nodes;
|
||||
import org.alfresco.rest.api.model.Node;
|
||||
import org.alfresco.rest.api.model.UserInfo;
|
||||
import org.alfresco.rest.api.search.context.FacetFieldContext;
|
||||
import org.alfresco.rest.api.search.context.FacetFieldContext.Bucket;
|
||||
import org.alfresco.rest.api.search.context.SpellCheckContext;
|
||||
import org.alfresco.rest.api.search.model.SearchEntry;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
@@ -38,6 +40,7 @@ import org.alfresco.rest.api.search.context.SearchContext;
|
||||
import org.alfresco.rest.api.search.context.FacetQueryContext;
|
||||
import org.alfresco.service.cmr.search.ResultSet;
|
||||
import org.alfresco.service.cmr.search.SpellCheckResult;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -138,7 +141,9 @@ public class ResultMapper
|
||||
Map<String, Integer> facetQueries = solrResultSet.getFacetQueries();
|
||||
List<FacetQueryContext> facetResults = null;
|
||||
SpellCheckContext spellCheckContext = null;
|
||||
FacetFieldContext ffc = null;
|
||||
|
||||
//Facet queries
|
||||
if(facetQueries!= null && !facetQueries.isEmpty())
|
||||
{
|
||||
facetResults = new ArrayList<>(facetQueries.size());
|
||||
@@ -148,12 +153,33 @@ public class ResultMapper
|
||||
}
|
||||
}
|
||||
|
||||
//Field Facets
|
||||
Map<String, List<Pair<String, Integer>>> facetFields = solrResultSet.getFieldFacets();
|
||||
if (facetFields != null && !facetFields.isEmpty())
|
||||
{
|
||||
for (Entry<String, List<Pair<String, Integer>>> facet:facetFields.entrySet())
|
||||
{
|
||||
if (facet.getValue() != null && !facet.getValue().isEmpty())
|
||||
{
|
||||
List<Bucket> buckets = new ArrayList<>(facet.getValue().size());
|
||||
for (Pair<String, Integer> buck:facet.getValue())
|
||||
{
|
||||
buckets.add(new Bucket(buck.getFirst(), buck.getSecond()));
|
||||
}
|
||||
ffc = new FacetFieldContext(facet.getKey(), buckets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Spelling
|
||||
SpellCheckResult spell = solrResultSet.getSpellCheckResult();
|
||||
if (spell != null && spell.getResultName() != null && !spell.getResults().isEmpty())
|
||||
{
|
||||
spellCheckContext = new SpellCheckContext(spell.getResultName(),spell.getResults());
|
||||
}
|
||||
context = new SearchContext(solrResultSet.getLastIndexedTxId(), facetResults, spellCheckContext);
|
||||
|
||||
//Put it all together
|
||||
context = new SearchContext(solrResultSet.getLastIndexedTxId(), facetResults, ffc, spellCheckContext);
|
||||
return isNullContext(context)?null:context;
|
||||
}
|
||||
|
||||
@@ -162,9 +188,12 @@ public class ResultMapper
|
||||
* @param context
|
||||
* @return true if its null
|
||||
*/
|
||||
protected boolean isNullContext(SearchContext context)
|
||||
public boolean isNullContext(SearchContext context)
|
||||
{
|
||||
return (context.getFacetQueries() == null && context.getConsistency() == null && context.getSpellCheck() == null);
|
||||
return (context.getFacetQueries() == null
|
||||
&& context.getConsistency() == null
|
||||
&& context.getSpellCheck() == null
|
||||
&& context.getFacetsFields() == null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -333,8 +333,8 @@ public class SearchMapper
|
||||
{
|
||||
ParameterCheck.mandatoryString("facetFields facet field", facet.getField());
|
||||
String field = facet.getField();
|
||||
String label = facet.getLabel()!=null?facet.getLabel():field;
|
||||
field = "{key='"+label+"'}"+field;
|
||||
//String label = facet.getLabel()!=null?facet.getLabel():field;
|
||||
//field = "{key='"+label+"'}"+field;
|
||||
|
||||
FieldFacet ff = new FieldFacet(field);
|
||||
|
||||
|
@@ -40,6 +40,9 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.rest.api.impl.NodesImpl;
|
||||
import org.alfresco.rest.api.model.Node;
|
||||
import org.alfresco.rest.api.model.UserInfo;
|
||||
import org.alfresco.rest.api.search.context.FacetFieldContext;
|
||||
import org.alfresco.rest.api.search.context.FacetQueryContext;
|
||||
import org.alfresco.rest.api.search.context.SpellCheckContext;
|
||||
import org.alfresco.rest.api.search.impl.ResultMapper;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
|
||||
@@ -77,8 +80,8 @@ public class ResultMapperTests
|
||||
{
|
||||
static ResultMapper mapper;
|
||||
public static final String JSON_REPONSE = "{\"responseHeader\":{\"status\":0,\"QTime\":9},\"_original_parameters_\":\"org.apache.solr.common.params.DefaultSolrParams:{params(df=TEXT&alternativeDic=DEFAULT_DICTIONARY&fl=DBID,score&start=0&fq={!afts}AUTHORITY_FILTER_FROM_JSON&fq={!afts}TENANT_FILTER_FROM_JSON&rows=1000&locale=en_US&wt=json),defaults(carrot.url=id&spellcheck.collateExtendedResults=true&carrot.produceSummary=true&spellcheck.maxCollations=3&spellcheck.maxCollationTries=5&spellcheck.alternativeTermCount=2&spellcheck.extendedResults=false&defType=afts&spellcheck.maxResultsForSuggest=5&spellcheck=false&carrot.outputSubClusters=false&spellcheck.count=5&carrot.title=mltext@m___t@{http://www.alfresco.org/model/content/1.0}title&carrot.snippet=content@s___t@{http://www.alfresco.org/model/content/1.0}content&spellcheck.collate=true)}\",\"_field_mappings_\":{},\"_date_mappings_\":{},\"_range_mappings_\":{},\"_pivot_mappings_\":{},\"_interval_mappings_\":{},\"_stats_field_mappings_\":{},\"_stats_facet_mappings_\":{},\"_facet_function_mappings_\":{},\"response\":{\"numFound\":6,\"start\":0,\"maxScore\":0.7849362,\"docs\":[{\"DBID\":565,\"score\":0.7849362},{\"DBID\":566,\"score\":0.7849362},{\"DBID\":521,\"score\":0.3540957},{\"DBID\":514,\"score\":0.33025497},{\"DBID\":420,\"score\":0.32440513},{\"DBID\":415,\"score\":0.2780319}]},"
|
||||
+ "\"facet_counts\":{\"facet_queries\":{\"{!afts}creator:admin\":1},\n"
|
||||
+ "\"facet_fields\":{},\"facet_dates\":{},\"facet_ranges\":{},\"facet_intervals\":{}\n" + " },"
|
||||
+ "\"facet_counts\":{\"facet_queries\":{\"small\":0,\"large\":0,\"xtra small\":3,\"xtra large\":0,\"medium\":8,\"XX large\":0},"
|
||||
+ "\"facet_fields\":{\"content.size\":[\"Big\",8,\"Brown\",3,\"Fox\",5,\"Jumped\",2,\"somewhere\",3]},\"facet_dates\":{},\"facet_ranges\":{},\"facet_intervals\":{}\n" + " },"
|
||||
+ "\"spellcheck\":{\"searchInsteadFor\":\"alfresco\"},"
|
||||
+ "\"processedDenies\":true, \"lastIndexedTx\":34}";
|
||||
|
||||
@@ -137,12 +140,24 @@ public class ResultMapperTests
|
||||
ResultSet results = mockResultset(Collections.emptyList());
|
||||
SearchContext searchContext = mapper.toSearchContext((SolrJSONResultSet) results);
|
||||
assertEquals(34l, searchContext.getConsistency().getlastTxId());
|
||||
assertEquals(1, searchContext.getFacetQueries().size());
|
||||
assertEquals("{!afts}creator:admin",searchContext.getFacetQueries().get(0).getLabel());
|
||||
assertEquals(1,searchContext.getFacetQueries().get(0).getCount());
|
||||
assertEquals(6, searchContext.getFacetQueries().size());
|
||||
// assertEquals("{!afts}creator:admin",searchContext.getFacetQueries().get(0).getLabel());
|
||||
// assertEquals(1,searchContext.getFacetQueries().get(0).getCount());
|
||||
assertEquals("searchInsteadFor",searchContext.getSpellCheck().getType());
|
||||
assertEquals(1,searchContext.getSpellCheck().getSuggestions().size());
|
||||
assertEquals("alfresco",searchContext.getSpellCheck().getSuggestions().get(0));
|
||||
assertEquals("content.size",searchContext.getFacetsFields().getLabel());
|
||||
assertEquals(5,searchContext.getFacetsFields().getBuckets().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNullContext() throws Exception
|
||||
{
|
||||
assertTrue(mapper.isNullContext(new SearchContext(0l,null,null,null)));
|
||||
assertFalse(mapper.isNullContext(new SearchContext(1l,null,null,null)));
|
||||
assertFalse(mapper.isNullContext(new SearchContext(0l,null,null,new SpellCheckContext(null, null))));
|
||||
assertFalse(mapper.isNullContext(new SearchContext(0l,Arrays.asList(new FacetQueryContext(null, 0)),null,null)));
|
||||
assertFalse(mapper.isNullContext(new SearchContext(0l,null,new FacetFieldContext(null, null),null)));
|
||||
}
|
||||
|
||||
private ResultSet mockResultset(List<Long> archivedNodes) throws JSONException
|
||||
|
@@ -436,13 +436,13 @@ public class SearchMapperTests
|
||||
assertEquals(0, ff.getMinCount());
|
||||
assertEquals(0, ff.getEnumMethodCacheMinDF());
|
||||
|
||||
assertEquals("{key='myfield'}myfield" ,ff.getField());
|
||||
// assertEquals("{key='myfield'}myfield" ,ff.getField());
|
||||
|
||||
searchParameters = new SearchParameters();
|
||||
searchMapper.fromFacetFields(searchParameters, new FacetFields(Arrays.asList(new FacetField("myfield","mylabel","myprefix",null,null,null,null,null,null,null))));
|
||||
|
||||
ff = searchParameters.getFieldFacets().get(0);
|
||||
assertEquals("{key='mylabel'}myfield" ,ff.getField());
|
||||
// assertEquals("{key='mylabel'}myfield" ,ff.getField());
|
||||
assertEquals("myprefix" ,ff.getPrefix());
|
||||
|
||||
try
|
||||
|
@@ -28,6 +28,8 @@ package org.alfresco.rest.api.search;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.alfresco.rest.api.search.context.FacetFieldContext;
|
||||
import org.alfresco.rest.api.search.context.FacetFieldContext.Bucket;
|
||||
import org.alfresco.rest.api.search.context.SpellCheckContext;
|
||||
import org.alfresco.rest.api.search.model.Default;
|
||||
import org.alfresco.rest.api.search.model.FacetField;
|
||||
@@ -90,15 +92,15 @@ public class SearchQuerySerializerTests
|
||||
assertEquals("facnoused",searchQuery.getFacetQueries().get(0).getLabel());
|
||||
assertEquals("alfrezco", searchQuery.getSpellcheck().getQuery());
|
||||
assertEquals(1, searchQuery.getScope().getStores().size());
|
||||
assertEquals("test://SpacesStore", searchQuery.getScope().getStores().get(0));
|
||||
assertEquals("workspace://SpacesStore", searchQuery.getScope().getStores().get(0));
|
||||
assertEquals(2, searchQuery.getFacetFields().getFacets().size());
|
||||
FacetField ff = searchQuery.getFacetFields().getFacets().get(0);
|
||||
assertEquals("aField", ff.getField());
|
||||
assertEquals("cm:creator", ff.getField());
|
||||
assertEquals("myquery2", ff.getPrefix());
|
||||
assertEquals("COUNT", ff.getSort());
|
||||
assertEquals(false, ff.getMissing());
|
||||
ff = searchQuery.getFacetFields().getFacets().get(1);
|
||||
assertEquals("anotherField", ff.getField());
|
||||
assertEquals("modifier", ff.getField());
|
||||
assertEquals("mylabel", ff.getLabel());
|
||||
assertEquals("FC", ff.getMethod());
|
||||
assertEquals(Integer.valueOf(5), ff.getMincount());
|
||||
@@ -110,7 +112,9 @@ public class SearchQuerySerializerTests
|
||||
{
|
||||
ExecutionResult exec1 = new ExecutionResult(new Farmer("180"),null);
|
||||
|
||||
FacetFieldContext ffc = new FacetFieldContext("theLabel", Arrays.asList(new Bucket("b1", 23), new Bucket("b2", 34)));
|
||||
SearchContext searchContext = new SearchContext(23l, Arrays.asList(new FacetQueryContext("f1", 15), new FacetQueryContext("f2", 20)),
|
||||
ffc,
|
||||
new SpellCheckContext("aFlag", Arrays.asList("bish", "bash")));
|
||||
CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
|
||||
String out = helper.writeResponse(coll);
|
||||
@@ -119,11 +123,15 @@ public class SearchQuerySerializerTests
|
||||
assertTrue("There must 'facetQueries f1' json output", out.contains("{\"label\":\"f1\",\"count\":15}"));
|
||||
assertTrue("There must 'facetQueries f2' json output", out.contains("{\"label\":\"f2\",\"count\":20}"));
|
||||
assertTrue("There must 'spellCheck' json output", out.contains("\"spellCheck\":{\"type\":\"aFlag\",\"suggestions\":[\"bish\",\"bash\"]}"));
|
||||
assertTrue("There must 'facetsFields' json output", out.contains("\"facetsFields\":{\"label\":\"theLabel\",\"buckets\""));
|
||||
assertTrue("There must 'bucket1' json output", out.contains("{\"label\":\"b1\",\"count\":23}"));
|
||||
assertTrue("There must 'bucket2' json output", out.contains("{\"label\":\"b2\",\"count\":34}"));
|
||||
|
||||
searchContext = new SearchContext(-1, null, null);
|
||||
searchContext = new SearchContext(-1, null, null, null);
|
||||
coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
|
||||
out = helper.writeResponse(coll);
|
||||
assertTrue("There must NOT BE a 'context' json output", out.contains("\"context\":{}"));
|
||||
assertFalse("There must NOT BE a 'facetsFields' json output", out.contains("\"facetsFields\":{}"));
|
||||
|
||||
}
|
||||
|
||||
|
@@ -60,10 +60,10 @@ public class SerializerTestHelper implements RequestReader
|
||||
+ "\"templates\": [{\"name\": \"mytemp\",\"template\": \"ATEMP\"}, {\"name\": \"yourtemp\",\"template\": \"%cm:content\"}],"
|
||||
+ "\"defaults\": {\"namespace\": \"namesp\",\"defaultFieldName\": \"myfield\",\"defaultFTSOperator\": \"AND\", \"textAttributes\": [\"roy\", \"king\"]},"
|
||||
+ "\"filterQueries\": [{\"query\": \"myquery\",\"tags\": [\"tag1\", \"tag2\"]},{\"query\": \"myquery2\"}],"
|
||||
+ "\"facetFields\": {\"facets\": [{\"field\": \"aField\",\"prefix\": \"myquery2\",\"sort\": \"COUNT\",\"missing\": \"false\"}, {\"field\": \"anotherField\",\"label\": \"mylabel\",\"method\": \"FC\",\"mincount\": \"5\"}]},"
|
||||
+ "\"facetFields\": {\"facets\": [{\"field\": \"cm:creator\",\"prefix\": \"myquery2\",\"sort\": \"COUNT\",\"missing\": \"false\"}, {\"field\": \"modifier\",\"label\": \"mylabel\",\"method\": \"FC\",\"mincount\": \"5\"}]},"
|
||||
+ "\"facetQueries\": [{\"query\": \"facquery\",\"label\": \"facnoused\"}],"
|
||||
+ "\"spellcheck\": {\"query\": \"alfrezco\"},"
|
||||
+ "\"scope\": { \"stores\": [\"test://SpacesStore\"]},"
|
||||
+ "\"scope\": { \"stores\": [\"workspace://SpacesStore\"]},"
|
||||
+ "\"include\": [\"aspectNames\", \"properties\"]}";
|
||||
|
||||
public SerializerTestHelper()
|
||||
|
Reference in New Issue
Block a user