Package org.apache.lucene.search

Examples of org.apache.lucene.search.Searcher


import org.apache.lucene.queryParser.QueryParser;

class SearchFiles {
  public static void main(String[] args) {
    try {
      Searcher searcher = new IndexSearcher("index");
      Analyzer analyzer = new StandardAnalyzer();

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      while (true) {
  System.out.print("Query: ");
  String line = in.readLine();

  if (line.length() == -1)
    break;

  Query query = QueryParser.parse(line, "contents", analyzer);
  System.out.println("Searching for: " + query.toString("contents"));

  Hits hits = searcher.search(query);
  System.out.println(hits.length() + " total matching documents");

  final int HITS_PER_PAGE = 10;
  for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
    int end = Math.min(hits.length(), start + HITS_PER_PAGE);
    for (int i = start; i < end; i++) {
      Document doc = hits.doc(i);
      String path = doc.get("path");
      if (path != null) {
              System.out.println(i + ". " + path);
      } else {
              String url = doc.get("url");
        if (url != null) {
    System.out.println(i + ". " + url);
    System.out.println("   - " + doc.get("title"));
        } else {
    System.out.println(i + ". " + "No path nor URL for this document");
        }
      }
    }

    if (hits.length() > end) {
      System.out.print("more (y/n) ? ");
      line = in.readLine();
      if (line.length() == 0 || line.charAt(0) == 'n')
        break;
    }
  }
      }
      searcher.close();

    } catch (Exception e) {
      System.out.println(" caught a " + e.getClass() +
       "\n with message: " + e.getMessage());
    }
View Full Code Here


        Searcher[] searchers = new Searcher[2];
        // VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
        searchers[0] = new IndexSearcher(indexStoreB);
        searchers[1] = new IndexSearcher(indexStoreA);
        // creating the multiSearcher
        Searcher mSearcher = new MultiSearcher(searchers);
        // performing the search
        Hits hits = mSearcher.search(query);

        assertEquals(3, hits.length());

        try {
            // iterating over the hit documents
            for (int i = 0; i < hits.length(); i++) {
                Document d = hits.doc(i);
            }
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            fail("ArrayIndexOutOfBoundsException thrown: " + e.getMessage());
            e.printStackTrace();
        } finally{
            mSearcher.close();
        }


        //--------------------------------------------------------------------
        // scenario 2
        //--------------------------------------------------------------------

        // adding one document to the empty index
        writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
        writerB.addDocument(lDoc);
        writerB.optimize();
        writerB.close();

        // building the searchables
        Searcher[] searchers2 = new Searcher[2];
        // VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
        searchers2[0] = new IndexSearcher(indexStoreB);
        searchers2[1] = new IndexSearcher(indexStoreA);
        // creating the mulitSearcher
        Searcher mSearcher2 = new MultiSearcher(searchers2);
        // performing the same search
        Hits hits2 = mSearcher2.search(query);

        assertEquals(4, hits2.length());

        try {
            // iterating over the hit documents
            for (int i = 0; i < hits2.length(); i++) {
                // no exception should happen at this point
                Document d = hits2.doc(i);
            }
        }
        catch (Exception e)
        {
            fail("Exception thrown: " + e.getMessage());
            e.printStackTrace();
        } finally{
            mSearcher2.close();
        }

        //--------------------------------------------------------------------
        // scenario 3
        //--------------------------------------------------------------------

        // deleting the document just added, this will cause a different exception to take place
        Term term = new Term("id", "doc1");
        IndexReader readerB = IndexReader.open(indexStoreB);
        readerB.delete(term);
        readerB.close();

        // optimizing the index with the writer
        writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
        writerB.optimize();
        writerB.close();

        // building the searchables
        Searcher[] searchers3 = new Searcher[2];

        searchers3[0] = new IndexSearcher(indexStoreB);
        searchers3[1] = new IndexSearcher(indexStoreA);
        // creating the mulitSearcher
        Searcher mSearcher3 = new MultiSearcher(searchers3);
        // performing the same search
        Hits hits3 = mSearcher3.search(query);

        assertEquals(3, hits3.length());

        try {
            // iterating over the hit documents
            for (int i = 0; i < hits3.length(); i++) {
                Document d = hits3.doc(i);
            }
        }
        catch (IOException e)
        {
            fail("IOException thrown: " + e.getMessage());
            e.printStackTrace();
        } finally{
            mSearcher3.close();
        }
    }
