Package net.sourceforge.pebble.search

Examples of net.sourceforge.pebble.search.SearchResults


  public SearchResults search(String queryString) throws SearchException {

    log.debug("Performing search : " + queryString);

    SearchResults searchResults = new SearchResults();
    searchResults.setQuery(queryString);

    if (queryString != null && queryString.length() > 0) {
      Searcher searcher = null;

      try {
        searcher = new IndexSearcher(blog.getSearchIndexDirectory());
        Query query = QueryParser.parse(queryString, "blogEntry", getAnalyzer());
        Hits hits = searcher.search(query);

        for (int i = 0; i < hits.length(); i++) {
          Document doc = hits.doc(i);
          SearchHit result = new SearchHit(
              blog,
              doc.get("id"),
              doc.get("permalink"),
              doc.get("title"),
              doc.get("truncatedBody"),
              DateField.stringToDate(doc.get("date")),
              hits.score(i));
          searchResults.add(result);
        }
      } catch (ParseException pe) {
        pe.printStackTrace();
        searchResults.setMessage("Sorry, but there was an error. Please try another search");
      } catch (Exception e) {
        e.printStackTrace();
        throw new SearchException(e.getMessage());
      } finally {
        if (searcher != null) {
View Full Code Here


  /**
   * Tests that a search can be performed on a blog entry title.
   */
   public void testSearchOnTitle() {
     try {
       SearchResults results = index.search("jsp");
       assertEquals(0, results.getNumberOfHits());

       BlogEntry blogEntry = new BlogEntry(blog);
       blogEntry.setTitle("Some JSP topic");
       blogEntry.setPublished(true);
       index.index(blogEntry);

       results = index.search("jsp");
       assertEquals(1, results.getNumberOfHits());
     } catch (Exception e) {
       e.printStackTrace();
       fail();
     }
   }
View Full Code Here

   /**
    * Tests that a search can be performed on a blog entry body.
    */
   public void testSearchOnBody() {
     try {
       SearchResults results = index.search("jsp");
       assertEquals(0, results.getNumberOfHits());

       BlogEntry blogEntry = new BlogEntry(blog);
       blogEntry.setBody("Some JSP topic");
       blogEntry.setPublished(true);
       index.index(blogEntry);

       results = index.search("jsp");
       assertEquals(1, results.getNumberOfHits());
     } catch (Exception e) {
       e.printStackTrace();
       fail();
     }
   }
View Full Code Here

   public void testSearchOnCategory() {
     try {
       blog.addCategory(new Category("/category1", "Category 1"));
       blog.addCategory(new Category("/category2", "Category 2"));

       SearchResults results = index.search("category:/category1");
       assertEquals(0, results.getNumberOfHits());

       BlogEntry blogEntry = new BlogEntry(blog);
       blogEntry.addCategory(blog.getCategory("/category1"));
       blogEntry.setPublished(true);
       index.index(blogEntry);

       results = index.search("category:/category1");
       assertEquals(1, results.getNumberOfHits());

       // and add another category
       blogEntry.addCategory(blog.getCategory("/category2"));
       index.index(blogEntry);

       results = index.search("category:/category1");
       assertEquals(1, results.getNumberOfHits());
       results = index.search("category:/category2");
       assertEquals(1, results.getNumberOfHits());
     } catch (Exception e) {
       e.printStackTrace();
       fail();
     }
   }
View Full Code Here

   /**
    * Tests that searches work across comments.
    */
   public void testSearchOnComments() {
     try {
       SearchResults results = index.search("swing");
       assertEquals(0, results.getNumberOfHits());

       BlogEntry blogEntry = new BlogEntry(blog);
       blogEntry.addCategory(blog.getCategory("/category1"));
       blogEntry.setPublished(true);
       index.index(blogEntry);

       // should be no hits returned yet
       results = index.search("swing");
       assertEquals(0, results.getNumberOfHits());

       // now add some comments
     Comment comment = blogEntry.createComment("Comment title", "Comment body", "Some author", "me@somedomain.com", "http://www.google.com", "http://graph.facebook.com/user/picture", "127.0.0.1");
       blogEntry.addComment(comment);
       index.index(blogEntry);
       results = index.search("swing");
       assertEquals(0, results.getNumberOfHits());

       comment = blogEntry.createComment("Comment title", "Comment body with Swing in it", "Some author", "me@somedomain.com", "http://www.google.com", "http://graph.facebook.com/user/picture", "127.0.0.1");
       blogEntry.addComment(comment);
       index.index(blogEntry);
       results = index.search("swing");
       assertEquals(1, results.getNumberOfHits());
     } catch (Exception e) {
       e.printStackTrace();
       fail();
     }
   }
View Full Code Here

       BlogEntry blogEntry = new BlogEntry(blog);
       blogEntry.setTitle("Some JSP topic");
       blogEntry.setPublished(true);
       index.index(blogEntry);

       SearchResults results = index.search("jsp");
       assertEquals(1, results.getNumberOfHits());

       // and now remove the blog entry, checking that the search no longer
       // returns that hit
       index.unindex(blogEntry);
       results = index.search("jsp");
       assertEquals(0, results.getNumberOfHits());
     } catch (Exception e) {
       e.printStackTrace();
       fail();
     }
   }
View Full Code Here

  public SearchResults search(String queryString) throws SearchException {

    log.debug("Performing search : " + queryString);

    SearchResults searchResults = new SearchResults();
    searchResults.setQuery(queryString);

    if (queryString != null && queryString.length() > 0) {
      Searcher searcher = null;

      try {
        searcher = new IndexSearcher(blog.getSearchIndexDirectory());
        Query query = QueryParser.parse(queryString, "blogEntry", getAnalyzer());
        Hits hits = searcher.search(query);

        for (int i = 0; i < hits.length(); i++) {
          Document doc = hits.doc(i);
          SearchHit result = new SearchHit(
              blog,
              doc.get("id"),
              doc.get("permalink"),
              doc.get("title"),
              doc.get("subtitle"),
              doc.get("truncatedBody"),
              DateField.stringToDate(doc.get("date")),
              hits.score(i));
          searchResults.add(result);
        }
      } catch (ParseException pe) {
        pe.printStackTrace();
        searchResults.setMessage("Sorry, but there was an error. Please try another search");
      } catch (Exception e) {
        e.printStackTrace();
        throw new SearchException(e.getMessage());
      } finally {
        if (searcher != null) {
View Full Code Here

        Vector posts = new Vector();
        try {

            Blog blog = getBlogWithBlogId(blogid);
            SearchResults result = blog.getSearchIndex().search( searchString );

            if ( sortBy != null && sortBy.equalsIgnoreCase("date") ) {
                result.sortByDateDescending();
            } else {
                result.sortByScoreDescending();
            }

            List<SearchHit> hits = result.getHits();
            BlogService service = new BlogService();

            for (SearchHit hit : hits) {
                BlogEntry entry = service.getBlogEntry(hit.getBlog(), hit.getId());
                adaptBlogEntry(entry);
View Full Code Here

        Vector posts = new Vector();
        try {

            Blog blog = getBlogWithBlogId(blogid);
            SearchResults result = blog.getSearchIndex().search( searchString );
           
            if ( sortBy != null && sortBy.equalsIgnoreCase("date") ) {
                result.sortByDateDescending();
            } else {
                result.sortByScoreDescending();
            }

            if ( pageSize <= 0 )
                pageSize = PAGE_SIZE;

            List<SearchHit> hits = result.getHits();
            Pageable pageable = new Pageable(hits);
            pageable.setPageSize(pageSize);
            pageable.setPage(offset);
            List<SearchHit> subList = pageable.getListForPage();
View Full Code Here

      } catch (NumberFormatException nfe) {
      }
    }

    try {
      SearchResults results = blog.getSearchIndex().search(query);

      if (results.getNumberOfHits() == 1) {
        // if there is only one hit, redirect the user to it without the
        // search results page
        SearchHit hit = (SearchHit)results.getHits().get(0);
        return new RedirectView(hit.getPermalink());
      } else {
        // show all results on the search results page
        String sort = request.getParameter("sort");
        if (sort != null && sort.equalsIgnoreCase("date")) {
          results.sortByDateDescending();
        } else {
          results.sortByScoreDescending();
        }

        Pageable pageable = new Pageable(results.getHits());
        pageable.setPageSize(PAGE_SIZE);
        pageable.setPage(page);

        try {
          getModel().put("searchResults", results);
View Full Code Here

TOP

Related Classes of net.sourceforge.pebble.search.SearchResults

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.