Merged HEAD-BUG-FIX (5.0/Cloud) to HEAD (5.0/Cloud)

84921: Merged PLATFORM1 (5.0/Cloud) to HEAD-BUG-FIX (5.0/Cloud)
      83449: ACE-2612: Added auto-suggest web script. Also, minor modification to Share search service. 


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@85238 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Mark Rogers
2014-09-20 09:10:28 +00:00
parent 9abe30d450
commit 8ea31336da
5 changed files with 221 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<webscript>
<shortname>Share Auto Suggest</shortname>
<description>
<![CDATA[
Get search term suggestions
http://<host>:<port>/alfresco/service/slingshot/auto-suggest?t={term}&limit={limit?}
Example response from this web script for the term "tes":
{
"suggestions" :
[
{
"weight" : 5,
"term" : "test"
},
{
"weight" : 1,
"term" : "test call"
},
{
"weight" : 1,
"term" : "test plan"
}
]
}
]]>
</description>
<url>/slingshot/auto-suggest?t={term}&amp;limit={limit?}</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction allow="readonly">required</transaction>
<lifecycle>internal</lifecycle>
</webscript>

View File

@@ -0,0 +1,13 @@
<#escape x as jsonUtils.encodeJSONString(x)>
{
"suggestions" :
[
<#list suggestions as suggestion>
{
"weight" : ${suggestion.weight?c},
"term" : "${suggestion.term}"
}<#if suggestion_has_next>,</#if>
</#list>
]
}
</#escape>

View File

@@ -1878,5 +1878,10 @@
<ref bean="OwnableService" /> <ref bean="OwnableService" />
</property> </property>
</bean> </bean>
<!-- AutoSuggest web script -->
<bean id="webscript.org.alfresco.slingshot.search.auto-suggest-search.get" class="org.alfresco.repo.web.scripts.search.AutoSuggestSearchGet" parent="webscript">
<property name="suggesterService" ref="suggesterService"/>
</bean>
</beans> </beans>

View File

@@ -0,0 +1,111 @@
/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.repo.web.scripts.search;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.service.cmr.search.SuggesterResult;
import org.alfresco.service.cmr.search.SuggesterService;
import org.alfresco.util.Pair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* This class is the controller for the <i>auto-suggest-search.get</i> web
* script.
*
* @author Jamal Kaabi-Mofrad
* @since 5.0
*/
public class AutoSuggestSearchGet extends DeclarativeWebScript
{
private static final Log logger = LogFactory.getLog(AutoSuggestSearchGet.class);
private static final String TERM = "t";
private static final String LIMIT = "limit";
private static final String SUGGESTIONS = "suggestions";
private SuggesterService suggesterService;
public void setSuggesterService(SuggesterService suggesterService)
{
this.suggesterService = suggesterService;
}
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
List<SearchSuggestionData> list = new ArrayList<>();
Map<String, Object> model = new HashMap<String, Object>(1);
model.put(SUGGESTIONS, list);
if (!suggesterService.isEnabled())
{
return model;
}
String term = req.getParameter(TERM);
int limit = getLimit(req.getParameter(LIMIT));
if (term == null || term.isEmpty())
{
return model;
}
SuggesterResult result = suggesterService.getSuggestions(term, limit);
List<Pair<String, Integer>> suggestedTerms = result.getSuggestions();
for (Pair<String, Integer> pair : suggestedTerms)
{
list.add(new SearchSuggestionData(pair.getFirst(), pair.getSecond()));
}
if (logger.isDebugEnabled())
{
logger.debug("Suggested terms for the [" + term + "] are: " + list);
}
return model;
}
private int getLimit(String limit)
{
if (limit == null)
{
return -1;
}
try
{
return Integer.parseInt(limit);
}
catch (NumberFormatException ne)
{
return -1;
}
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.repo.web.scripts.search;
/**
* Basic POJO to represent a term suggestion.
*
* @author Jamal Kaabi-Mofrad
* @since 5.0
*/
public class SearchSuggestionData
{
private final String term;
private final int weight;
public SearchSuggestionData(String term, int weight)
{
this.term = term;
this.weight = weight;
}
public String getTerm()
{
return this.term;
}
public int getWeight()
{
return this.weight;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder(100);
builder.append("SearchSuggestionData [term=").append(this.term).append(", weight=").append(this.weight)
.append("]");
return builder.toString();
}
}