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:
Gethin James
2016-09-06 15:39:39 +00:00
parent 9f5cc53cf3
commit 9bd1b92cf7
8 changed files with 153 additions and 19 deletions

View File

@@ -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;
}
}
}

View File

@@ -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;

View File

@@ -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);
}
/**

View File

@@ -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);