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

130107 gjames: SEARCH-122: Implementing spellcheck queries


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130301 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2016-09-06 15:39:10 +00:00
parent de4bf3db68
commit d4676e8aac
9 changed files with 202 additions and 9 deletions

View File

@@ -34,9 +34,11 @@ public class SearchContext
{
private final Consistency consistency;
private final List<FacetQueryContext> facetQueries;
private final SpellCheckContext spellCheck;
public SearchContext(long lastTxId, List<FacetQueryContext> facetQueries)
public SearchContext(long lastTxId, List<FacetQueryContext> facetQueries, SpellCheckContext spellCheck)
{
this.spellCheck = spellCheck;
if (lastTxId > 0)
{
consistency = new Consistency(lastTxId);
@@ -58,6 +60,11 @@ public class SearchContext
return facetQueries;
}
public SpellCheckContext getSpellCheck()
{
return spellCheck;
}
public class Consistency
{
private final long lastTxId;

View File

@@ -0,0 +1,53 @@
/*-
* #%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.context;
import java.util.List;
/**
* The results of a SpellCheck
*/
public class SpellCheckContext
{
private final String type;
private final List<String> suggestions;
public SpellCheckContext(String type, List<String> suggestions)
{
this.type = type;
this.suggestions = suggestions;
}
public String getType()
{
return type;
}
public List<String> getSuggestions()
{
return suggestions;
}
}

View File

@@ -30,12 +30,14 @@ import org.alfresco.repo.search.impl.lucene.SolrJSONResultSet;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
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.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SpellCheckResult;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -135,6 +137,7 @@ public class ResultMapper
SearchContext context = null;
Map<String, Integer> facetQueries = solrResultSet.getFacetQueries();
List<FacetQueryContext> facetResults = null;
SpellCheckContext spellCheckContext = null;
if(facetQueries!= null && !facetQueries.isEmpty())
{
@@ -144,7 +147,13 @@ public class ResultMapper
facetResults.add(new FacetQueryContext(fq.getKey(), fq.getValue()));
}
}
context = new SearchContext(solrResultSet.getLastIndexedTxId(), facetResults);
SpellCheckResult spell = solrResultSet.getSpellCheckResult();
if (spell != null && spell.getResultName() != null && !spell.getResults().isEmpty())
{
spellCheckContext = new SpellCheckContext(spell.getResultName(),spell.getResults());
}
context = new SearchContext(solrResultSet.getLastIndexedTxId(), facetResults, spellCheckContext);
return isNullContext(context)?null:context;
}
@@ -155,7 +164,7 @@ public class ResultMapper
*/
protected boolean isNullContext(SearchContext context)
{
return (context.getFacetQueries() == null && context.getConsistency() == null);
return (context.getFacetQueries() == null && context.getConsistency() == null && context.getSpellCheck() == null);
}
/**

View File

@@ -32,6 +32,7 @@ import org.alfresco.rest.api.search.model.FilterQuery;
import org.alfresco.rest.api.search.model.Query;
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.content.BasicContentInfo;
@@ -82,7 +83,7 @@ public class SearchMapper
public static final String AFTS = "afts";
/**
* Turn the params into the Java SearchParameters object
* Turn the SearchQuery params serialized by Jackson into the Java SearchParameters object
* @param params
* @return SearchParameters
*/
@@ -101,6 +102,7 @@ public class SearchMapper
fromDefault(sp, searchQuery.getDefaults());
fromFilterQuery(sp, searchQuery.getFilterQueries());
fromFacetQuery(sp, searchQuery.getFacetQueries());
fromSpellCheck(sp, searchQuery.getSpellcheck());
return sp;
}
@@ -304,4 +306,29 @@ public class SearchMapper
}
}
/**
* SearchParameters from SpellCheck object
* @param sp SearchParameters
* @param defaults SpellCheck
*/
public void fromSpellCheck(SearchParameters sp, Spelling spelling)
{
if (spelling != null)
{
if (spelling.getQuery() != null && !spelling.getQuery().isEmpty())
{
sp.setSearchTerm(spelling.getQuery());
}
else
{
if (sp.getSearchTerm() == null || sp.getSearchTerm().isEmpty())
{
//We don't have a valid search term to use with the spelling
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID,
new Object[] { ": userQuery or spelling.query required." });
}
}
sp.setSpellCheck(true);
}
}
}

