我是lucene环境中的新手并使用tutorial。
1- Indexer.java类用于索引原始数据,以便我们可以使用lucene库对其进行搜索。
2- LuceneConstants.java类用于提供在示例应用程序中使用的各种常量。
3- Searcher.java类用于搜索Indexer创建的索引以搜索请求的内容。
4- TextFileFilter.java类用作.txt文件过滤器。
5- LuceneTester.java lass用于测试lucene库的索引和搜索功能..
现在我尝试在Fielder.java中为字段位置(TermVectorPosition)编制索引,并通过LuceneTester.java中的查询检索它。任何人帮助我
答案 0 :(得分:0)
public void doSearch(String querystr) throws IOException, ParseException {
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
Directory index = FSDirectory.open(new File(indexDir));
// 2. query
Query q = new QueryParser(Version.LUCENE_36, LuceneConstants.Term_Vector_Position, analyzer).parse(querystr);
// 3. search
int hitsPerPage = 10;
IndexSearcher searcher = new IndexSearcher(index, true);
IndexReader reader = IndexReader.open(index, true);
searcher.setDefaultFieldSortScoring(true, true);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
// 4. display term positions, and term indexes
System.out.println("Found " + hits.length + " hits.");
// System.out.println("Found " + hits.clone().length+ " hits.");
for(int i=0;i<hits.clone().length;++i) {
int docId = hits[i].doc;
System.out.println("docId:" + docId);
TermFreqVector tfvector = reader.getTermFreqVector(docId, "TVP");
TermPositionVector tpvector = (TermPositionVector)tfvector;
System.out.println("tfvector " + tfvector + " tpvector" + tpvector);
int termidx = tfvector.indexOf(querystr);
System.out.println("termidx " + termidx );
int[] termposx = tpvector.getTermPositions(termidx);
//System.out.println("termposx " + termposx.length);
TermVectorOffsetInfo[] tvoffsetinfo = tpvector.getOffsets(termidx);
for (int j=0;j<termposx.length;j++) {
System.out.println("termpos at j :"+j + ": " +termposx[j]);
}
System.out.println("tvoffsetinfo " + tvoffsetinfo.length);
for (int j=0;j<tvoffsetinfo.length;j++) {
int offsetStart = tvoffsetinfo[j].getStartOffset();
int offsetEnd = tvoffsetinfo[j].getEndOffset();
System.out.println("offsets : "+offsetStart+" "+offsetEnd);
}
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("filepath"));
}
// searcher can only be closed when there
// is no need to access the do***ents any more.
searcher.close();
}