简体   繁体   中英

Java-Can't pass Directory variable as an argument to IndexReader.open() in Apache Lucene 6.4.2

I'm trying to use the open function defined in the Lucene documentation here- https://lucene.apache.org/core/3_5_0/api/core/org/apache/lucene/index/IndexReader.html (Do a Ctrl + F for 'open'). However Netbeans 8.1 with Apache Lucene 6.4.2 gives an in-line error on the code at statement 'reader = IndexReader.open(indexDirectory);'. Here is the error and code.

Cannot find symbol symbol: method open(Directory) location: class IndexReader

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Indexing_Searching 
{

    public static final String FIELD_CONTENTS = "contents";

    public int searchIndex(String instring, String Index_Dir_Path)
    {
    int numDocs =0;
    try 
    {
        Path path = Paths.get(Index_Dir_Path);
        Directory indexDirectory = FSDirectory.open(path);

        IndexReader reader;
        reader = IndexReader.open(indexDirectory);
        Term term = new Term("content", instring);
        numDocs = reader.docFreq(term);
        //System.out.println("Number of documents for given key" + instring +" # docs" + numDocs);
    } 
    catch (CorruptIndexException e) 
    {
    e.printStackTrace();
    } 
    catch (IOException e) 
    {
    e.printStackTrace();
    }
    return(numDocs);
    }// End of one-words searching function
}

根据当前用于Lucene 6.4.2的IndexReader JavaDoc ,您应该使用DirectoryReader.open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM