Package org.apache.lucene.search

Examples of org.apache.lucene.search.Searcher


    private static String[] queryImpl( String aLanguageStr, String aIndexStr, String aQueryStr,
      boolean bCaptionOnly, Object[] aScoreOutArray ) throws Exception
    {
      IndexReader reader = IndexReader.open( aIndexStr );
      Searcher searcher = new IndexSearcher( reader );
            Analyzer analyzer = aLanguageStr.equals("ja") ? (Analyzer)new CJKAnalyzer() : (Analyzer)new StandardAnalyzer();

      String aField;
      if( bCaptionOnly )
        aField = "caption";
      else
        aField = "content";

      Query aQuery;
      if( aQueryStr.endsWith( "*" ) )
        aQuery = new WildcardQuery( new Term( aField, aQueryStr ) );
      else
        aQuery = new TermQuery( new Term( aField, aQueryStr ) );

      // Perform search
      Hits aHits = searcher.search( aQuery );
      int nHitCount = aHits.length();

      String aDocs[] = new String[nHitCount]
      float aScores[] = null;
      aScores = new float[nHitCount];
View Full Code Here


    private void assertRecordCount( RepositoryContentIndex index, int expectedCount )
        throws Exception
    {
        Query query = new MatchAllDocsQuery();
        Searcher searcher = (Searcher) index.getSearchable();
        Hits hits = searcher.search( query );
        assertEquals( "Expected Record Count for " + index.getId(), expectedCount, hits.length() );
    }
View Full Code Here

    }

    protected List search( Query query )
        throws RepositoryIndexSearchException, IOException, java.text.ParseException
    {
        Searcher searcher = (Searcher) index.getSearchable();; // this shouldn't cause a problem.

        Hits hits = searcher.search( query );

        List results = new ArrayList();
        Iterator it = hits.iterator();
        while ( it.hasNext() )
        {
View Full Code Here

    protected void tearDown() throws Exception {
        super.tearDown();
    }
    public void testFilter() throws IOException{
        Searcher s = new IndexSearcher(this.reader);
        Query q = new TermQuery(new Term(fieldFeedId,feedID));
        Hits hits = s.search(q);
        assertEquals(2,hits.length());
        
        hits = s.search(q,new ModifiedEntryFilter(this.excludeList.toArray(new String[0]),StorageEntryWrapper.FIELD_ENTRY_ID));
        assertEquals(1,hits.length());
        this.excludeList.add("2");
        hits = s.search(q,new ModifiedEntryFilter(this.excludeList.toArray(new String[0]),StorageEntryWrapper.FIELD_ENTRY_ID));
        assertEquals(0,hits.length());
        this.excludeList.add(null);
        this.excludeList.add("5");
        hits = s.search(q,new ModifiedEntryFilter(this.excludeList.toArray(new String[0]),StorageEntryWrapper.FIELD_ENTRY_ID));
        assertEquals(0,hits.length());
        
    }
View Full Code Here

        RAMDirectory dir = new RAMDirectory();
        IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
        writer.addDocument(makeDocumentWithFields());
        writer.close();

        Searcher searcher = new IndexSearcher(dir);

  // search for something that does exists
  Query query = new TermQuery(new Term("keyword", "test1"));

  // ensure that queries return expected results without DateFilter first
        Hits hits = searcher.search(query);
  assertEquals(1, hits.length());

         doAssert(hits.doc(0), true);
         searcher.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

    }
  }
   
  private float query(Object index, Query query) {
//    System.out.println("MB=" + (getMemorySize(index) / (1024.0f * 1024.0f)));
    Searcher searcher = null;
    try {
      if (index instanceof Directory)
        searcher = new IndexSearcher((Directory)index);
      else
        searcher = ((MemoryIndex) index).createSearcher();

      final float[] scores = new float[1]; // inits to 0.0f
      searcher.search(query, new HitCollector() {
        public void collect(int doc, float score) {
          scores[0] = score;
        }
      });
      float score = scores[0];
//      Hits hits = searcher.search(query);
//      float score = hits.length() > 0 ? hits.score(0) : 0.0f;
      return score;
    } catch (IOException e) { // should never happen (RAMDirectory)
      throw new RuntimeException(e);
    } finally {
      try {
        if (searcher != null) searcher.close();
      } catch (IOException e) { // should never happen (RAMDirectory)
        throw new RuntimeException(e);
      }
    }
  }
