Merged searchbcr (5.2.1) to 5.2.N (5.2.1)

136072 gjames: SEARCH-334: Adding SearchRequestContext and pivot logic


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@136102 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2017-03-24 13:18:36 +00:00
parent 719f6637be
commit 725604db46
5 changed files with 190 additions and 24 deletions

View File

@@ -25,8 +25,8 @@
*/
package org.alfresco.rest.api.search;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.search.context.SearchRequestContext;
import org.alfresco.rest.api.search.impl.ResultMapper;
import org.alfresco.rest.api.search.impl.SearchMapper;
import org.alfresco.rest.api.search.model.SearchQuery;
@@ -39,7 +39,6 @@ import org.alfresco.rest.framework.tools.RecognizedParamsExtractor;
import org.alfresco.rest.framework.tools.RequestReader;
import org.alfresco.rest.framework.tools.ResponseWriter;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
import org.alfresco.rest.framework.webscripts.WithResponse;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters;
@@ -53,7 +52,6 @@ import org.springframework.extensions.webscripts.WebScriptResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@@ -92,14 +90,17 @@ public class SearchApiWebscript extends AbstractWebScript implements RecognizedP
//Parse the parameters
Params params = getParams(webScriptRequest, searchQuery.getFields(), searchQuery.getInclude(), searchQuery.getPaging());
//Make a copy of the request
SearchRequestContext searchRequestContext = SearchRequestContext.from(searchQuery);
//Turn the SearchQuery json into the Java SearchParameters object
SearchParameters searchParams = searchMapper.toSearchParameters(params, searchQuery);
SearchParameters searchParams = searchMapper.toSearchParameters(params, searchQuery, searchRequestContext);
//Call searchService
ResultSet results = searchService.query(searchParams);
//Turn solr results into JSON
CollectionWithPagingInfo<Node> resultJson = resultMapper.toCollectionWithPagingInfo(params, searchQuery, results);
CollectionWithPagingInfo<Node> resultJson = resultMapper.toCollectionWithPagingInfo(params, searchRequestContext, searchQuery, results);
//Post-process the request and pass in params, eg. params.getFilter()
Object toRender = helper.processAdditionsToTheResponse(null, null, null, params, resultJson);

View File

@@ -0,0 +1,90 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2017 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 static java.util.stream.Collectors.*;
import org.alfresco.rest.api.search.model.FacetFields;
import org.alfresco.rest.api.search.model.FacetQuery;
import org.alfresco.rest.api.search.model.Pivot;
import org.alfresco.rest.api.search.model.Query;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.service.cmr.search.IntervalParameters;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* This is a snapshot of the SearchQuery before the request is made.
* It isn't a complete copy of SearchQuery but only has fields that are useful when building
* the response
*/
public class SearchRequestContext
{
private final Query query;
private final boolean includeRequest;
private final Map<String, String> pivotKeys;
private SearchRequestContext(Query query, boolean includeRequest)
{
this.query = query;
this.includeRequest = includeRequest;
this.pivotKeys = new HashMap<>();
/**
this.facetQueries = facetQueries!=null?Collections.unmodifiableList(facetQueries): Collections.emptyList();
this.facetFields = new FacetFields(facetFields!=null?Collections.unmodifiableList(facetFields.getFacets()):Collections.emptyList());
this.facetIntervals = facetIntervals!=null?
new IntervalParameters(Collections.unmodifiableList(facetIntervals.getSets()),
Collections.unmodifiableList(facetIntervals.getIntervals()))
:
new IntervalParameters(Collections.emptyList(),Collections.emptyList());
this.pivots = pivots!=null?Collections.unmodifiableList(pivots): Collections.emptyList();**/
}
public static final SearchRequestContext from(SearchQuery searchQuery)
{
return new SearchRequestContext(searchQuery.getQuery(), searchQuery.includeRequest());
}
public Query getQuery()
{
return query;
}
public boolean includeRequest()
{
return includeRequest;
}
public Map<String, String> getPivotKeys()
{
return pivotKeys;
}
}

View File

