From c80a95d755fe5c65036ac2acc3856dac6b2478af Mon Sep 17 00:00:00 2001 From: Alan Davis Date: Tue, 6 Sep 2016 22:09:06 +0000 Subject: [PATCH] Merged 5.2.N (5.2.1) to HEAD (5.2) 130288 gjames: 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/HEAD/root@130360 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../rest/api/search/impl/ResultMapper.java | 29 ++++++++-- .../rest/api/search/impl/SearchMapper.java | 18 ++++++ .../rest/api/search/model/FacetQuery.java | 55 +++++++++++++++++++ .../rest/api/search/model/SearchQuery.java | 12 +++- .../resource/parameters/SearchContext.java | 47 ++++++++++++++-- .../rest/api/search/ResultMapperTests.java | 12 +++- .../rest/api/search/SearchMapperTests.java | 20 ++++++- .../search/SearchQuerySerializerTests.java | 35 ++++++++---- .../rest/api/search/SerializerTestHelper.java | 23 ++++++++ .../framework/tests/core/SerializeTests.java | 15 ----- 10 files changed, 228 insertions(+), 38 deletions(-) create mode 100644 source/java/org/alfresco/rest/api/search/model/FacetQuery.java diff --git a/source/java/org/alfresco/rest/api/search/impl/ResultMapper.java b/source/java/org/alfresco/rest/api/search/impl/ResultMapper.java index 379ba96d41..91dec4cf88 100644 --- a/source/java/org/alfresco/rest/api/search/impl/ResultMapper.java +++ b/source/java/org/alfresco/rest/api/search/impl/ResultMapper.java @@ -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 facetQueries = solrResultSet.getFacetQueries(); + List facetResults = null; - if (solrResultSet.getLastIndexedTxId() > 0) + if(facetQueries!= null && !facetQueries.isEmpty()) { - context = new SearchContext(solrResultSet.getLastIndexedTxId()); + facetResults = new ArrayList<>(facetQueries.size()); + for (Entry 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); } /** diff --git a/source/java/org/alfresco/rest/api/search/impl/SearchMapper.java b/source/java/org/alfresco/rest/api/search/impl/SearchMapper.java index 6aa30d109b..786f4db9a4 100644 --- a/source/java/org/alfresco/rest/api/search/impl/SearchMapper.java +++ b/source/java/org/alfresco/rest/api/search/impl/SearchMapper.java @@ -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 + * @param sp + * @param facetQueries + */ + public void fromFacetQuery(SearchParameters sp, List facetQueries) + { + if (facetQueries != null && !facetQueries.isEmpty()) + { + for (FacetQuery fq:facetQueries) + { + sp.addFacetQuery(fq.getQuery()); + } + } + } + } diff --git a/source/java/org/alfresco/rest/api/search/model/FacetQuery.java b/source/java/org/alfresco/rest/api/search/model/FacetQuery.java new file mode 100644 index 0000000000..34ff0cc073 --- /dev/null +++ b/source/java/org/alfresco/rest/api/search/model/FacetQuery.java @@ -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 . + * #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; + } +} diff --git a/source/java/org/alfresco/rest/api/search/model/SearchQuery.java b/source/java/org/alfresco/rest/api/search/model/SearchQuery.java index 42744821b4..f55a7627b2 100644 --- a/source/java/org/alfresco/rest/api/search/model/SearchQuery.java +++ b/source/java/org/alfresco/rest/api/search/model/SearchQuery.java @@ -46,8 +46,9 @@ public class SearchQuery private final List