QueryParser short无法解除引用

时间:2018-05-21 14:52:23

标签: java apache lucene

我有一些索引文档,我想使用查询搜索它们,我检查了lucene文档并制作了这段代码但不知何故我得到了"短片不能被解除引用"在QueryParser行中,我是Java和Lucene的新手,我正在使用Lucene 5.3.1

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.time.Clock.system;
import javax.management.Query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.KeywordAnalyzer;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import static sun.rmi.transport.TransportConstants.Version;
import static sun.rmi.transport.TransportConstants.Version;




public class Searcher
{
    public static void main(String args[]) throws IOException{
        String  query="computer science";
        Analyzer analyzer = new KeywordAnalyzer();
         Query q = new QueryParser(Version.LUCENE_CURRENT, "W", analyzer).parse(query); //ERROR IS HERE

         Path indexPath = Paths.get("MonIndex");
        Directory directory = FSDirectory.open(indexPath);
        DirectoryReader reader = DirectoryReader.open(directory);
        IndexSearcher iSearcher = new IndexSearcher(reader);

        TopDocs topdocs = iSearcher.search(q2, 100);
        ScoreDoc[] resultsList = topdocs.scoreDocs;
         for(int i = 0; i<resultsList.length; i++){
             Document book = iSearcher.doc(resultsList[i].doc); 
             System.out.println(book.getField("I").stringValue());
         }

    }

}

1 个答案:

答案 0 :(得分:0)

问题是Version.LUCENE_CURRENT。您没有导入Lucene Version,但您确实拥有sun.rmi.transport.TransportConstants.Version,虽然我不熟悉该库,但肯定是does appear to be a short。因此,尝试通过尝试引用不存在的sun.rmi.transport.TransportConstants.Version.LUCENE_CURRENT来尝试取消引用该错误会导致抛出该错误。

但是,在Lucene的版本中,你说你正在使用,the QueryParserctor甚至不再接受Version参数,所以只需删除它:

Query q = new QueryParser("W", analyzer).parse(query);

您的下一个错误:queryparser返回的Query不是javax.management.Query