@@ -30,6 +30,10 @@ import static org.alfresco.rest.api.search.impl.StoreMapper.DELETED;
import static org.alfresco.rest.api.search.impl.StoreMapper.LIVE_NODES;
import static org.alfresco.rest.api.search.impl.StoreMapper.VERSIONS;
import org.alfresco.repo.search.impl.lucene.SolrJSONResultSet;
import org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket;
import org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse;
import org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse.FACET_TYPE;
import org.alfresco.repo.search.impl.solr.facet.facetsresponse.MetricCount;
import org.alfresco.repo.version.Version2Model;
import org.alfresco.rest.api.DeletedNodes;
import org.alfresco.rest.api.Nodes;
@@ -41,11 +45,8 @@ import org.alfresco.rest.api.search.context.FacetFieldContext;
import org.alfresco.rest.api.search.context.FacetFieldContext.Bucket;
import org.alfresco.rest.api.search.context.FacetQueryContext;
import org.alfresco.rest.api.search.context.SearchContext;
import org.alfresco.rest.api.search.context.SearchRequestContext;
import org.alfresco.rest.api.search.context.SpellCheckContext;
import org.alfresco.rest.api.search.context.facetsresponse.GenericBucket;
import org.alfresco.rest.api.search.context.facetsresponse.GenericFacetResponse;
import org.alfresco.rest.api.search.context.facetsresponse.GenericFacetResponse.FACET_TYPE;
import org.alfresco.rest.api.search.context.facetsresponse.MetricCount;
import org.alfresco.rest.api.search.model.FacetField;
import org.alfresco.rest.api.search.model.FacetQuery;
import org.alfresco.rest.api.search.model.HighlightEntry;
@@ -77,7 +78,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Maps from a ResultSet to a json public api representation.
@@ -134,7 +135,7 @@ public class ResultMapper
* @param searchQuery
*@param results @return CollectionWithPagingInfo<Node>
*/
public CollectionWithPagingInfo<Node> toCollectionWithPagingInfo(Params params, SearchQuery searchQuery, ResultSet results)
public CollectionWithPagingInfo<Node> toCollectionWithPagingInfo(Params params, SearchRequestContext searchRequestContext, SearchQuery searchQuery, ResultSet results)
{
SearchContext context = null;
Integer total = null;
@@ -176,7 +177,7 @@ public class ResultMapper
if (solrResultSet != null)
{
//We used Solr for this query
context = toSearchContext(solrResultSet, searchQuery, notFound);
context = toSearchContext(solrResultSet, searchRequestContext, searchQuery, notFound);
total = setTotal(solrResultSet);
}
else
@@ -273,7 +274,7 @@ public class ResultMapper
* @param searchQuery
* @return SearchContext
*/
public SearchContext toSearchContext(SolrJSONResultSet solrResultSet, SearchQuery searchQuery, int notFound)
public SearchContext toSearchContext(SolrJSONResultSet solrResultSet, SearchRequestContext searchRequestContext, SearchQuery searchQuery, int notFound)
{
SearchContext context = null;
Map<String, Integer> facetQueries = solrResultSet.getFacetQueries();
@@ -316,6 +317,9 @@ public class ResultMapper
Map<String, List<Pair<String, Integer>>> facetInterval = solrResultSet.getFacetIntervals();
facets.addAll(getGenericFacetsForIntervals(facetInterval, searchQuery));
List<GenericFacetResponse> pimped = getPivots(searchRequestContext, solrResultSet.getPivotFacets());
facets.addAll(pimped);
//Spelling
SpellCheckResult spell = solrResultSet.getSpellCheckResult();
if (spell != null && spell.getResultName() != null && !spell.getResults().isEmpty())
@@ -324,7 +328,7 @@ public class ResultMapper
}
//Put it all together
context = new SearchContext(solrResultSet.getLastIndexedTxId(), facets, facetResults, ffcs, spellCheckContext, searchQuery.includeRequest()?searchQuery:null);
context = new SearchContext(solrResultSet.getLastIndexedTxId(), facets, facetResults, ffcs, spellCheckContext, searchRequestContext.includeRequest()?searchQuery:null);
return isNullContext(context)?null:context;
}
/**
@@ -371,6 +375,29 @@ public class ResultMapper
return facetResults;
}
protected List<GenericFacetResponse> getPivots(SearchRequestContext searchRequest, List<GenericFacetResponse> pivots)
{
if(pivots != null && !pivots.isEmpty())
{
Map<String, String> pivotKeys = searchRequest.getPivotKeys();
return pivots.stream().map(aFacet -> {
String pivotLabel = pivotKeys.containsKey(aFacet.getLabel())?pivotKeys.get(aFacet.getLabel()):aFacet.getLabel();
List<GenericBucket> bucks = aFacet.getBuckets().stream().map(genericBucket -> {
Object display = propertyLookup.lookup(aFacet.getLabel(), genericBucket.getLabel());
return new GenericBucket(genericBucket.getLabel(), genericBucket.getFilterQuery(),
display,genericBucket.getMetrics(), getPivots(searchRequest, genericBucket.getFacets()));
}).collect(Collectors.toList());
return new GenericFacetResponse(aFacet.getType(), pivotLabel, bucks);
}).collect(Collectors.toList());
}
return Collections.emptyList();
}
protected List<FacetFieldContext> getFacetBucketsForFacetFields(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery)
{
if (facetFields != null && !facetFields.isEmpty())
@@ -448,7 +475,7 @@ public class ResultMapper
}
}
}
GenericBucket bucket = new GenericBucket(buck.getFirst(), filterQuery, null , Arrays.asList(new MetricCount(buck.getSecond())));
GenericBucket bucket = new GenericBucket(buck.getFirst(), filterQuery, null , Arrays.asList(new MetricCount(buck.getSecond())), null);
buckets.add(bucket);
}
ffcs.add(new GenericFacetResponse(FACET_TYPE.interval, facet.getKey(), buckets));