View Full Code Here

    Path localDir = new Path(getUnitTestdir(getName()), "index_" +
      Integer.toString(new Random().nextInt()));
    this.fs.copyToLocalFile(new Path(INDEX_DIR), localDir);
    FileSystem localfs = FileSystem.getLocal(conf);
    FileStatus [] indexDirs = localfs.listStatus(localDir);
    Searcher searcher = null;
    Scanner scanner = null;
    try {
      if (indexDirs.length == 1) {
        searcher = new IndexSearcher((new File(indexDirs[0].getPath().
          toUri())).getAbsolutePath());
      } else if (indexDirs.length > 1) {
        Searchable[] searchers = new Searchable[indexDirs.length];
        for (int i = 0; i < indexDirs.length; i++) {
          searchers[i] = new IndexSearcher((new File(indexDirs[i].getPath().
            toUri()).getAbsolutePath()));
        }
        searcher = new MultiSearcher(searchers);
      } else {
        throw new IOException("no index directory found");
      }

      HTable table = new HTable(conf, TABLE_NAME);
      scanner = table.getScanner(columns, HConstants.EMPTY_START_ROW);

      IndexConfiguration indexConf = new IndexConfiguration();
      String content = conf.get("hbase.index.conf");
      if (content != null) {
        indexConf.addFromXML(content);
      }
      String rowkeyName = indexConf.getRowkeyName();

      int count = 0;
      for (RowResult r : scanner) {
        String value = Bytes.toString(r.getRow());
        Term term = new Term(rowkeyName, value);
        int hitCount = searcher.search(new TermQuery(term)).length();
        assertEquals("check row " + value, 1, hitCount);
        count++;
      }
      LOG.debug("Searcher.maxDoc: " + searcher.maxDoc());
      LOG.debug("IndexReader.numDocs: " + ((IndexSearcher)searcher).getIndexReader().numDocs());     
      int maxDoc = ((IndexSearcher)searcher).getIndexReader().numDocs();
      assertEquals("check number of rows", maxDoc, count);
    } finally {
      if (null != searcher)
        searcher.close();
      if (null != scanner)
        scanner.close();
    }
  }
View Full Code Here

     * @return A Hits object containing the matches
     * @throws Exception if an error occurs
     *
     */
    public Hits search(String line, File index_directory) throws Exception {
        Searcher searcher = new IndexSearcher(index_directory.getAbsolutePath());
        Analyzer analyzer = new StandardAnalyzer();

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

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

                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");
                            }
                        }
                    }

                }
                searcher.close();
        return hits;
    }
View Full Code Here

     */
    private Hits search(String query_string, String publication_id, String sortField, boolean sortReverse) {
        Hits hits =null;
       
        try {
            Searcher searcher=new IndexSearcher(this.indexDir.getAbsolutePath());
            Analyzer l_analyzer=new StandardAnalyzer();
            QueryParser l_queryParser = new QueryParser(this.field,l_analyzer); // Single field
            //MultiFieldQueryParser l_queryParser = new MultiFieldQueryParser(fields[0], l_analyzer); // Multiple fields
            l_queryParser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
            getLogger().debug(query_string);
            Query l_query = l_queryParser.parse(query_string); // Single field
            //Query l_query = l_queryParser.parse(query_string,fields,l_analyzer); // Multiple fields
            if (sortField != null) {
                Sort sort = new Sort(sortField, sortReverse);
                hits = searcher.search(l_query, sort);
            } else {
                hits = searcher.search(l_query);
            }
            if(hits != null){
                return hits;
            }
        } catch (final IOException e) {
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.