View Full Code Here

  public ArrayList search(String item, boolean content){
    try {
      ArrayList results = new ArrayList();
      IndexReader reader = IndexReader.open(storeIndex);
      Searcher searcher = new IndexSearcher(reader);
      Analyzer analyzer = new StandardAnalyzer();
      Query query = QueryParser.parse(item,"TextPath",analyzer);
      Hits hits = searcher.search(query);
      for(int i = 0; i < hits.length() && i < QueryManager.resultSize; i++){
        Document doc = hits.doc(i);
        String path = doc.get("Path");
        if(!results.contains(path))
          results.add(path);
      }
      if(content){
        query = QueryParser.parse(item, "Contents", analyzer);
        hits = searcher.search(query);
        for (int i = 0; i < hits.length() && i < QueryManager.resultSize; i++) {
          Document doc = hits.doc(i);
          String path = doc.get("Path");
          if (!results.contains(path))
            results.add(path);
        }
        query = QueryParser.parse(item, "ExtendedInfosContent", analyzer);
        hits = searcher.search(query);
        for (int i = 0; i < hits.length() && i < QueryManager.resultSize; i++) {
          Document doc = hits.doc(i);
          String path = doc.get("Path");
          if (!results.contains(path))
            results.add(path);
        }
      }
      query = QueryParser.parse(item,"PartialFileName",analyzer);
      hits = searcher.search(query);
      for(int i = 0; i < hits.length() && i < QueryManager.resultSize; i++){
        Document doc = hits.doc(i);
        StringHash hash = new StringHash(doc.get("PartialFileHash"));
        if(!results.contains(hash))
          results.add(hash);
View Full Code Here

  public ArrayList searchRemoteFiles(String item, boolean localSearch){
    try {
      ArrayList results = new ArrayList();
      IndexReader reader = IndexReader.open(remoteStoreIndex);
      Searcher searcher = new IndexSearcher(reader);
      Analyzer analyzer = new StandardAnalyzer();
      Query query = QueryParser.parse(item,"RemoteFileContent",analyzer);
      Hits hits = searcher.search(query);
      for(int i = 0; i < hits.length() && (localSearch || i < QueryManager.resultSize); i++){
        Document doc = hits.doc(i);
        String hash = doc.get("RemoteFileHash");
        if(!results.contains(hash))
          results.add(hash);
View Full Code Here

@throws SearchException if the search messes up!
         */
        public Hits doSearch(SearchQuery searchFor) throws SearchException, Exception
         {
                //Local Variables
                Searcher searcher = null; //index to search
                Analyzer analyzer = new RpAnalyzer(); //analysis of this index

                Query query = null; //the (parsed) query
                Hits hits = null; //the hits against the database

                Document doc = null; //ID of current Document

                log.debug(" Original Query: " + searchFor);

                try
                 {
                        //Get a handle to the index
                        searcher = new IndexSearcher(Constants.INDEX_NAME);

                        //Build the query into Lucene specific format
                        query = QueryParser.parse(searchFor.getSearchString(), "contents", analyzer);

                        log.debug("Query Class:" + query.getClass().getName());
                        log.debug("Searching for: " + query.toString("contents"));
                        log.debug("Searching for: " + query.toString());

                        //Do the Search
                        hits = searcher.search(query);

                        log.debug("Number of Results:" + hits.length());

                        //Loop and display the results
                        for (int counter = 0; counter < hits.length(); counter++)
                         {
                                doc = hits.doc(counter);
                                log.debug("Rank:" + hits.score(counter) + " Info:" + doc.toString());
                        }

                        searcher.close();
                       
                } catch (java.io.IOException iex)
                 {
                        throw new SearchException(iex);
                } catch (org.apache.lucene.queryParser.ParseException pex)
View Full Code Here

     @throws ProviderException if there is a problem with the backend
     */
    public Collection findPages( String query, int flags )
        throws ProviderException
    {
        Searcher  searcher = null;
        ArrayList<SearchResult> list = null;
        Highlighter highlighter = null;

        try
        {
            String[] queryfields = { LUCENE_PAGE_CONTENTS, LUCENE_PAGE_NAME, LUCENE_AUTHOR, LUCENE_ATTACHMENTS };
            QueryParser qp = new MultiFieldQueryParser( queryfields, getLuceneAnalyzer() );

            //QueryParser qp = new QueryParser( LUCENE_PAGE_CONTENTS, getLuceneAnalyzer() );
            Query luceneQuery = qp.parse( query );

            if( (flags & FLAG_CONTEXTS) != 0 )
            {
                highlighter = new Highlighter(new SimpleHTMLFormatter("<span class=\"searchmatch\">", "</span>"),
                                              new SimpleHTMLEncoder(),
                                              new QueryScorer(luceneQuery));
            }

            try
            {
                searcher = new IndexSearcher(m_luceneDirectory);
            }
            catch( Exception ex )
            {
                log.info("Lucene not yet ready; indexing not started",ex);
                return null;
            }

            Hits hits = searcher.search(luceneQuery);

            list = new ArrayList<SearchResult>(hits.length());
            for ( int curr = 0; curr < hits.length(); curr++ )
            {
                Document doc = hits.doc(curr);
                String pageName = doc.get(LUCENE_ID);
                WikiPage page = m_engine.getPage(pageName, WikiPageProvider.LATEST_VERSION);

                if(page != null)
                {
                    if(page instanceof Attachment)
                    {
                        // Currently attachments don't look nice on the search-results page
                        // When the search-results are cleaned up this can be enabled again.
                    }

                    int score = (int)(hits.score(curr) * 100);


                    // Get highlighted search contexts
                    String text = doc.get(LUCENE_PAGE_CONTENTS);

                    String[] fragments = new String[0];
                    if( text != null && highlighter != null )
                    {
                        TokenStream tokenStream = getLuceneAnalyzer()
                        .tokenStream(LUCENE_PAGE_CONTENTS, new StringReader(text));
                        fragments = highlighter.getBestFragments(tokenStream,
                                                                 text, MAX_FRAGMENTS);

                    }

                    SearchResult result = new SearchResultImpl( page, score, fragments );    
                    list.add(result);
                }
                else
                {
                    log.error("Lucene found a result page '" + pageName + "' that could not be loaded, removing from Lucene cache");
                    pageRemoved(new WikiPage( m_engine, pageName ));
                }
            }
        }
        catch( IOException e )
        {
            log.error("Failed during lucene search",e);
        }
        catch( InstantiationException e )
        {
            log.error("Unable to get a Lucene analyzer",e);
        }
        catch( IllegalAccessException e )
        {
            log.error("Unable to get a Lucene analyzer",e);
        }
        catch( ClassNotFoundException e )
        {
            log.error("Specified Lucene analyzer does not exist",e);
        }
        catch( ParseException e )
        {
            log.info("Broken query; cannot parse",e);

            throw new ProviderException("You have entered a query Lucene cannot process: "+e.getMessage());
        }
        finally
        {
            if( searcher != null )
            {
                try
                {
                    searcher.close();
                }
                catch( IOException e )
                {}
            }
        }
View Full Code Here

    SearchResult result=new SearchResult();
   
    List<ZoieIndexReader<R>> readers=null;

    MultiReader multiReader=null;
    Searcher searcher = null;
    try
    {
      Query q=null;
      if (queryString == null || queryString.length() ==0)
      {
        q = new MatchAllDocsQuery();
      }
      else
      {
        q = qparser.parse(queryString);
      }
      readers=_idxReaderFactory.getIndexReaders();
      multiReader=new MultiReader(readers.toArray(new IndexReader[readers.size()]), false);
      searcher=new IndexSearcher(multiReader);
     
      long start=System.currentTimeMillis();
      TopDocs docs=searcher.search(q, null, 10);
      long end=System.currentTimeMillis();
     
      result.setTime(end-start);
      result.setTotalDocs(multiReader.numDocs());
      result.setTotalHits(docs.totalHits);
     

      ScoreDoc[] scoreDocs=docs.scoreDocs;
      ArrayList<SearchHit> hitList=new ArrayList<SearchHit>(scoreDocs.length);
      for (ScoreDoc scoreDoc : scoreDocs)
      {
        SearchHit hit=new SearchHit();
        hit.setScore(scoreDoc.score);
        int docid=scoreDoc.doc;
       
        Document doc=multiReader.document(docid);
        String content=doc.get("content");
       
        Scorer qs=new QueryScorer(q);
       
        SimpleHTMLFormatter formatter=new SimpleHTMLFormatter("<span class=\"hl\">","</span>");
        Highlighter hl=new Highlighter(formatter,qs);
        String[] fragments=hl.getBestFragments(analyzer, "content",content, 1);
       
        Map<String,String[]> fields=convert(doc);
        fields.put("fragment",fragments);
        hit.setFields(fields);
        hitList.add(hit);
      }
     
      result.setHits(hitList.toArray(new SearchHit[hitList.size()]));
      return result;
    }
    catch(Exception e)
    {
      log.error(e.getMessage(),e);
      throw new ZoieException(e.getMessage(),e);
    }
    finally
    {
      try{
        if (searcher!=null)
        {
        try {
          searcher.close();
        } catch (IOException e) {
          log.error(e.getMessage(),e);
        }
        finally{
            if (multiReader!=null){
View Full Code Here

      if (i%113 !=0) continue;
      long flushtime = System.currentTimeMillis();
      int numDoc = -1;
      List<ZoieIndexReader<IndexReader>> readers=null;
      IndexReader reader = null;
      Searcher searcher = null;
      int oldNum = -1;
      while(numDoc < i + 1)
      {
        if (reader!=null && readers!=null)
        {
          searcher.close();
          searcher = null;
          reader.close();
          hourglass.returnIndexReaders(readers);
        }
        readers = hourglass.getIndexReaders();
        reader = new MultiReader(readers.toArray(new IndexReader[0]),false);
        searcher = new IndexSearcher(reader);
        TopDocs hitsall = searcher.search(new MatchAllDocsQuery(), 10);
        numDoc = hitsall.totalHits;
        if (numDoc!=oldNum)System.out.println("numDoc: " + numDoc);
        oldNum = numDoc;
        Thread.sleep(30);
      }
      accumulatedTime += (System.currentTimeMillis() - flushtime);
      TopDocs hits = searcher.search(new TermQuery(new Term("contents",""+i)), 10);
      TopDocs hitsall = searcher.search(new MatchAllDocsQuery(), 10);
      try
      {
        assertEquals("one hit for " + i, 1, hits.totalHits);
        assertEquals("MatchAllDocsHit ", i+1, hitsall.totalHits);
      } finally
      {
        searcher.close();
        searcher = null;
        reader.close();
        reader = null;
        hourglass.returnIndexReaders(readers);
        readers = null;
View Full Code Here

          }

          int expected = TestData.testdata.length;
          while(!stop)
          {
            Searcher searcher = null;
            List<ZoieIndexReader<IndexReader>> readers = null;
            MultiReader reader=null;
            try
            {
              readers=idxSystem.getIndexReaders();
              reader=new MultiReader(readers.toArray(new IndexReader[readers.size()]),false);

              searcher=new IndexSearcher(reader);

              TopDocs hits = searcher.search(q,10);
              int count = hits.totalHits;

              if (count != expected)
              {
                mismatch = true;
                message = "hit count: " + count +" / expected: "+expected;
                stop = true;
                StringBuffer sb = new StringBuffer();
                sb.append(message + "\n");
                sb.append("each\n");
                sb.append(groupDump(readers, q));
                sb.append("main\n");
                sb.append(dump(reader, hits));
                System.out.println(sb.toString());
                log.info(sb.toString());
              }
              Thread.sleep(20);
            }
            catch(Exception ex)
            {
              ex.printStackTrace();
              exception = ex;
              stop = true;
            }
            finally
            {
              try{
                if (searcher != null){
                  searcher.close();
                  reader.close();
                  reader = null;
                  searcher = null;
                }
              }
              catch(IOException ioe){
                log.error(ioe.getMessage(),ioe);
              }
              finally{
                idxSystem.returnIndexReaders(readers);
              }
            }
          }
        }
        private String groupDump(List<ZoieIndexReader<IndexReader>> readers, Query q) throws IOException
        {
          StringBuffer sb = new StringBuffer();
          for(ZoieIndexReader<IndexReader> reader : readers)
          {
            sb.append(reader).append("\n");
            Searcher searcher = new IndexSearcher(reader);
            TopDocs hits = searcher.search(q, 20);
            sb.append(dump(reader, hits));
            searcher.close();
            searcher = null;
          }
          return sb.toString();
        }
View Full Code Here

          }

          int expected = testdata.length;
          while(!stop)
          {
            Searcher searcher = null;
            List<ZoieIndexReader<IndexReader>> readers = null;
            MultiReader reader=null;
            try
            {
              readers=idxSystem.getIndexReaders();
              reader=new MultiReader(readers.toArray(new IndexReader[readers.size()]),false);

              searcher=new IndexSearcher(reader);

              TopDocs hits = searcher.search(q,10);
              int count = hits.totalHits;

              if (count != expected)
              {
                mismatch = true;
                message = "hit count: " + count +" / expected: "+expected;
                stop = true;
                StringBuffer sb = new StringBuffer();
                sb.append(message + "\n");
                sb.append("each\n");
                sb.append(groupDump(readers, q));
                sb.append("main\n");
                sb.append(dump(reader, hits));
                System.out.println(sb.toString());
                log.info(sb.toString());
              }
              Thread.sleep(20);
            }
            catch(Exception ex)
            {
              ex.printStackTrace();
              exception = ex;
              stop = true;
            }
            finally
            {
              try{
                if (searcher != null){
                  searcher.close();
                  reader.close();
                  reader = null;
                  searcher = null;
                }
              }
              catch(IOException ioe){
                log.error(ioe.getMessage(),ioe);
              }
              finally{
                idxSystem.returnIndexReaders(readers);
              }
            }
          }
        }
        private String groupDump(List<ZoieIndexReader<IndexReader>> readers, Query q) throws IOException
        {
          StringBuffer sb = new StringBuffer();
          for(ZoieIndexReader<IndexReader> reader : readers)
          {
            sb.append(reader).append("\n");
            Searcher searcher = new IndexSearcher(reader);
            TopDocs hits = searcher.search(q, 20);
            sb.append(dump(reader, hits));
            searcher.close();
            searcher = null;
          }
          return sb.toString();
        }
View Full Code Here

TOP

Related Classes of org.apache.lucene.search.Searcher

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.