mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged 5.2.N (5.2.1) to HEAD (5.2)
131073 gjames: SEARCH-189: Always return skipCount and maxItems git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@132213 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -32,6 +32,7 @@ import org.alfresco.rest.api.search.impl.SearchMapper;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
import org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter;
|
||||
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.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.rest.framework.tools.RecognizedParamsExtractor;
|
||||
@@ -92,16 +93,16 @@ public class SearchApiWebscript extends AbstractWebScript implements RecognizedP
|
||||
SearchQuery searchQuery = extractJsonContent(webScriptRequest, assistant.getJsonHelper(), SearchQuery.class);
|
||||
|
||||
//Parse the parameters
|
||||
Params params = getParams(webScriptRequest, searchQuery);
|
||||
Params params = getParams(webScriptRequest, searchQuery.getFields(), searchQuery.getInclude(), searchQuery.getPaging());
|
||||
|
||||
//Turn the SearchQuery json into the Java SearchParameters object
|
||||
SearchParameters searchParams = searchMapper.toSearchParameters(searchQuery);
|
||||
SearchParameters searchParams = searchMapper.toSearchParameters(params, searchQuery);
|
||||
|
||||
//Call searchService
|
||||
ResultSet results = searchService.query(searchParams);
|
||||
|
||||
//Turn solr results into JSON
|
||||
CollectionWithPagingInfo<Node> resultJson = resultMapper.toCollectionWithPagingInfo(searchQuery, results);
|
||||
CollectionWithPagingInfo<Node> resultJson = resultMapper.toCollectionWithPagingInfo(params, results);
|
||||
//Post-process the request and pass in params, eg. params.getFilter()
|
||||
Object toRender = helper.processAdditionsToTheResponse(null, null, null, params, resultJson);
|
||||
|
||||
@@ -120,21 +121,28 @@ public class SearchApiWebscript extends AbstractWebScript implements RecognizedP
|
||||
* @param searchQuery
|
||||
* @return Params
|
||||
*/
|
||||
protected Params getParams(WebScriptRequest webScriptRequest, SearchQuery searchQuery)
|
||||
protected Params getParams(WebScriptRequest webScriptRequest, List<String> fields, List<String> include, Paging paging)
|
||||
{
|
||||
BeanPropertiesFilter filter = null;
|
||||
if (searchQuery.getFields()!= null && !searchQuery.getFields().isEmpty())
|
||||
if (paging == null)
|
||||
{
|
||||
List<String> selectList = new ArrayList<>(searchQuery.getFields().size());
|
||||
selectList.addAll(searchQuery.getFields());
|
||||
if (searchQuery.getInclude()!= null && !searchQuery.getInclude().isEmpty())
|
||||
paging = Paging.DEFAULT;
|
||||
}
|
||||
BeanPropertiesFilter filter = null;
|
||||
if (fields != null && !fields.isEmpty())
|
||||
{
|
||||
List<String> selectList = new ArrayList<>(fields.size());
|
||||
selectList.addAll(fields);
|
||||
|
||||
if (include != null && !include.isEmpty())
|
||||
{
|
||||
selectList.addAll(searchQuery.getInclude());
|
||||
selectList.addAll(include);
|
||||
}
|
||||
|
||||
filter = getFilter("", selectList);
|
||||
}
|
||||
Params.RecognizedParams recognizedParams = new Params.RecognizedParams(null, null, filter, null, null, null, null, null, false);
|
||||
return Params.valueOf(null, recognizedParams, searchQuery, webScriptRequest);
|
||||
|
||||
Params.RecognizedParams recognizedParams = new Params.RecognizedParams(null, paging, filter, null, include, null, null, null, false);
|
||||
return Params.valueOf(null, recognizedParams, null, webScriptRequest);
|
||||
}
|
||||
|
||||
public void setNodes(Nodes nodes) {
|
||||
|
@@ -34,10 +34,10 @@ 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;
|
||||
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
|
||||
import org.alfresco.rest.api.search.context.SearchContext;
|
||||
import org.alfresco.rest.api.search.context.FacetQueryContext;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.service.cmr.search.ResultSet;
|
||||
import org.alfresco.service.cmr.search.SpellCheckResult;
|
||||
import org.alfresco.util.Pair;
|
||||
@@ -74,7 +74,7 @@ public class ResultMapper
|
||||
* @param results
|
||||
* @return CollectionWithPagingInfo<Node>
|
||||
*/
|
||||
public CollectionWithPagingInfo<Node> toCollectionWithPagingInfo(SearchQuery searchQuery, ResultSet results)
|
||||
public CollectionWithPagingInfo<Node> toCollectionWithPagingInfo(Params params, ResultSet results)
|
||||
{
|
||||
SearchContext context = null;
|
||||
Integer total = null;
|
||||
@@ -83,7 +83,7 @@ public class ResultMapper
|
||||
|
||||
results.forEach(row ->
|
||||
{
|
||||
Node aNode = nodes.getFolderOrDocument(row.getNodeRef(), null, null, searchQuery.getInclude(), mapUserInfo);
|
||||
Node aNode = nodes.getFolderOrDocument(row.getNodeRef(), null, null, params.getInclude(), mapUserInfo);
|
||||
if (aNode != null)
|
||||
{
|
||||
float f = row.getScore();
|
||||
@@ -115,7 +115,7 @@ public class ResultMapper
|
||||
}
|
||||
}
|
||||
|
||||
return CollectionWithPagingInfo.asPaged(searchQuery.getPaging(), noderesults, results.hasMore(), total, null, context);
|
||||
return CollectionWithPagingInfo.asPaged(params.getPaging(), noderesults, results.hasMore(), total, null, context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -41,6 +41,7 @@ import org.alfresco.rest.api.search.model.Spelling;
|
||||
import org.alfresco.rest.api.search.model.Template;
|
||||
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
|
||||
import org.alfresco.rest.framework.resource.parameters.Paging;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.search.LimitBy;
|
||||
import org.alfresco.service.cmr.search.SearchParameters;
|
||||
@@ -84,7 +85,7 @@ public class SearchMapper
|
||||
* @param params
|
||||
* @return SearchParameters
|
||||
*/
|
||||
public SearchParameters toSearchParameters(SearchQuery searchQuery)
|
||||
public SearchParameters toSearchParameters(Params params, SearchQuery searchQuery)
|
||||
{
|
||||
ParameterCheck.mandatory("query", searchQuery.getQuery());
|
||||
|
||||
@@ -92,7 +93,7 @@ public class SearchMapper
|
||||
setDefaults(sp);
|
||||
|
||||
fromQuery(sp, searchQuery.getQuery());
|
||||
fromPaging(sp, searchQuery.getPaging());
|
||||
fromPaging(sp, params.getPaging());
|
||||
fromSort(sp, searchQuery.getSort());
|
||||
fromTemplate(sp, searchQuery.getTemplates());
|
||||
validateInclude(searchQuery.getInclude());
|
||||
@@ -115,8 +116,6 @@ public class SearchMapper
|
||||
{
|
||||
//Hardcode workspace store
|
||||
sp.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
|
||||
sp.setLimitBy(LimitBy.FINAL_SIZE);
|
||||
sp.setLimit(100);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -342,11 +342,23 @@ public interface RecognizedParamsExtractor
|
||||
*/
|
||||
default Paging findPaging(WebScriptRequest req)
|
||||
{
|
||||
int skipped = Paging.DEFAULT_SKIP_COUNT;
|
||||
int max = Paging.DEFAULT_MAX_ITEMS;
|
||||
String skip = req.getParameter(PARAM_PAGING_SKIP);
|
||||
String maxItems = req.getParameter(PARAM_PAGING_MAX);
|
||||
|
||||
return getPaging(skip,maxItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default paging object
|
||||
* @param skip
|
||||
* @param maxItems
|
||||
* @return
|
||||
*/
|
||||
default Paging getPaging(String skip, String maxItems)
|
||||
{
|
||||
int skipped = Paging.DEFAULT_SKIP_COUNT;
|
||||
int max = Paging.DEFAULT_MAX_ITEMS;
|
||||
|
||||
try
|
||||
{
|
||||
if (skip != null) { skipped = Integer.parseInt(skip);}
|
||||
|
@@ -35,7 +35,8 @@ import org.junit.runners.Suite.SuiteClasses;
|
||||
* @author Gethin James
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ SearchMapperTests.class, ResultMapperTests.class,SearchQuerySerializerTests.class})
|
||||
@SuiteClasses({ SearchMapperTests.class, ResultMapperTests.class,SearchQuerySerializerTests.class,
|
||||
SearchApiWebscriptTests.class})
|
||||
public class AllSearchApiTests
|
||||
{
|
||||
}
|
||||
|
@@ -47,6 +47,7 @@ import org.alfresco.rest.api.search.impl.ResultMapper;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
|
||||
import org.alfresco.rest.api.search.context.SearchContext;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
@@ -63,6 +64,8 @@ import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
import org.springframework.social.support.ParameterMap;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
@@ -84,6 +87,7 @@ public class ResultMapperTests
|
||||
+ "\"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}";
|
||||
public static final Params EMPTY_PARAMS = Params.valueOf((String)null,(String)null,(WebScriptRequest) null);
|
||||
|
||||
@BeforeClass
|
||||
public static void setupTests() throws Exception
|
||||
@@ -115,7 +119,7 @@ public class ResultMapperTests
|
||||
@Test
|
||||
public void testNoResults() throws Exception
|
||||
{
|
||||
CollectionWithPagingInfo<Node> collection = mapper.toCollectionWithPagingInfo(SearchQuery.EMPTY,new EmptyResultSet());
|
||||
CollectionWithPagingInfo<Node> collection = mapper.toCollectionWithPagingInfo(EMPTY_PARAMS,new EmptyResultSet());
|
||||
assertNotNull(collection);
|
||||
assertFalse(collection.hasMoreItems());
|
||||
assertTrue(collection.getTotalItems() < 1);
|
||||
@@ -126,7 +130,7 @@ public class ResultMapperTests
|
||||
public void testToCollectionWithPagingInfo() throws Exception
|
||||
{
|
||||
ResultSet results = mockResultset(Arrays.asList(514l));
|
||||
CollectionWithPagingInfo<Node> collectionWithPage = mapper.toCollectionWithPagingInfo(SearchQuery.EMPTY,results);
|
||||
CollectionWithPagingInfo<Node> collectionWithPage = mapper.toCollectionWithPagingInfo(EMPTY_PARAMS,results);
|
||||
assertNotNull(collectionWithPage);
|
||||
Long found = results.getNumberFound();
|
||||
assertEquals(found.intValue(), collectionWithPage.getTotalItems().intValue());
|
||||
|
@@ -0,0 +1,125 @@
|
||||
/*-
|
||||
* #%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;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertNotNull;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static junit.framework.TestCase.fail;
|
||||
import static org.alfresco.service.cmr.repository.StoreRef.PROTOCOL_DELETED;
|
||||
import static org.alfresco.service.cmr.repository.StoreRef.PROTOCOL_TEST;
|
||||
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_CMIS_ALFRESCO;
|
||||
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_FTS_ALFRESCO;
|
||||
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_LUCENE;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import org.alfresco.rest.api.search.impl.SearchMapper;
|
||||
import org.alfresco.rest.api.search.model.Default;
|
||||
import org.alfresco.rest.api.search.model.FacetField;
|
||||
import org.alfresco.rest.api.search.model.FacetFields;
|
||||
import org.alfresco.rest.api.search.model.FacetQuery;
|
||||
import org.alfresco.rest.api.search.model.FilterQuery;
|
||||
import org.alfresco.rest.api.search.model.Limits;
|
||||
import org.alfresco.rest.api.search.model.Query;
|
||||
import org.alfresco.rest.api.search.model.Scope;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
import org.alfresco.rest.api.search.model.SortDef;
|
||||
import org.alfresco.rest.api.search.model.Spelling;
|
||||
import org.alfresco.rest.api.search.model.Template;
|
||||
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
|
||||
import org.alfresco.rest.framework.resource.parameters.Paging;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.search.LimitBy;
|
||||
import org.alfresco.service.cmr.search.SearchParameters;
|
||||
import org.alfresco.service.cmr.search.SearchParameters.FieldFacet;
|
||||
import org.alfresco.service.cmr.search.SearchService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Tests the SearchApiWebscript class
|
||||
*
|
||||
* @author Gethin James
|
||||
*/
|
||||
public class SearchApiWebscriptTests
|
||||
{
|
||||
static SearchApiWebscript webscript = new SearchApiWebscript();
|
||||
|
||||
@Test
|
||||
public void testPaging() throws Exception
|
||||
{
|
||||
Params params = webscript.getParams(null, null, null, null);
|
||||
//Defaults
|
||||
assertNotNull(params.getPaging());
|
||||
assertEquals(Paging.DEFAULT_MAX_ITEMS, params.getPaging().getMaxItems());
|
||||
assertEquals(Paging.DEFAULT_SKIP_COUNT,params.getPaging().getSkipCount());
|
||||
|
||||
params = webscript.getParams(null, null, null, Paging.valueOf(4,20));
|
||||
assertEquals(20, params.getPaging().getMaxItems());
|
||||
assertEquals(4,params.getPaging().getSkipCount());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilter() throws Exception
|
||||
{
|
||||
Params params = webscript.getParams(null, null, null, null);
|
||||
//Defaults
|
||||
assertNotNull(params.getFilter());
|
||||
|
||||
params = webscript.getParams(null, null, Arrays.asList("name", "size"), null);
|
||||
assertTrue("This isn't used until include is also specfied", params.getFilter().isAllowed("name"));
|
||||
|
||||
assertTrue("Anything is allowed if include hasn't been specfied", params.getFilter().isAllowed("horse"));
|
||||
|
||||
params = webscript.getParams(null, Arrays.asList("cat", "dog"), null, null);
|
||||
assertTrue(params.getFilter().isAllowed("cat"));
|
||||
assertTrue(params.getFilter().isAllowed("dog"));
|
||||
assertFalse(params.getFilter().isAllowed("horse"));
|
||||
|
||||
params = webscript.getParams(null, Arrays.asList("cat", "dog"), Arrays.asList("name", "size"), null);
|
||||
assertTrue(params.getFilter().isAllowed("cat"));
|
||||
assertFalse(params.getFilter().isAllowed("horse"));
|
||||
assertTrue("name and size should be automatically added to the filter list",params.getFilter().isAllowed("name"));
|
||||
assertTrue("name and size should be automatically added to the filter list",params.getFilter().isAllowed("size"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInclude() throws Exception
|
||||
{
|
||||
Params params = webscript.getParams(null, null, null, null);
|
||||
//Defaults
|
||||
assertNotNull(params.getInclude());
|
||||
assertEquals(0, params.getInclude().size());
|
||||
|
||||
params = webscript.getParams(null, null, Arrays.asList("name", "size"), null);
|
||||
assertNotNull(params.getInclude());
|
||||
assertEquals(2, params.getInclude().size());
|
||||
}
|
||||
}
|
@@ -51,6 +51,7 @@ import org.alfresco.rest.api.search.model.Spelling;
|
||||
import org.alfresco.rest.api.search.model.Template;
|
||||
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
|
||||
import org.alfresco.rest.framework.resource.parameters.Paging;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.search.LimitBy;
|
||||
import org.alfresco.service.cmr.search.SearchParameters;
|
||||
@@ -74,13 +75,13 @@ public class SearchMapperTests
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testMandatory() throws Exception
|
||||
{
|
||||
SearchParameters searchParameters = searchMapper.toSearchParameters(SearchQuery.EMPTY);
|
||||
SearchParameters searchParameters = searchMapper.toSearchParameters(ResultMapperTests.EMPTY_PARAMS, SearchQuery.EMPTY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toSearchParameters() throws Exception
|
||||
{
|
||||
SearchParameters searchParameters = searchMapper.toSearchParameters(minimalQuery());
|
||||
SearchParameters searchParameters = searchMapper.toSearchParameters(ResultMapperTests.EMPTY_PARAMS, minimalQuery());
|
||||
assertNotNull(searchParameters);
|
||||
|
||||
//Test defaults
|
||||
@@ -89,7 +90,7 @@ public class SearchMapperTests
|
||||
assertEquals(LimitBy.FINAL_SIZE, searchParameters.getLimitBy());
|
||||
assertEquals(100, searchParameters.getLimit());
|
||||
|
||||
searchParameters = searchMapper.toSearchParameters(helper.searchQueryFromJson());
|
||||
searchParameters = searchMapper.toSearchParameters(ResultMapperTests.EMPTY_PARAMS, helper.searchQueryFromJson());
|
||||
assertNotNull(searchParameters);
|
||||
}
|
||||
|
||||
@@ -488,12 +489,12 @@ public class SearchMapperTests
|
||||
|
||||
//Doesn't error
|
||||
searchMapper.fromLimits(searchParameters, null);
|
||||
assertEquals(LimitBy.FINAL_SIZE, searchParameters.getLimitBy());
|
||||
assertEquals(100, searchParameters.getLimit());
|
||||
assertEquals(500, searchParameters.getLimit());
|
||||
assertEquals(LimitBy.UNLIMITED, searchParameters.getLimitBy());
|
||||
|
||||
searchMapper.fromLimits(searchParameters, new Limits(null, null));
|
||||
assertEquals(LimitBy.FINAL_SIZE, searchParameters.getLimitBy());
|
||||
assertEquals(100, searchParameters.getLimit());
|
||||
assertEquals(LimitBy.UNLIMITED, searchParameters.getLimitBy());
|
||||
assertEquals(500, searchParameters.getLimit());
|
||||
|
||||
searchMapper.fromLimits(searchParameters, new Limits(null, 34));
|
||||
assertEquals(LimitBy.NUMBER_OF_PERMISSION_EVALUATIONS, searchParameters.getLimitBy());
|
||||
|
Reference in New Issue
Block a user