简体   繁体   中英

AbstractMethodError while creating an index in Lucene

I want to create an index to store the url as a file name and using:

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

However I get the following exception:

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)

Generally, an abstract method call should be caught when compiling, if it's an issue in your code. This coming up an a runtime error is generally due to importing incompatible jar versions. What version are the Lucene jar version you are using?

See also, this question: Abstract Method Error

Also, another note regarding Lucene versions, You appear to be using version 4.0 or later, in which case Field constructors using Field.Index are deprecated. You should use one of the Field subclasses instead, like TextField or StringField , for example.

Here is how to make a simple Index for text searches using 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);
    }

}

Note that you have to TextField.

And here is how to search:

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

}

Hope it helps.....

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