mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-17 14:02:22 +00:00
implemented auto-completion for OpenSearch.
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@4171 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
dce296ac78
commit
cbcfe88523
2 files changed
+43
-11
No files matched your search
@@ -8,6 +8,7 @@ import org.kohsuke.stapler.StaplerResponse;
|
||||
import org.kohsuke.stapler.export.Exported;
|
||||
import org.kohsuke.stapler.export.ExportedBean;
|
||||
import org.kohsuke.stapler.export.Flavor;
|
||||
import org.kohsuke.stapler.export.DataWriter;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
@@ -46,22 +47,39 @@ public class Search {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by OpenSearch auto-completion. Returns JSON array of the form:
|
||||
*
|
||||
* <pre>
|
||||
* ["queryString",["comp1","comp2",...]]
|
||||
* </pre>
|
||||
*/
|
||||
public void doSuggestOpenSearch(StaplerRequest req, StaplerResponse rsp, @QueryParameter("q")String query) throws IOException, ServletException {
|
||||
DataWriter w = Flavor.JSON.createDataWriter(null, rsp);
|
||||
w.startArray();
|
||||
w.value(query);
|
||||
|
||||
w.startArray();
|
||||
Set<String> paths = new HashSet<String>(); // paths already added, to control duplicates
|
||||
for (SuggestedItem item : suggest(makeSuggestIndex(req), query)) {
|
||||
if(paths.size()>20) break;
|
||||
|
||||
String p = item.getPath();
|
||||
if(paths.add(p))
|
||||
w.value(p);
|
||||
}
|
||||
w.endArray();
|
||||
w.endArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by search box auto-completion. Returns JSON array.
|
||||
*/
|
||||
public void doSuggest(StaplerRequest req, StaplerResponse rsp, @QueryParameter("query")String query) throws IOException, ServletException {
|
||||
SearchIndexBuilder builder = new SearchIndexBuilder();
|
||||
for (Ancestor a : req.getAncestors()) {
|
||||
if (a.getObject() instanceof SearchableModelObject) {
|
||||
SearchableModelObject smo = (SearchableModelObject) a.getObject();
|
||||
builder.add(smo.getSearchIndex());
|
||||
}
|
||||
}
|
||||
|
||||
Result r = new Result();
|
||||
Set<String> paths = new HashSet<String>(); // paths already added, to control duplicates
|
||||
for (SuggestedItem item : suggest(builder.make(), query)) {
|
||||
if(r.suggestions.size()>20) break;
|
||||
for (SuggestedItem item : suggest(makeSuggestIndex(req), query)) {
|
||||
if(paths.size()>20) break;
|
||||
|
||||
String p = item.getPath();
|
||||
if(paths.add(p))
|
||||
@@ -71,6 +89,20 @@ public class Search {
|
||||
rsp.serveExposedBean(req,r,Flavor.JSON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates merged search index for suggestion.
|
||||
*/
|
||||
private SearchIndex makeSuggestIndex(StaplerRequest req) {
|
||||
SearchIndexBuilder builder = new SearchIndexBuilder();
|
||||
for (Ancestor a : req.getAncestors()) {
|
||||
if (a.getObject() instanceof SearchableModelObject) {
|
||||
SearchableModelObject smo = (SearchableModelObject) a.getObject();
|
||||
builder.add(smo.getSearchIndex());
|
||||
}
|
||||
}
|
||||
return builder.make();
|
||||
}
|
||||
|
||||
@ExportedBean
|
||||
public static class Result {
|
||||
@Exported
|
||||
|
||||
Reference in new issue
Block a user