Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AbstractMethodError在Lucene中创建索引时出错_Java_Lucene - Fatal编程技术网

Java AbstractMethodError在Lucene中创建索引时出错

Java AbstractMethodError在Lucene中创建索引时出错,java,lucene,Java,Lucene,我想创建一个索引,将url存储为文件名,并使用: Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer); FSDirectory dir = FSDirectory.open(new File(index)); IndexWriter writer = new IndexWriter(dir, config)

我想创建一个索引,将url存储为文件名,并使用:

Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
FSDirectory dir = FSDirectory.open(new File(index));
IndexWriter writer = new IndexWriter(dir, config);

Document doc = new Document();
doc.add(newField("file_name", rs.getString("file_name"), 
        Field.Store.YES,
        Field.Index.ANALYZED));
writer.addDocument(doc);
但是,我得到以下例外情况:

Exception in thread "main" java.lang.AbstractMethodError:
       org.apache.lucene.analysis.TokenStream.incrementToken()
    at org.apache.lucene.index.DocInverterPerField.processFields(DocInverterPerField.java:133)
    at org.apache.lucene.index.DocFieldProcessorPerThread.processDocument(DocFieldProcessorPerThread.java:248)
    at org.apache.lucene.index.DocumentsWriter.updateDocument(DocumentsWriter.java:851)
    at org.apache.lucene.index.DocumentsWriter.addDocument(DocumentsWriter.java:827)
    at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:2022)
    at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1996)
    at TextIndex1.main(TextIndex1.java:54)

下面是如何使用Lucene 4.6为文本搜索创建简单索引

public void indexFilmTitle() {
    try {
        Directory dir = FSDirectory.open(new File(AppConstants.INDEX_DIR));
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46, analyzer);
        iwc.setOpenMode(OpenMode.CREATE);
        IndexWriter writer = new IndexWriter(dir, iwc);

        String sql = "SELECT * FROM sakila.film_text";
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        int i=0;
        while (rs.next()) {
            i++;
            Document doc = new Document();
            Field id = new IntField("id", rs.getInt(1), Field.Store.YES);               
            doc.add(id);
            Field title = new TextField("title", rs.getString(2), Field.Store.YES);
            doc.add(title);
            writer.addDocument(doc);
        }
        writer.close();
    } catch (IOException ex) {
        Logger.getLogger(IndexManager.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(IndexManager.class.getName()).log(Level.SEVERE, null, ex);
    }

}
请注意,您必须输入文本字段

以下是如何搜索:

public void searchFromFilm_Text(String keyword) {
    try {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(AppConstants.INDEX_DIR)));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
        QueryParser parser = new QueryParser(Version.LUCENE_46, "title", analyzer);            
        Query query = parser.parse(keyword);
        System.out.println("Searching for: " + query.toString("title"));
        TopDocs results = searcher.search(query, 100);             
        ScoreDoc[] hits = results.scoreDocs;
        System.out.println(hits.length);
        for(ScoreDoc sdoc : hits)
        {
            Document doc = searcher.doc(sdoc.doc);
            System.out.println(doc.get("id"));
            System.out.println(doc.get("title"));             
        }
    } catch (IOException ex) {
        Logger.getLogger(SearchManager.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(SearchManager.class.getName()).log(Level.SEVERE, null, ex);
    }

}

希望对您有所帮助….

一般来说,如果代码中存在问题,则在编译时应捕获抽象方法调用。出现运行时错误通常是由于导入了不兼容的jar版本。您使用的Lucene jar版本是什么

另见本问题:


另外,关于Lucene版本的另一个注意事项是,您似乎正在使用版本4.0或更高版本,在这种情况下,不推荐使用Field.Index的字段构造函数。您应该使用字段子类中的一个,例如或。

我想进行文本搜索。但上面的代码只返回您要搜索的确切单词。但我希望只使用文本进行搜索。也许你可以试试这个:编辑一个用于全文搜索的工作原型的答案