提交 5bf6c0da 编写于 作者: V Vincent Latombe

Follow-up on PR #848 https://github.com/jenkinsci/jenkins/pull/848

a search context is now provided to build SuggestedItem in order to
provide correct search name and url in the given results.
上级 2135a175
......@@ -73,7 +73,7 @@ public class Search {
SearchIndex index = smo.getSearchIndex();
String query = req.getParameter("q");
if(query!=null) {
SuggestedItem target = find(index, query);
SuggestedItem target = find(index, query, smo);
if(target!=null) {
// found
rsp.sendRedirect2(a.getUrl()+target.getUrl());
......@@ -132,7 +132,8 @@ public class Search {
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;
for (SuggestedItem i : suggest(makeSuggestIndex(req), query)) {
SearchableModelObject smo = findClosestSearchableModelObject(req);
for (SuggestedItem i : suggest(makeSuggestIndex(req), query, smo)) {
if(r.size()>=max) {
r.hasMoreResults = true;
break;
......@@ -143,6 +144,17 @@ public class Search {
return r;
}
private SearchableModelObject findClosestSearchableModelObject(StaplerRequest req) {
List<Ancestor> l = req.getAncestors();
for( int i=l.size()-1; i>=0; i-- ) {
Ancestor a = l.get(i);
if (a.getObject() instanceof SearchableModelObject) {
return (SearchableModelObject)a.getObject();
}
}
return null;
}
/**
* Creates merged search index for suggestion.
*/
......@@ -225,11 +237,20 @@ public class Search {
}
/**
* Performs a search and returns the match, or null if no match was found
* or more than one match was found
* @deprecated Use {@link Search#find(SearchIndex, String, SearchableModelObject)} instead.
*/
@Deprecated
public static SuggestedItem find(SearchIndex index, String query) {
List<SuggestedItem> r = find(Mode.FIND, index, query);
return find(index, query, null);
}
/**
* Performs a search and returns the match, or null if no match was found
* or more than one match was found.
* @since XXX
*/
public static SuggestedItem find(SearchIndex index, String query, SearchableModelObject searchContext) {
List<SuggestedItem> r = find(Mode.FIND, index, query, searchContext);
if(r.isEmpty()){
return null;
}
......@@ -244,7 +265,18 @@ public class Search {
}
/**
* @deprecated use {@link Search#suggest(SearchIndex, String, SearchableModelObject)} instead.
*/
@Deprecated
public static List<SuggestedItem> suggest(SearchIndex index, final String tokenList) {
return suggest(index, tokenList, null);
}
/**
* @since XXX
*/
public static List<SuggestedItem> suggest(SearchIndex index, final String tokenList, SearchableModelObject searchContext) {
class Tag implements Comparable<Tag>{
final SuggestedItem item;
......@@ -266,7 +298,7 @@ public class Search {
}
List<Tag> buf = new ArrayList<Tag>();
List<SuggestedItem> items = find(Mode.SUGGEST, index, tokenList);
List<SuggestedItem> items = find(Mode.SUGGEST, index, tokenList, searchContext);
// sort them
for( SuggestedItem i : items)
......@@ -323,7 +355,7 @@ public class Search {
}
}
private static List<SuggestedItem> find(Mode m, SearchIndex index, String tokenList) {
private static List<SuggestedItem> find(Mode m, SearchIndex index, String tokenList, SearchableModelObject searchContext) {
TokenList tokens = new TokenList(tokenList);
if(tokens.length()==0) return Collections.emptyList(); // no tokens given
......@@ -341,7 +373,7 @@ public class Search {
items.clear();
m.find(index,token,items);
for (SearchItem si : items) {
paths[w].add(SuggestedItem.build(si));
paths[w].add(SuggestedItem.build(searchContext ,si));
LOGGER.log(Level.FINE, "found search item: {0}", si.getSearchName());
}
w++;
......
......@@ -79,23 +79,33 @@ public class SuggestedItem {
return buf.toString();
}
private static SuggestedItem build(Item top) {
private static SuggestedItem build(SearchableModelObject searchContext, Item top) {
ItemGroup<? extends Item> parent = top.getParent();
if (parent instanceof Item) {
if (parent instanceof Item && parent != searchContext) {
Item parentItem = (Item)parent;
return new SuggestedItem(build(parentItem), top);
return new SuggestedItem(build(searchContext, parentItem), top);
}
return new SuggestedItem(top);
}
/**
* Given a SearchItem, builds a SuggestedItem hierarchy by looking up parent items (if applicable).
* This allows search results for items not contained within the same {@link ItemGroup} to be distinguished.
* @since 1.527
* @deprecated Use {@link SuggestedItem#build(SearchableModelObject, SearchItem) instead.}
*/
@Deprecated
public static SuggestedItem build(SearchItem si) {
return build(null, si);
}
/**
* Given a SearchItem, builds a SuggestedItem hierarchy by looking up parent items (if applicable).
* This allows search results for items not contained within the same {@link ItemGroup} to be distinguished.
* If provided searchContext is null, results will be interpreted from the root {@link jenkins.model.Jenkins} object
* @since XXX
*/
public static SuggestedItem build(SearchableModelObject searchContext, SearchItem si) {
if (si instanceof Item) {
return build((Item)si);
return build(searchContext, (Item)si);
}
return new SuggestedItem(si);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册