Package org.apache.lucene.search

Examples of org.apache.lucene.search.Searcher


  }

  public Explanation explain(int documentId) {
    Explanation explanation = null;
    SearchFactoryImplementor searchFactoryImplementor = getSearchFactoryImplementor();
    Searcher searcher = buildSearcher( searchFactoryImplementor );
    if ( searcher == null ) {
      throw new SearchException(
          "Unable to build explanation for document id:"
              + documentId + ". no index found"
      );
    }
    try {
      org.apache.lucene.search.Query query = filterQueryByClasses( luceneQuery );
      buildFilters();
      explanation = searcher.explain( query, documentId );
    }
    catch ( IOException e ) {
      throw new HibernateException( "Unable to query Lucene index and build explanation", e );
    }
    finally {
View Full Code Here


   */
  public float search(Query query) {
    if (query == null)
      throw new IllegalArgumentException("query must not be null");
   
    Searcher searcher = createSearcher();
    try {
      final float[] scores = new float[1]; // inits to 0.0f (no match)
      searcher.search(query, new Collector() {
        private Scorer scorer;

        @Override
        public void collect(int doc) throws IOException {
          scores[0] = scorer.score();
View Full Code Here

    SearcherHolder searcherHolder = (SearcherHolder) ResourceBindingManager.getResource(searcherFactory);
    if (searcherHolder != null && searcherHolder.getSearcher()!=null ) {
      return searcherHolder.getSearcher();
    }

    Searcher searcher = searcherFactory.getSearcher();
    if( searcherHolder!=null ) {
      //Lazily open the search if there is an IndexHolder
      searcherHolder.setSearcher(searcher);
    }
View Full Code Here

   * @see LuceneIndexReader#createSearcher()
   */
  public LuceneSearcher createSearcher() {
    //TODO: to be implemented
    //throw new IllegalAccessException("Not implemented!");
    Searcher nativeSearcher = new IndexSearcher(indexReader);
    return new SimpleLuceneSearcher(nativeSearcher);
  }
View Full Code Here

      if( directories!=null ) {
        startSearchersIndex = directories.length;
      }
      for(int index=0; index<indexFactories.length; index++) {
        LuceneIndexReader indexReader = IndexReaderFactoryUtils.getIndexReader(indexFactories[index]);
        Searcher searcher = indexReader.createNativeSearcher();
        if( searcher!=null ) {
          searchers[index+startSearchersIndex] = searcher;
        }
      }
    }
View Full Code Here

   * This method initializes the single searcher instance.
   * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
   */
  public void afterPropertiesSet() throws Exception {
    if( getDirectory()!=null ) {
      Searcher searcher = new IndexSearcher(getDirectory());
      this.indexSearcher = new SimpleLuceneSearcher(searcher);
    } else if( getIndexFactory()!=null ) {
      LuceneIndexReader indexReader = IndexReaderFactoryUtils.getIndexReader(getIndexFactory());
      Searcher searcher = indexReader.createNativeSearcher();
      this.indexSearcher = new SimpleLuceneSearcher(searcher);
    } else {
      throw new LuceneSearchException("Either a Directory or an IndexReader must be specified.");
    }
  }
View Full Code Here

   * @return a Searcher instance
   * @see org.springmodules.lucene.search.SearcherFactory#getSearcher()
   */
  public LuceneSearcher getSearcher() throws IOException {
    if( getDirectory()!=null ) {
      Searcher indexSearcher = new IndexSearcher(getDirectory());
      return new SimpleLuceneSearcher(indexSearcher);
    } else if( getIndexFactory()!=null ) {
      LuceneIndexReader indexReader = IndexReaderFactoryUtils.getIndexReader(getIndexFactory());
      Searcher indexSearcher = indexReader.createNativeSearcher();
      return new SimpleLuceneSearcher(indexSearcher);
    } else {
      throw new LuceneSearchException("Either a Directory or an Indexreader must be specified.");
    }
  }
View Full Code Here

    writer.close();

    /*
     * Verify the index
     */        
    Searcher searcher = new IndexSearcher(dir, true);
    searcher.setSimilarity(new SimpleSimilarity());
       
    Term a = new Term("noTf", term);
    Term b = new Term("tf", term);
    Term c = new Term("noTf", "notf");
    Term d = new Term("tf", "tf");
    TermQuery q1 = new TermQuery(a);
    TermQuery q2 = new TermQuery(b);
    TermQuery q3 = new TermQuery(c);
    TermQuery q4 = new TermQuery(d);

       
    searcher.search(q1,
                    new CountingHitCollector() {
                      private Scorer scorer;
                      @Override
                      public final void setScorer(Scorer scorer) {
                        this.scorer = scorer;
                      }
                      @Override
                      public final void collect(int doc) throws IOException {
                        //System.out.println("Q1: Doc=" + doc + " score=" + score);
                        float score = scorer.score();
                        assertTrue(score==1.0f);
                        super.collect(doc);
                      }
                    });
    //System.out.println(CountingHitCollector.getCount());
       
       
    searcher.search(q2,
                    new CountingHitCollector() {
                      private Scorer scorer;
                      @Override
                      public final void setScorer(Scorer scorer) {
                        this.scorer = scorer;
                      }
                      @Override
                      public final void collect(int doc) throws IOException {
                        //System.out.println("Q2: Doc=" + doc + " score=" + score);
                        float score = scorer.score();
                        assertEquals(1.0f+doc, score, 0.00001f);
                        super.collect(doc);
                      }
                    });
    //System.out.println(CountingHitCollector.getCount());
        
       
       
       
       
    searcher.search(q3,
                    new CountingHitCollector() {
                      private Scorer scorer;
                      @Override
                      public final void setScorer(Scorer scorer) {
                        this.scorer = scorer;
                      }
                      @Override
                      public final void collect(int doc) throws IOException {
                        //System.out.println("Q1: Doc=" + doc + " score=" + score);
                        float score = scorer.score();
                        assertTrue(score==1.0f);
                        assertFalse(doc%2==0);
                        super.collect(doc);
                      }
                    });
    //System.out.println(CountingHitCollector.getCount());
       
       
    searcher.search(q4,
                    new CountingHitCollector() {
                      private Scorer scorer;
                      @Override
                      public final void setScorer(Scorer scorer) {
                        this.scorer = scorer;
                      }
                      @Override
                      public final void collect(int doc) throws IOException {
                        float score = scorer.score();
                        //System.out.println("Q1: Doc=" + doc + " score=" + score);
                        assertTrue(score==1.0f);
                        assertTrue(doc%2==0);
                        super.collect(doc);
                      }
                    });
    //System.out.println(CountingHitCollector.getCount());
       
       
       
    BooleanQuery bq = new BooleanQuery();
    bq.add(q1,Occur.MUST);
    bq.add(q4,Occur.MUST);
       
    searcher.search(bq,
                    new CountingHitCollector() {
                      @Override
                      public final void collect(int doc) throws IOException {
                        //System.out.println("BQ: Doc=" + doc + " score=" + score);
                        super.collect(doc);
                      }
                    });
    assertTrue(15 == CountingHitCollector.getCount());
       
    searcher.close();    
    dir.close();
  }
View Full Code Here

   */
  public float search(Query query) {
    if (query == null)
      throw new IllegalArgumentException("query must not be null");
   
    Searcher searcher = createSearcher();
    try {
      final float[] scores = new float[1]; // inits to 0.0f (no match)
      searcher.search(query, new HitCollector() {
        public void collect(int doc, float score) {
          scores[0] = score;
        }
      });
      float score = scores[0];
View Full Code Here

   */
  public float search(Query query) {
    if (query == null)
      throw new IllegalArgumentException("query must not be null");
   
    Searcher searcher = createSearcher();
    try {
      final float[] scores = new float[1]; // inits to 0.0f (no match)
      searcher.search(query, new Collector() {
        private Scorer scorer;

        @Override
        public void collect(int doc) throws IOException {
          scores[0] = scorer.score();
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.