ACE-5622: Search api scope now uses "locations"

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@133518 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2016-12-08 15:49:24 +00:00
parent ffdb176db2
commit d8c47d60e1
17 changed files with 539 additions and 47 deletions

View File

@@ -77,8 +77,8 @@ public class SearchApiWebscript extends AbstractWebScript implements RecognizedP
PropertyCheck.mandatory(this, "serviceRegistry", serviceRegistry);
this.searchService = serviceRegistry.getSearchService();
ParameterCheck.mandatory("assistant", this.assistant);
searchMapper = new SearchMapper();
ParameterCheck.mandatory("searchMapper", this.searchMapper);
ParameterCheck.mandatory("resultMapper", this.resultMapper);
}
@Override
@@ -142,6 +142,11 @@ public class SearchApiWebscript extends AbstractWebScript implements RecognizedP
return Params.valueOf(null, recognizedParams, null, webScriptRequest);
}
public void setSearchMapper(SearchMapper searchMapper)
{
this.searchMapper = searchMapper;
}
public void setResultMapper(ResultMapper resultMapper)
{
this.resultMapper = resultMapper;

View File

@@ -26,11 +26,18 @@
package org.alfresco.rest.api.search.impl;
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.version.Version2Model;
import org.alfresco.repo.version.VersionBaseModel;
import org.alfresco.rest.api.DeletedNodes;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.lookups.PropertyLookupRegistry;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.api.nodes.NodeVersionsRelation;
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;
@@ -38,15 +45,24 @@ import org.alfresco.rest.api.search.context.SearchContext;
import org.alfresco.rest.api.search.context.SpellCheckContext;
import org.alfresco.rest.api.search.model.HighlightEntry;
import org.alfresco.rest.api.search.model.SearchEntry;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.cmr.search.SpellCheckResult;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionHistory;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -60,15 +76,38 @@ import java.util.Map.Entry;
*/
public class ResultMapper
{
private ServiceRegistry serviceRegistry;
private Nodes nodes;
private NodeVersionsRelation nodeVersions;
private PropertyLookupRegistry propertyLookup;
private StoreMapper storeMapper;
private DeletedNodes deletedNodes;
private static Log logger = LogFactory.getLog(ResultMapper.class);
public ResultMapper()
{
}
public void setServiceRegistry(ServiceRegistry serviceRegistry)
{
this.serviceRegistry = serviceRegistry;
}
public void setNodeVersions(NodeVersionsRelation nodeVersions)
{
this.nodeVersions = nodeVersions;
}
public void setDeletedNodes(DeletedNodes deletedNodes)
{
this.deletedNodes = deletedNodes;
}
public void setStoreMapper(StoreMapper storeMapper)
{
this.storeMapper = storeMapper;
}
public void setNodes(Nodes nodes)
{
this.nodes = nodes;
@@ -92,10 +131,12 @@ public class ResultMapper
List<Node> noderesults = new ArrayList();
Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
Map<NodeRef, List<Pair<String, List<String>>>> hightLighting = results.getHighlighting();
int notFound = 0;
results.forEach(row ->
for (ResultSetRow row:results)
{
Node aNode = nodes.getFolderOrDocument(row.getNodeRef(), null, null, params.getInclude(), mapUserInfo);
Node aNode = getNode(row, params, mapUserInfo);
if (aNode != null)
{
float f = row.getScore();
@@ -116,15 +157,16 @@ public class ResultMapper
else
{
logger.debug("Unknown noderef returned from search results "+row.getNodeRef());
notFound++;
}
});
}
SolrJSONResultSet solrResultSet = findSolrResultSet(results);
if (solrResultSet != null)
{
//We used Solr for this query
context = toSearchContext(solrResultSet);
context = toSearchContext(solrResultSet, notFound);
total = setTotal(solrResultSet);
}
else
@@ -141,6 +183,65 @@ public class ResultMapper
return CollectionWithPagingInfo.asPaged(params.getPaging(), noderesults, results.hasMore(), total, null, context);
}
/**
* Builds a node representation based on a ResultSetRow;
* @param aRow
* @param params
* @param mapUserInfo
* @return Node
*/
public Node getNode(ResultSetRow aRow, Params params, Map<String, UserInfo> mapUserInfo)
{
String nodeStore = storeMapper.getStore(aRow.getNodeRef());
Node aNode = null;
switch (nodeStore)
{
case LIVE_NODES:
aNode = nodes.getFolderOrDocument(aRow.getNodeRef(), null, null, params.getInclude(), mapUserInfo);
break;
case VERSIONS:
Map<QName, Serializable> properties = serviceRegistry.getNodeService().getProperties(aRow.getNodeRef());
NodeRef frozenNodeRef = ((NodeRef) properties.get(Version2Model.PROP_QNAME_FROZEN_NODE_REF));
String versionLabelId = (String) properties.get(Version2Model.PROP_QNAME_VERSION_LABEL);
Version v = null;
try
{
v = nodeVersions.findVersion(frozenNodeRef.getId(),versionLabelId);
aNode = nodes.getFolderOrDocument(v.getFrozenStateNodeRef(), null, null, params.getInclude(), mapUserInfo);
}
catch (EntityNotFoundException|InvalidNodeRefException e)
{
//Solr says there is a node but we can't find it
logger.debug("Failed to find a versioned node with id of "+frozenNodeRef
+ " this is probably because the original node has been deleted.");
}
if (v != null && aNode != null)
{
nodeVersions.mapVersionInfo(v, aNode, aRow.getNodeRef());
aNode.setNodeId(frozenNodeRef.getId());
aNode.setVersionLabel(versionLabelId);
}
break;
case DELETED:
try
{
aNode = deletedNodes.getDeletedNode(aRow.getNodeRef().getId(), params, false, mapUserInfo);
}
catch (EntityNotFoundException enfe)
{
//Solr says there is a deleted node but we can't find it, we want the rest of the search to return so lets ignore it.
logger.debug("Failed to find a deleted node with id of "+aRow.getNodeRef().getId());
}
break;
}
if (aNode != null)
{
aNode.setLocation(nodeStore);
}
return aNode;
}
/**
* Sets the total number found.
* @param results
@@ -158,7 +259,7 @@ public class ResultMapper
* @param SolrJSONResultSet
* @return SearchContext
*/
public SearchContext toSearchContext(SolrJSONResultSet solrResultSet)
public SearchContext toSearchContext(SolrJSONResultSet solrResultSet, int notFound)
{
SearchContext context = null;
Map<String, Integer> facetQueries = solrResultSet.getFacetQueries();

View File

@@ -83,6 +83,7 @@ public class SearchMapper
public static final String CMIS = "cmis";
public static final String LUCENE = "lucene";
public static final String AFTS = "afts";
private StoreMapper storeMapper;
/**
* Turn the SearchQuery params serialized by Jackson into the Java SearchParameters object
@@ -430,7 +431,7 @@ public class SearchMapper
{
if (scope != null)
{
List<String> stores = scope.getStores();
List<String> stores = scope.getLocations();
if (stores!= null && !stores.isEmpty())
{
//First reset the stores then add them.
@@ -439,7 +440,7 @@ public class SearchMapper
{
try
{
sp.addStore(new StoreRef(aStore));
sp.addStore(storeMapper.getStoreRef(aStore));
}
catch (AlfrescoRuntimeException are)
{
@@ -485,4 +486,9 @@ public class SearchMapper
}
}
}
public void setStoreMapper(StoreMapper storeMapper)
{
this.storeMapper = storeMapper;
}
}

View File

@@ -0,0 +1,102 @@
/*-
* #%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.impl;
import org.alfresco.repo.version.Version2Model;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Maps to and from a StoreRef for the json public api.
*
* @author Gethin James
*/
public class StoreMapper
{
public static final String LIVE_NODES = "nodes";
public static final String VERSIONS = "versions";
public static final String DELETED = "deleted-nodes";
private static Log logger = LogFactory.getLog(StoreMapper.class);
public static final StoreRef STORE_REF_VERSION2_SPACESSTORE = new StoreRef("workspace", Version2Model.STORE_ID);
/**
* Work out which StoreRef this store belongs to.
* @param String representing a store
* @return StoreRef
*/
public StoreRef getStoreRef(String store)
{
if (store != null && !store.isEmpty())
{
switch (store.toLowerCase())
{
case LIVE_NODES:
return StoreRef.STORE_REF_WORKSPACE_SPACESSTORE;
case VERSIONS:
return STORE_REF_VERSION2_SPACESSTORE;
case DELETED:
return StoreRef.STORE_REF_ARCHIVE_SPACESSTORE;
}
}
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID,
new Object[] { ": scope allowed values: nodes,deleted-nodes,versions" });
}
/**
* Work out which store this noderef belongs to.
* @param nodeRef
* @return String representing a store
*/
public String getStore(NodeRef nodeRef)
{
if (nodeRef != null)
{
if (StoreRef.STORE_REF_WORKSPACE_SPACESSTORE.equals(nodeRef.getStoreRef()))
{
return LIVE_NODES;
}
if (STORE_REF_VERSION2_SPACESSTORE.equals(nodeRef.getStoreRef()))
{
return VERSIONS;
}
if (StoreRef.STORE_REF_ARCHIVE_SPACESSTORE.equals(nodeRef.getStoreRef()))
{
return DELETED;
}
}
logger.warn("Unknown store ref: "+nodeRef);
return null;
}
}

View File

@@ -37,16 +37,16 @@ import java.util.List;
*/
public class Scope
{
private final List<String> stores;
private final List<String> locations;
@JsonCreator
public Scope(@JsonProperty("stores") List<String> stores)
public Scope(@JsonProperty("locations") List<String> locations)
{
this.stores = stores;
this.locations = locations;
}
public List<String> getStores()
public List<String> getLocations()
{
return stores;
return locations;
}
}