View File

@@ -27,6 +27,7 @@
package org.alfresco.rest.api.search.model;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.apache.solr.common.params.SpellingParams;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
@@ -47,8 +48,9 @@ public class SearchQuery
private final Default defaults;
private final List<FilterQuery> filterQueries;
private final List<FacetQuery> facetQueries;
private final Spelling spellcheck;
public static final SearchQuery EMPTY = new SearchQuery(null, null, null, null, null,null, null, null);
public static final SearchQuery EMPTY = new SearchQuery(null, null, null, null, null,null, null, null, null);
@JsonCreator
public SearchQuery(@JsonProperty("query") Query query,
@@ -58,7 +60,8 @@ public class SearchQuery
@JsonProperty("templates") List<Template> templates,
@JsonProperty("defaults") Default defaults,
@JsonProperty("filterQueries") List<FilterQuery> filterQueries,
@JsonProperty("facetQueries") List<FacetQuery> facetQueries)
@JsonProperty("facetQueries") List<FacetQuery> facetQueries,
@JsonProperty("spellcheck") Spelling spellcheck)
{
this.query = query;
this.paging = paging;
@@ -68,6 +71,7 @@ public class SearchQuery
this.defaults = defaults;
this.filterQueries = filterQueries;
this.facetQueries = facetQueries;
this.spellcheck = spellcheck;
}
public Query getQuery()
@@ -107,4 +111,9 @@ public class SearchQuery
{
return facetQueries;
}
public Spelling getSpellcheck()
{
return spellcheck;
}
}

View File

@@ -0,0 +1,49 @@
/*-
* #%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 query Spelling of the JSON body
**/
public class Spelling
{
private final String query;
@JsonCreator
public Spelling(@JsonProperty("query") String query)
{
this.query = query;
}
public String getQuery()
{
return query;
}
}

View File

@@ -79,6 +79,7 @@ public class ResultMapperTests
public static final String JSON_REPONSE = "{\"responseHeader\":{\"status\":0,\"QTime\":9},\"_original_parameters_\":\"org.apache.solr.common.params.DefaultSolrParams:{params(df=TEXT&alternativeDic=DEFAULT_DICTIONARY&fl=DBID,score&start=0&fq={!afts}AUTHORITY_FILTER_FROM_JSON&fq={!afts}TENANT_FILTER_FROM_JSON&rows=1000&locale=en_US&wt=json),defaults(carrot.url=id&spellcheck.collateExtendedResults=true&carrot.produceSummary=true&spellcheck.maxCollations=3&spellcheck.maxCollationTries=5&spellcheck.alternativeTermCount=2&spellcheck.extendedResults=false&defType=afts&spellcheck.maxResultsForSuggest=5&spellcheck=false&carrot.outputSubClusters=false&spellcheck.count=5&carrot.title=mltext@m___t@{http://www.alfresco.org/model/content/1.0}title&carrot.snippet=content@s___t@{http://www.alfresco.org/model/content/1.0}content&spellcheck.collate=true)}\",\"_field_mappings_\":{},\"_date_mappings_\":{},\"_range_mappings_\":{},\"_pivot_mappings_\":{},\"_interval_mappings_\":{},\"_stats_field_mappings_\":{},\"_stats_facet_mappings_\":{},\"_facet_function_mappings_\":{},\"response\":{\"numFound\":6,\"start\":0,\"maxScore\":0.7849362,\"docs\":[{\"DBID\":565,\"score\":0.7849362},{\"DBID\":566,\"score\":0.7849362},{\"DBID\":521,\"score\":0.3540957},{\"DBID\":514,\"score\":0.33025497},{\"DBID\":420,\"score\":0.32440513},{\"DBID\":415,\"score\":0.2780319}]},"
+ "\"facet_counts\":{\"facet_queries\":{\"{!afts}creator:admin\":1},\n"
+ "\"facet_fields\":{},\"facet_dates\":{},\"facet_ranges\":{},\"facet_intervals\":{}\n" + " },"
+ "\"spellcheck\":{\"searchInsteadFor\":\"alfresco\"},"
+ "\"processedDenies\":true, \"lastIndexedTx\":34}";
@BeforeClass
@@ -139,6 +140,9 @@ public class ResultMapperTests
assertEquals(1, searchContext.getFacetQueries().size());
assertEquals("{!afts}creator:admin",searchContext.getFacetQueries().get(0).getLabel());
assertEquals(1,searchContext.getFacetQueries().get(0).getCount());
assertEquals("searchInsteadFor",searchContext.getSpellCheck().getType());
assertEquals(1,searchContext.getSpellCheck().getSuggestions().size());
assertEquals("alfresco",searchContext.getSpellCheck().getSuggestions().get(0));
}
private ResultSet mockResultset(List<Long> archivedNodes) throws JSONException

