/** Used by search box auto-completion. Returns JSON array. */ public void doSuggest(StaplerRequest req, StaplerResponse rsp, @QueryParameter String query) throws IOException, ServletException { Result r = new Result(); for (SuggestedItem item : getSuggestions(req, query)) r.suggestions.add(new Item(item.getPath())); rsp.serveExposedBean(req, r, Flavor.JSON); }
/** * Gets the list of suggestions that match the given query. * * @return can be empty but never null. The size of the list is always smaller than a certain * threshold to avoid showing too many options. */ public List<SuggestedItem> getSuggestions(StaplerRequest req, String query) { Set<String> paths = new HashSet<String>(); // paths already added, to control duplicates List<SuggestedItem> r = new ArrayList<SuggestedItem>(); for (SuggestedItem i : suggest(makeSuggestIndex(req), query)) { if (r.size() > 20) break; if (paths.add(i.getPath())) r.add(i); } return r; }
/** * Used by OpenSearch auto-completion. Returns JSON array of the form: * * <pre> * ["queryString",["comp1","comp2",...]] * </pre> * * See http://developer.mozilla.org/en/docs/Supporting_search_suggestions_in_search_plugins */ public void doSuggestOpenSearch(StaplerRequest req, StaplerResponse rsp, @QueryParameter String q) throws IOException, ServletException { DataWriter w = Flavor.JSON.createDataWriter(null, rsp); w.startArray(); w.value(q); w.startArray(); for (SuggestedItem item : getSuggestions(req, q)) w.value(item.getPath()); w.endArray(); w.endArray(); }
/** * Gets the list of suggestions that match the given query. * * @return can be empty but never null. The size of the list is always smaller than a certain * threshold to avoid showing too many options. */ public SearchResult getSuggestions(StaplerRequest req, String query) { Set<String> paths = new HashSet<String>(); // paths already added, to control duplicates SearchResultImpl r = new SearchResultImpl(); int max = req.hasParameter("max") ? Integer.parseInt(req.getParameter("max")) : 20; SearchableModelObject smo = findClosestSearchableModelObject(req); for (SuggestedItem i : suggest(makeSuggestIndex(req), query, smo)) { if (r.size() >= max) { r.hasMoreResults = true; break; } if (paths.add(i.getPath())) r.add(i); } return r; }
Tag(SuggestedItem i) { item = i; distance = EditDistance.editDistance(i.getPath(), tokenList); prefixMatch = i.getPath().startsWith(tokenList) ? 1 : 0; }