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

129778 gjames: SEARCH-113: Initial implementation of the Search public API


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130169 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2016-09-06 14:15:02 +00:00
parent f60bdb5914
commit b18f433fca
14 changed files with 971 additions and 11 deletions

View File

@@ -0,0 +1,91 @@
/*-
* #%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.model.ContentModel;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.lang.NotImplementedException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ASPECTNAMES;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_PROPERTIES;
/**
* Maps from a Solr ResultSet to a json public api representation.
*
* @author Gethin James
*/
public class ResultMapper
{
private Nodes nodes;
public ResultMapper(Nodes nodes)
{
this.nodes = nodes;
ParameterCheck.mandatory("nodes", this.nodes);
}
/**
*
* @param params
* @param results
* @return
*/
public CollectionWithPagingInfo<Node> toCollectionWithPagingInfo(Params params, ResultSet results)
{
Long totalItems = results.getNumberFound();
List<Node> noderesults = new ArrayList();
results.forEach(row ->
{
Node aNode = nodes.getFolderOrDocument(row.getNodeRef(), null, null, params.getInclude(), null);
//float f = row.getScore();
//Long dbId = (Long) row.getValue(ContentModel.PROP_NODE_DBID);
noderesults.add(aNode);
}
);
Integer total = Integer.valueOf(totalItems.intValue());
return CollectionWithPagingInfo.asPaged(null, noderesults, noderesults.size() < total, total);
}
}

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.rest.api.search.model.Query;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.content.BasicContentInfo;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.rest.api.model.Node;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.lang.NotImplementedException;
import static org.alfresco.service.cmr.search.SearchService.*;
/**
* Maps from a json request and a solr SearchParameters object.
*
* @author Gethin James
*/
public class SearchMapper
{
/**
* Turn the params into the Java SearchParameters object
* @param params
* @return
*/
public SearchParameters toSearchParameters(Params params)
{
BasicContentInfo info = params.getContentInfo();
SearchQuery searchQuery = (SearchQuery) params.getPassedIn();
ParameterCheck.mandatory("query", searchQuery.getQuery());
SearchParameters sp = new SearchParameters();
fromQuery(sp, searchQuery.getQuery());
//Hardcode workspace store
sp.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
return sp;
}
/**
*
* @param sp
* @param q
*/
public void fromQuery(SearchParameters sp, Query q)
{
ParameterCheck.mandatoryString("query", q.getQuery());
ParameterCheck.mandatoryString("language", q.getLanguage());
switch (q.getLanguage().toLowerCase())
{
case "afts":
sp.setLanguage(LANGUAGE_FTS_ALFRESCO);
break;
case "lucene":
sp.setLanguage(LANGUAGE_LUCENE);
break;
case "cmis":
sp.setLanguage(LANGUAGE_CMIS_ALFRESCO);
break;
default:
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID,
new Object[] { ": language allowed values: afts,lucene,cmis" });
}
sp.setQuery(q.getQuery());
sp.setSearchTerm(q.getUserQuery());
}
}