View File

@@ -32,6 +32,7 @@ import static junit.framework.TestCase.fail;
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;
@@ -40,6 +41,7 @@ import org.alfresco.rest.api.search.model.FilterQuery;
import org.alfresco.rest.api.search.model.Query;
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;
@@ -329,11 +331,40 @@ public class SearchMapperTests
}
}
@Test
public void fromSpelling() throws Exception
{
SearchParameters searchParameters = new SearchParameters();
//Doesn't error
searchMapper.fromSpellCheck(searchParameters, null);
assertFalse(searchParameters.isSpellCheck());
try
{
searchMapper.fromSpellCheck(searchParameters, new Spelling(null));
fail();
}
catch (InvalidArgumentException iae)
{
//Can't be null
assertNotNull(iae);
}
//Now set search term first
searchParameters.setSearchTerm("fred");
searchMapper.fromSpellCheck(searchParameters, new Spelling(null));
assertEquals("fred",searchParameters.getSearchTerm());
//Now query replaces userQuery (search term)
searchMapper.fromSpellCheck(searchParameters, new Spelling("favourit"));
assertEquals("favourit",searchParameters.getSearchTerm());
assertTrue(searchParameters.isSpellCheck());
}
private SearchQuery minimalQuery()
{
Query query = new Query("cmis", "foo", "");
SearchQuery sq = new SearchQuery(query,null, null, null, null, null, null, null);
SearchQuery sq = new SearchQuery(query,null, null, null, null, null, null, null, null);
return sq;
}
}

View File

@@ -28,6 +28,7 @@ package org.alfresco.rest.api.search;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.alfresco.rest.api.search.context.SpellCheckContext;
import org.alfresco.rest.api.search.model.Default;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.framework.jacksonextensions.ExecutionResult;
@@ -93,15 +94,18 @@ public class SearchQuerySerializerTests
public void testSerializeContext() throws IOException
{
ExecutionResult exec1 = new ExecutionResult(new Farmer("180"),null);
SearchContext searchContext = new SearchContext(23l, Arrays.asList(new FacetQueryContext("f1", 15), new FacetQueryContext("f2", 20)));
SearchContext searchContext = new SearchContext(23l, Arrays.asList(new FacetQueryContext("f1", 15), new FacetQueryContext("f2", 20)),
new SpellCheckContext("aFlag", Arrays.asList("bish", "bash")));
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}"));
assertTrue("There must 'spellCheck' json output", out.contains("\"spellCheck\":{\"type\":\"aFlag\",\"suggestions\":[\"bish\",\"bash\"]}"));
searchContext = new SearchContext(-1, null);
searchContext = new SearchContext(-1, null, null);
coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
out = helper.writeResponse(coll);
assertTrue("There must NOT BE a 'context' json output", out.contains("\"context\":{}"));