java希望将数据库中的数据存储在lucene索引文件中,并检索表信息和数据
我是lucene的新手。我想从数据库中存储和检索数据。我还想存储表信息,这样我就可以知道数据属于哪个表,并为用户加载表
我能够从数据库中以文本形式存储数据,但我无法找到任何方法来让我知道数据来自哪个表
我已经达到了这个水平,可以将数据存储到索引文件中:
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Events event=Event.getEventById(5);
Version matchVersion= Version.LUCENE_30;
Analyzer analyzer=new StandardAnalyzer(matchVersion);
boolean recreateIndexIfExists = true;
File indexDir=new File(INDEX_DIRECTORY);
Directory fsDir = FSDirectory.open(indexDir);
IndexWriter indexWriter= new IndexWriter(fsDir,analyzer,MaxFieldLength.UNLIMITED);
if(event !=null){
Document doc=new Document();
doc.add(new Field("field", event.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("field", event.getPlace(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("field", event.getDescription(), Field.Store.YES, Field.Index.ANALYZED));
indexWriter.addDocument(doc);
}
indexWriter.optimize();
indexWriter.close();
searchIndex("first");
}
这是从索引文件中搜索:
File indexDir=new File(INDEX_DIRECTORY);
Directory fsDir = FSDirectory.open(indexDir);
if(indexDir.exists()){
System.out.println("index file found");
}else{
System.out.println("not found");
}
IndexReader indexReader=IndexReader.open(fsDir);
IndexSearcher searcher=new IndexSearcher(indexReader);
Version matchVersion= Version.LUCENE_30;
Analyzer analyzer=new StandardAnalyzer(matchVersion);
QueryParser parser=new QueryParser(matchVersion, "field", analyzer);
Query query;
try {
query = parser.parse(searchString);
int n=10;
TopDocs topDocs= searcher.search(query,n);
ScoreDoc[] scoreDocs=topDocs.scoreDocs;
System.out.println("hits= "+scoreDocs.length);
for (int i = 0; i < scoreDocs.length; ++i) {
ScoreDoc sd = scoreDocs[i];
System.out.println("scoredocs: "+scoreDocs[i]);
float score = sd.score;
int docId = sd.doc;
Document d = searcher.doc(docId);
Field value = (Field) d.getField("place");
System.out.println("placesdfgddd: "+value);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
# 1 楼答案
你的文档结构有点奇怪。我通常希望标题、地点和描述存储为单独的字段,而不是全部存储在名为“field”的同一字段中。当然,这取决于你和你的搜索需求
要存储表名,只需将其放入存储字段。因为听起来你不需要在上面搜索,所以你可以将它设置为^{:
一旦你有了搜索结果,你就可以简单地检索: