[英]how to refine the search using apache lucene index
I am searching a keyword using index created by apache lucene , it returns the name of files which contains the given keyword now i want to refine the search again only in the files returned by lucene search . 我正在使用由apache lucene创建的索引搜索关键字,它返回包含给定关键字的文件的名称,我现在只想在lucene搜索返回的文件中再次优化搜索。 How is it possible to refine the search using apache lucene.
如何使用apache lucene优化搜索。 I am using the following code.
我使用以下代码。
try
{
File indexDir=new File("path upto the index directory");
Directory directory = FSDirectory.open(indexDir);
IndexSearcher searcher = new IndexSearcher(directory, true);
QueryParser parser = new QueryParser(Version.LUCENE_36, "contents", new SimpleAnalyzer(Version.LUCENE_36));
Query query = parser.parse(qu);
query.setBoost((float) 1.5);
TopDocs topDocs = searcher.search(query, maxhits);
ScoreDoc[] hits = topDocs.scoreDocs;
len = hits.length;
int docId = 0;
Document d;
for ( i = 0; i<len; i++) {
docId = hits[i].doc;
d = searcher.doc(docId);
filename= d.get(("filename"));
}
}
catch(Exception ex){ex.printStackTrace();}
I have added documents in the lucene index using as contents and filename. 我使用as contents和filename在lucene索引中添加了文档。
You want to use a BooleanQuery for something like this. 你想对这样的东西使用BooleanQuery 。 That will let you AND the original search constraints with the refined search constraints.
这将使您和原始搜索约束与精炼的搜索约束。
Example: 例:
BooleanQuery query = new BooleanQuery();
Query origSearch = getOrigSearch(searchString);
Query refinement = makeRefinement();
query.add(origSearch, Occur.MUST);
query.add(refinement, Occur.MUST);
TopDocs topDocs = searcher.search(query, maxHits);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.