diff --git a/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/auto-suggest-search.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/auto-suggest-search.get.desc.xml new file mode 100644 index 0000000000..b14f641c3c --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/auto-suggest-search.get.desc.xml @@ -0,0 +1,35 @@ + + Share Auto Suggest + + :/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" + } + ] + } + ]]> + + /slingshot/auto-suggest?t={term}&limit={limit?} + argument + user + required + internal + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/auto-suggest-search.get.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/auto-suggest-search.get.json.ftl new file mode 100644 index 0000000000..b1d813cec8 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/auto-suggest-search.get.json.ftl @@ -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>, + + ] +} + \ No newline at end of file diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index 3e41bcb21e..32ea02e854 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -1878,5 +1878,10 @@ + + + + + diff --git a/source/java/org/alfresco/repo/web/scripts/search/AutoSuggestSearchGet.java b/source/java/org/alfresco/repo/web/scripts/search/AutoSuggestSearchGet.java new file mode 100644 index 0000000000..9d42f73167 --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/search/AutoSuggestSearchGet.java @@ -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 . + */ + +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 auto-suggest-search.get 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 executeImpl(WebScriptRequest req, Status status, Cache cache) + { + + List list = new ArrayList<>(); + Map model = new HashMap(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> suggestedTerms = result.getSuggestions(); + for (Pair 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; + } + } +} diff --git a/source/java/org/alfresco/repo/web/scripts/search/SearchSuggestionData.java b/source/java/org/alfresco/repo/web/scripts/search/SearchSuggestionData.java new file mode 100644 index 0000000000..f001b4f501 --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/search/SearchSuggestionData.java @@ -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 . + */ + +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(); + } +}