コード例 #1
0
  /**
   * {@inheritDoc}
   *
   * <p>Search implementation using Compass.
   */
  @SuppressWarnings("unchecked")
  public List<T> search(String q, Class clazz) {
    if (q == null || "".equals(q.trim())) {
      return getAll();
    }

    List<T> results = new ArrayList<T>();

    CompassSearchCommand command = new CompassSearchCommand(q);
    CompassSearchResults compassResults = compass.search(command);
    CompassHit[] hits = compassResults.getHits();

    if (log.isDebugEnabled() && clazz != null) {
      log.debug("Filtering by type: " + clazz.getName());
    }

    for (CompassHit hit : hits) {
      if (clazz != null) {
        if (hit.data().getClass().equals(clazz)) {
          results.add((T) hit.data());
        }
      } else {
        results.add((T) hit.data());
      }
    }

    if (log.isDebugEnabled()) {
      log.debug("Number of results for '" + q + "': " + results.size());
    }

    return results;
  }
コード例 #2
0
 /* (non-Javadoc)
  * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  */
 @Override
 protected ModelAndView handleRequestInternal(
     HttpServletRequest request, HttpServletResponse response) throws Exception {
   String query = request.getParameter("query");
   if (StringUtils.isBlank(query)) {
     return new ModelAndView(getSearchView());
   }
   String p = request.getParameter("p");
   int currentPage = 1;
   if (NumberUtils.isNumber(p)) {
     currentPage = Integer.parseInt(p);
   }
   if (currentPage < 1) {
     currentPage = 1;
   }
   CompassSearchCommand searchCommand = new CompassSearchCommand();
   searchCommand.setPage(new Integer(currentPage - 1));
   searchCommand.setQuery(query);
   ModelAndView mv = new ModelAndView();
   mv.addObject("query", query);
   mv.addObject("p", currentPage);
   List<String> errors = new ArrayList<String>();
   try {
     CompassSearchResults searchResults = searchHelper.search(searchCommand);
     List<CompassHitWapper> hits = new ArrayList<CompassHitWapper>();
     for (int i = 0; i < searchResults.getHits().length; i++) {
       CompassHitWapper hit = new CompassHitWapper(searchResults.getHits()[i]);
       hits.add(hit);
     }
     CompassSearchResultsWrapper results = new CompassSearchResultsWrapper(searchResults);
     results.setHits(hits);
     mv.addObject(getSearchResultsName(), results);
   } catch (SearchEngineQueryParseException ex) {
     errors.add(TextUtil.escapeHTML(ex.getMessage()));
     mv.addObject("errors", errors);
   } catch (Exception ex) {
     errors.add(TextUtil.escapeHTML(ex.getMessage()));
     mv.addObject("errors", errors);
   }
   mv.setViewName(getSearchResultsView());
   return mv;
 }