我想创建一个索引,将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)
答案 0 :(得分:1)
通常,编译时应该捕获抽象方法调用,如果它是代码中的问题。这是一个运行时错误,通常是由于导入不兼容的jar版本。您使用的Lucene jar版本是什么版本?
另请参阅此问题:Abstract Method Error
另外,关于Lucene版本的另一个注释,您似乎使用的是4.0或更高版本,在这种情况下,不推荐使用Field
Field.Index
构造函数。您应该使用其中一个Field子类,例如TextField
或StringField
。
答案 1 :(得分:-1)
以下是如何使用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);
}
}
请注意,您必须使用TextField。
以下是搜索方式:
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);
}
}
希望它有所帮助.....