Example usage for org.apache.lucene.search IndexSearcher IndexSearcher

List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher IndexSearcher.

Prototype

public IndexSearcher(IndexReaderContext context) 

Source Link

Document

Creates a searcher searching the provided top-level IndexReaderContext .

Usage

From source file:com.knowledgebooks.rdf.implementation.LuceneRdfManager.java

License:Open Source License

/**
 * @param search_query//  w  w  w  . ja v  a  2s  .  co m
 * @return
 * @throws org.apache.lucene.queryParser.ParseException
 *
 * @throws java.io.IOException
 */
public List<List<String>> searchIndex(String search_query) throws ParseException, IOException {
    File index_dir = new File(data_store_file_root + "/lucene_index");
    reader = IndexReader.open(FSDirectory.open(index_dir), true);
    List<List<String>> ret = new ArrayList<List<String>>();
    Searcher searcher = new IndexSearcher(reader);

    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
    QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "object", analyzer);
    Query query = parser.parse(search_query);

    TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
    searcher.search(query, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;

    for (int i = 0; i < hits.length; i += 1) {
        Document doc = searcher.doc(hits[i].doc);
        List<String> as2 = new ArrayList<String>(23);
        as2.add(doc.get("subject"));
        as2.add(doc.get("predicate"));
        as2.add(doc.get("object"));
        ret.add(as2);
    }
    reader.close();
    return ret;
}

From source file:com.knowledgetree.indexer.IndexerManager.java

/**
 * Closes any existing readers and reopens them.
 * @throws Exception/*www .  j a  v a2s  .c o m*/
 */
private void reopenIndex() throws Exception {
    this.logger.debug("Reopenning index");
    WriteLock lock = this.locker.writeLock();
    lock.lock();
    try {
        if (null != this.queryReader) {
            this.querySearcher.close();
            this.queryReader.close();
        }
        this.queryReader = IndexReader.open(this.indexDirectory);
        this.querySearcher = new IndexSearcher(this.queryReader);
        this.logger.debug("Timestamp: " + new Date());
        this.logger.debug("Documents in index: " + this.queryReader.numDocs());
    } finally {
        lock.unlock();
    }
}

From source file:com.knowledgetree.indexer.IndexerManager.java

/**
 * Optimise the lucene database.//from  w w  w  . jav a2 s . c o m
 * @throws Exception
 */
public void optimise() throws Exception {
    synchronized (this) {
        this.optimiseCount++;
    }

    this.logger.debug("Optimise index");
    WriteLock lock = this.locker.writeLock();
    lock.lock();
    try {
        if (null != this.queryReader) {
            this.querySearcher.close();
            this.queryReader.close();
        }

        IndexWriter writer = new IndexWriter(this.indexDirectory, this.analyzer, false);
        writer.optimize();
        writer.close();

        this.queryReader = IndexReader.open(this.indexDirectory);
        this.querySearcher = new IndexSearcher(this.queryReader);
    } finally {
        lock.unlock();
    }
}

From source file:com.krawler.luceneSearchService.LuceneSearchImpl.java

License:Open Source License

@Override
public Hits searchIndexWithSort(String query, String[] Field, String indexPath, Sort sort)
        throws KWLuceneException {
    Hits result = null;// w w  w .  ja va 2  s  .c  o  m
    try {
        IndexSearcher searcher = new IndexSearcher(indexPath);
        MultiFieldQueryParser multiparser = new MultiFieldQueryParser(Field, this.KWLAnalyzer);
        multiparser.setAllowLeadingWildcard(true);
        multiparser.setDefaultOperator(QueryParser.Operator.OR);
        Query lucenequery = multiparser.parse(query);
        if (sort == null) {
            result = searcher.search(lucenequery);
        } else {
            result = searcher.search(lucenequery, sort);
        }
    } catch (Exception ex) {
        throw new KWLuceneException("searchIndexWithSort: " + ex.toString(), ex);
    }
    return result;
}

From source file:com.leavesfly.lia.admin.SearcherManager.java

License:Apache License

public SearcherManager(Directory dir) throws IOException { //1
    currentSearcher = new IndexSearcher(IndexReader.open(dir)); //B
    warm(currentSearcher);/*from   ww w  . j  a  v  a2 s .co  m*/
}

From source file:com.leavesfly.lia.admin.SearcherManager.java

License:Apache License

public SearcherManager(IndexWriter writer) throws IOException { //2
    this.writer = writer;
    currentSearcher = new IndexSearcher(writer.getReader()); //C
    warm(currentSearcher);/*from   w  w w  .  ja va 2 s. co  m*/

    writer.setMergedSegmentWarmer( // 3
            new IndexWriter.IndexReaderWarmer() { // 3
                public void warm(IndexReader reader) throws IOException { // 3
                    SearcherManager.this.warm(new IndexSearcher(reader)); // 3
                } // 3
            }); // 3
}

From source file:com.leavesfly.lia.admin.SearcherManager.java

License:Apache License

public void maybeReopen() //E
        throws InterruptedException, //E
        IOException { //E

    startReopen();//from  w  ww .j  a v a  2  s  .c o  m

    try {
        final IndexSearcher searcher = get();
        try {
            IndexReader newReader = currentSearcher.getIndexReader().reopen();
            if (newReader != currentSearcher.getIndexReader()) {
                IndexSearcher newSearcher = new IndexSearcher(newReader);
                if (writer == null) {
                    warm(newSearcher);
                }
                swapSearcher(newSearcher);
            }
        } finally {
            release(searcher);
        }
    } finally {
        doneReopen();
    }
}

From source file:com.leavesfly.lia.advsearching.FilterTest.java

License:Apache License

protected void setUp() throws Exception { // #1
    allBooks = new MatchAllDocsQuery();
    dir = TestUtil.getBookIndexDirectory();
    searcher = new IndexSearcher(dir);
}

From source file:com.leavesfly.lia.advsearching.FunctionQueryTest.java

License:Apache License

public void testRecency() throws Throwable {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexReader r = IndexReader.open(dir);
    IndexSearcher s = new IndexSearcher(r);
    s.setDefaultFieldSortScoring(true, true);

    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents",
            new StandardAnalyzer(Version.LUCENE_30));
    Query q = parser.parse("java in action"); // #A
    Query q2 = new RecencyBoostingQuery(q, // #B
            2.0, 2 * 365, "pubmonthAsDay");
    Sort sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("title2", SortField.STRING) });
    TopDocs hits = s.search(q2, null, 5, sort);

    for (int i = 0; i < hits.scoreDocs.length; i++) {
        Document doc = r.document(hits.scoreDocs[i].doc);
        System.out.println((1 + i) + ": " + doc.get("title") + ": pubmonth=" + doc.get("pubmonth") + " score="
                + hits.scoreDocs[i].score);
    }/*from w  ww  .  j a  v  a2 s .  c  o  m*/
    s.close();
    r.close();
    dir.close();
}

From source file:com.leavesfly.lia.advsearching.MultiPhraseQueryTest.java

License:Apache License

protected void setUp() throws Exception {
    Directory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),
            IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc1 = new Document();
    doc1.add(new Field("field", "the quick brown fox jumped over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc1);//from w w  w .jav  a  2  s  .c o  m
    Document doc2 = new Document();
    doc2.add(new Field("field", "the fast fox hopped over the hound", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc2);
    writer.close();

    searcher = new IndexSearcher(directory);
}