Example #1
0
  public void doIndex(StaplerRequest req, StaplerResponse rsp)
      throws IOException, ServletException {
    List<Ancestor> l = req.getAncestors();
    for (int i = l.size() - 1; i >= 0; i--) {
      Ancestor a = l.get(i);
      if (a.getObject() instanceof SearchableModelObject) {
        SearchableModelObject smo = (SearchableModelObject) a.getObject();

        SearchIndex index = smo.getSearchIndex();
        String query = req.getParameter("q");
        if (query != null) {
          SuggestedItem target = find(index, query);
          if (target != null) {
            // found
            rsp.sendRedirect2(a.getUrl() + target.getUrl());
            return;
          }
        }
      }
    }

    // no exact match. show the suggestions
    rsp.setStatus(SC_NOT_FOUND);
    req.getView(this, "search-failed.jelly").forward(req, rsp);
  }
Example #2
0
  /** 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);
  }
Example #3
0
 /**
  * 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;
 }
Example #4
0
  /**
   * 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();
  }
Example #5
0
 /**
  * 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;
 }
Example #6
0
  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

    List<SuggestedItem>[] paths = new List[tokens.length() + 1]; // we won't use [0].
    for (int i = 1; i <= tokens.length(); i++) paths[i] = new ArrayList<SuggestedItem>();

    List<SearchItem> items = new ArrayList<SearchItem>(); // items found in 1 step

    LOGGER.log(Level.FINE, "tokens={0}", tokens);

    // first token
    int w = 1; // width of token
    for (String token : tokens.subSequence(0)) {
      items.clear();
      m.find(index, token, items);
      for (SearchItem si : items) {
        paths[w].add(SuggestedItem.build(searchContext, si));
        LOGGER.log(Level.FINE, "found search item: {0}", si.getSearchName());
      }
      w++;
    }

    // successive tokens
    for (int j = 1; j < tokens.length(); j++) {
      // for each length
      w = 1;
      for (String token : tokens.subSequence(j)) {
        // for each candidate
        for (SuggestedItem r : paths[j]) {
          items.clear();
          m.find(r.item.getSearchIndex(), token, items);
          for (SearchItem i : items) paths[j + w].add(new SuggestedItem(r, i));
        }
        w++;
      }
    }

    return paths[tokens.length()];
  }
Example #7
0
 Tag(SuggestedItem i) {
   item = i;
   distance = EditDistance.editDistance(i.getPath(), tokenList);
   prefixMatch = i.getPath().startsWith(tokenList) ? 1 : 0;
 }