mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged searchapi (5.2.1) to 5.2.N (5.2.1)
130027 gjames: SEARCH-120: Implementing facet queries git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130288 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -34,6 +34,7 @@ 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.framework.resource.parameters.SearchContext;
|
||||
import org.alfresco.rest.framework.resource.parameters.SearchContext.FacetQueryResult;
|
||||
import org.alfresco.service.cmr.search.ResultSet;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -43,6 +44,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* Maps from a ResultSet to a json public api representation.
|
||||
@@ -94,7 +96,7 @@ public class ResultMapper
|
||||
if (solrResultSet != null)
|
||||
{
|
||||
//We used Solr for this query
|
||||
context = setSearchContext(solrResultSet);
|
||||
context = toSearchContext(solrResultSet);
|
||||
total = setTotal(solrResultSet);
|
||||
}
|
||||
else
|
||||
@@ -128,15 +130,32 @@ public class ResultMapper
|
||||
* @param SolrJSONResultSet
|
||||
* @return SearchContext
|
||||
*/
|
||||
protected SearchContext setSearchContext(SolrJSONResultSet solrResultSet)
|
||||
public SearchContext toSearchContext(SolrJSONResultSet solrResultSet)
|
||||
{
|
||||
SearchContext context = null;
|
||||
Map<String, Integer> facetQueries = solrResultSet.getFacetQueries();
|
||||
List<FacetQueryResult> facetResults = null;
|
||||
|
||||
if (solrResultSet.getLastIndexedTxId() > 0)
|
||||
if(facetQueries!= null && !facetQueries.isEmpty())
|
||||
{
|
||||
context = new SearchContext(solrResultSet.getLastIndexedTxId());
|
||||
facetResults = new ArrayList<>(facetQueries.size());
|
||||
for (Entry<String, Integer> fq:facetQueries.entrySet())
|
||||
{
|
||||
facetResults.add(new FacetQueryResult(fq.getKey(), fq.getValue()));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
context = new SearchContext(solrResultSet.getLastIndexedTxId(), facetResults);
|
||||
return isNullContext(context)?null:context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the context null?
|
||||
* @param context
|
||||
* @return true if its null
|
||||
*/
|
||||
protected boolean isNullContext(SearchContext context)
|
||||
{
|
||||
return (context.getFacetQueries() == null && context.getConsistency() == null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -27,6 +27,7 @@
|
||||
package org.alfresco.rest.api.search.impl;
|
||||
|
||||
import org.alfresco.rest.api.search.model.Default;
|
||||
import org.alfresco.rest.api.search.model.FacetQuery;
|
||||
import org.alfresco.rest.api.search.model.FilterQuery;
|
||||
import org.alfresco.rest.api.search.model.Query;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
@@ -92,6 +93,7 @@ public class SearchMapper
|
||||
validateInclude(searchQuery.getInclude());
|
||||
fromDefault(sp, searchQuery.getDefaults());
|
||||
fromFilterQuery(sp, searchQuery.getFilterQueries());
|
||||
fromFacetQuery(sp, searchQuery.getFacetQueries());
|
||||
|
||||
return sp;
|
||||
}
|
||||
@@ -257,4 +259,20 @@ public class SearchMapper
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** SearchParameters from List<FacetQuery>
|
||||
* @param sp
|
||||
* @param facetQueries
|
||||
*/
|
||||
public void fromFacetQuery(SearchParameters sp, List<FacetQuery> facetQueries)
|
||||
{
|
||||
if (facetQueries != null && !facetQueries.isEmpty())
|
||||
{
|
||||
for (FacetQuery fq:facetQueries)
|
||||
{
|
||||
sp.addFacetQuery(fq.getQuery());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,55 @@
|
||||
/*-
|
||||
* #%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.model;
|
||||
|
||||
import org.codehaus.jackson.annotate.JsonCreator;
|
||||
import org.codehaus.jackson.annotate.JsonProperty;
|
||||
|
||||
/**
|
||||
* POJO class representing the FacetQuery
|
||||
*/
|
||||
public class FacetQuery
|
||||
{
|
||||
private final String query;
|
||||
private final String label;
|
||||
|
||||
@JsonCreator
|
||||
public FacetQuery(@JsonProperty("query") String query, @JsonProperty("label") String label)
|
||||
{
|
||||
this.query = query;
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getQuery()
|
||||
{
|
||||
return query;
|
||||
}
|
||||
|
||||
public String getLabel()
|
||||
{
|
||||
return label;
|
||||
}
|
||||
}
|
@@ -46,8 +46,9 @@ public class SearchQuery
|
||||
private final List<Template> templates;
|
||||
private final Default defaults;
|
||||
private final List<FilterQuery> filterQueries;
|
||||
private final List<FacetQuery> facetQueries;
|
||||
|
||||
public static final SearchQuery EMPTY = new SearchQuery(null, null, null, null, null,null, null);
|
||||
public static final SearchQuery EMPTY = new SearchQuery(null, null, null, null, null,null, null, null);
|
||||
|
||||
@JsonCreator
|
||||
public SearchQuery(@JsonProperty("query") Query query,
|
||||
@@ -56,7 +57,8 @@ public class SearchQuery
|
||||
@JsonProperty("sort") List<SortDef> sort,
|
||||
@JsonProperty("templates") List<Template> templates,
|
||||
@JsonProperty("defaults") Default defaults,
|
||||
@JsonProperty("filterQueries") List<FilterQuery> filterQueries)
|
||||
@JsonProperty("filterQueries") List<FilterQuery> filterQueries,
|
||||
@JsonProperty("facetQueries") List<FacetQuery> facetQueries)
|
||||
{
|
||||
this.query = query;
|
||||
this.paging = paging;
|
||||
@@ -65,6 +67,7 @@ public class SearchQuery
|
||||
this.templates = templates;
|
||||
this.defaults = defaults;
|
||||
this.filterQueries = filterQueries;
|
||||
this.facetQueries = facetQueries;
|
||||
}
|
||||
|
||||
public Query getQuery()
|
||||
@@ -99,4 +102,9 @@ public class SearchQuery
|
||||
{
|
||||
return filterQueries;
|
||||
}
|
||||
|
||||
public List<FacetQuery> getFacetQueries()
|
||||
{
|
||||
return facetQueries;
|
||||
}
|
||||
}
|
||||
|
@@ -25,26 +25,65 @@
|
||||
*/
|
||||
package org.alfresco.rest.framework.resource.parameters;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The contextual results of a Search
|
||||
*/
|
||||
public class SearchContext
|
||||
{
|
||||
Consistency consistency;
|
||||
private final Consistency consistency;
|
||||
private final List<FacetQueryResult> facetQueries;
|
||||
|
||||
public SearchContext(long lastTxId)
|
||||
public SearchContext(long lastTxId, List<FacetQueryResult> facetQueries)
|
||||
{
|
||||
if (lastTxId > 0)
|
||||
{
|
||||
consistency = new Consistency(lastTxId);
|
||||
}
|
||||
else
|
||||
{
|
||||
consistency = null;
|
||||
}
|
||||
this.facetQueries = facetQueries;
|
||||
}
|
||||
|
||||
public Consistency getConsistency()
|
||||
{
|
||||
return consistency;
|
||||
}
|
||||
|
||||
public List<FacetQueryResult> getFacetQueries()
|
||||
{
|
||||
return facetQueries;
|
||||
}
|
||||
|
||||
public static class FacetQueryResult
|
||||
{
|
||||
private final String label;
|
||||
private final int count;
|
||||
|
||||
public FacetQueryResult(String label, int count)
|
||||
{
|
||||
this.label = label;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public String getLabel()
|
||||
{
|
||||
return label;
|
||||
}
|
||||
|
||||
public int getCount()
|
||||
{
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public class Consistency
|
||||
{
|
||||
private long lastTxId;
|
||||
private final long lastTxId;
|
||||
|
||||
public Consistency(long lastTxId)
|
||||
{
|
||||
|
@@ -43,6 +43,7 @@ import org.alfresco.rest.api.model.UserInfo;
|
||||
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.framework.resource.parameters.SearchContext;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
@@ -62,6 +63,7 @@ import org.mockito.stubbing.Answer;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -123,7 +125,15 @@ public class ResultMapperTests
|
||||
assertEquals(found.intValue(), collectionWithPage.getTotalItems().intValue());
|
||||
Node firstNode = collectionWithPage.getCollection().stream().findFirst().get();
|
||||
assertNotNull(firstNode.getSearch().getScore());
|
||||
assertEquals(34l, collectionWithPage.getContext().getConsistency().getlastTxId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToSearchContext() throws Exception
|
||||
{
|
||||
ResultSet results = mockResultset(Collections.emptyList());
|
||||
SearchContext searchContext = mapper.toSearchContext((SolrJSONResultSet) results);
|
||||
assertEquals(34l, searchContext.getConsistency().getlastTxId());
|
||||
assertNotNull(searchContext.getFacetQueries());
|
||||
}
|
||||
|
||||
private ResultSet mockResultset(List<Long> archivedNodes) throws JSONException
|
||||
|
@@ -35,6 +35,7 @@ import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_LUCENE;
|
||||
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.FacetQuery;
|
||||
import org.alfresco.rest.api.search.model.FilterQuery;
|
||||
import org.alfresco.rest.api.search.model.Query;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
@@ -286,10 +287,27 @@ public class SearchMapperTests
|
||||
//tags aren't used at the moment
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromFacetQuery() throws Exception
|
||||
{
|
||||
SearchParameters searchParameters = new SearchParameters();
|
||||
//Doesn't error
|
||||
searchMapper.fromFacetQuery(searchParameters, null);
|
||||
|
||||
searchMapper.fromFacetQuery(searchParameters, Arrays.asList(new FacetQuery("ping", null), new FacetQuery("pong", "table"), new FacetQuery("pang", "tennis")));
|
||||
assertEquals(3 ,searchParameters.getFacetQueries().size());
|
||||
assertEquals("ping" ,searchParameters.getFacetQueries().get(0));
|
||||
assertEquals("pong" ,searchParameters.getFacetQueries().get(1));
|
||||
assertEquals("pang" ,searchParameters.getFacetQueries().get(2));
|
||||
|
||||
//label isn't used at the moment
|
||||
}
|
||||
|
||||
|
||||
private SearchQuery minimalQuery()
|
||||
{
|
||||
Query query = new Query("cmis", "foo", "");
|
||||
SearchQuery sq = new SearchQuery(query,null, null, null, null, null, null);
|
||||
SearchQuery sq = new SearchQuery(query,null, null, null, null, null, null, null);
|
||||
return sq;
|
||||
}
|
||||
}
|
||||
|
@@ -27,22 +27,18 @@ package org.alfresco.rest.api.search;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import org.alfresco.rest.api.model.Comment;
|
||||
import org.alfresco.rest.api.search.model.Default;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
|
||||
import org.alfresco.rest.framework.jacksonextensions.RestJsonModule;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.rest.framework.tools.RequestReader;
|
||||
import org.alfresco.rest.framework.jacksonextensions.ExecutionResult;
|
||||
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
|
||||
import org.alfresco.rest.framework.resource.parameters.SearchContext;
|
||||
import org.alfresco.rest.framework.resource.parameters.SearchContext.FacetQueryResult;
|
||||
import org.alfresco.rest.framework.tests.api.mocks.Farmer;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.extensions.surf.util.Content;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Tests json -> SearchQuery deserialization
|
||||
@@ -86,6 +82,25 @@ public class SearchQuerySerializerTests
|
||||
assertEquals("myquery",searchQuery.getFilterQueries().get(0).getQuery());
|
||||
assertEquals(2, searchQuery.getFilterQueries().get(0).getTags().size());
|
||||
assertEquals("myquery2",searchQuery.getFilterQueries().get(1).getQuery());
|
||||
assertEquals(1, searchQuery.getFacetQueries().size());
|
||||
assertEquals("facquery",searchQuery.getFacetQueries().get(0).getQuery());
|
||||
assertEquals("facnoused",searchQuery.getFacetQueries().get(0).getLabel());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSerializeContext() throws IOException
|
||||
{
|
||||
ExecutionResult exec1 = new ExecutionResult(new Farmer("180"),null);
|
||||
SearchContext searchContext = new SearchContext(23l, Arrays.asList(new FacetQueryResult("f1", 15), new FacetQueryResult("f2", 20)));
|
||||
CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
|
||||
String out = helper.writeResponse(coll);
|
||||
assertTrue("There must 'context' json output", out.contains("\"context\":{\"consistency\":{\"lastTxId\":23}"));
|
||||
assertTrue("There must 'facetQueries' json output", out.contains("\"facetQueries\":"));
|
||||
assertTrue("There must 'facetQueries f1' json output", out.contains("{\"label\":\"f1\",\"count\":15}"));
|
||||
assertTrue("There must 'facetQueries f2' json output", out.contains("{\"label\":\"f2\",\"count\":20}"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -29,12 +29,18 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import org.alfresco.rest.api.search.model.SearchQuery;
|
||||
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
|
||||
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer;
|
||||
import org.alfresco.rest.framework.jacksonextensions.RestJsonModule;
|
||||
import org.alfresco.rest.framework.tools.RequestReader;
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.JsonGenerator;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.junit.BeforeClass;
|
||||
import org.springframework.extensions.surf.util.Content;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
@@ -54,6 +60,7 @@ public class SerializerTestHelper implements RequestReader
|
||||
+ "\"templates\": [{\"name\": \"mytemp\",\"template\": \"ATEMP\"}, {\"name\": \"yourtemp\",\"template\": \"%cm:content\"}],"
|
||||
+ "\"defaults\": {\"namespace\": \"namesp\",\"defaultFieldName\": \"myfield\",\"defaultFTSOperator\": \"AND\", \"textAttributes\": [\"roy\", \"king\"]},"
|
||||
+ "\"filterQueries\": [{\"query\": \"myquery\",\"tags\": [\"tag1\", \"tag2\"]},{\"query\": \"myquery2\"}],"
|
||||
+ "\"facetQueries\": [{\"query\": \"facquery\",\"label\": \"facnoused\"}],"
|
||||
+ "\"include\": [\"aspectNames\", \"properties\"]}";
|
||||
|
||||
public SerializerTestHelper()
|
||||
@@ -84,4 +91,20 @@ public class SerializerTestHelper implements RequestReader
|
||||
{
|
||||
return extractFromJson(JSON);
|
||||
}
|
||||
|
||||
public String writeResponse(final Object respons) throws IOException
|
||||
{
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
jsonHelper.withWriter(out, new Writer()
|
||||
{
|
||||
@Override
|
||||
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper)
|
||||
throws JsonGenerationException, JsonMappingException, IOException
|
||||
{
|
||||
objectMapper.writeValue(generator, respons);
|
||||
}
|
||||
});
|
||||
System.out.println(out.toString());
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
|
@@ -236,21 +236,6 @@ public class SerializeTests extends AbstractContextTest implements RecognizedPar
|
||||
assertTrue("There must 'source' json output", StringUtils.contains(out, "\"source\":{\"name\":\"Dolly\",\"age\":3,\"sheepGuid\":\"barbie\""));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIncludeContext() throws IOException
|
||||
{
|
||||
ExecutionResult exec1 = new ExecutionResult(new Farmer("180"),null);
|
||||
ExecutionResult exec2 = new ExecutionResult(new Farmer("456"),getFilter("age"));
|
||||
SearchContext searchContext = new SearchContext(23l);
|
||||
CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1, exec2), false, 2, null, searchContext);
|
||||
Object resultCollection = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null,ParamsExtender.valueOf(false,null),coll);
|
||||
assertNotNull(resultCollection);
|
||||
String out = writeResponse(resultCollection);
|
||||
assertTrue("There must 'context' json output", StringUtils.contains(out, "\"context\":{\"consistency\":{\"lastTxId\":23}}"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExpandRecursiveRelations() throws IOException
|
||||
{
|
||||
|
Reference in New Issue
Block a user