您的位置:首页 > 其它

lucene索引库优化二

2014-02-10 12:21 429 查看
其实提高索引查询的速度最简洁的方法技术将索引放到内存当中,减少IO,从而提高查询速度:

public class MergePolicies {
public static void main(String[] args) throws Exception {
long start=new Date().getTime();
IOContext context=new IOContext();
Directory dir=FSDirectory.open(new File("E:/docData/indexDir"));
/**
* 把索引存储到内存中
*/
Directory directory=new RAMDirectory(dir,context);

IndexReader reader=DirectoryReader.open(directory);

IndexSearcher searcher=new IndexSearcher(reader);
/**
* 多条件查询
*/
String[] fields={"content"};
QueryParser parser=new MultiFieldQueryParser(Version.LUCENE_44, fields,new StandardAnalyzer(Version.LUCENE_44));
Query query = parser.parse("源码");
TopScoreDocCollector results=TopScoreDocCollector.create(10, false);
searcher.search(query, results);
ScoreDoc[] scoreDocs = results.topDocs().scoreDocs;

System.out.println(scoreDocs.length);
/**
* 可以在此分页
* start  起始位置
* length 记录数
*/
for(int i=0;i<scoreDocs.length;i++){
Document doc= searcher.doc(scoreDocs[i].doc);
System.out.println(doc.getField("filename")+"  "+scoreDocs[i].toString());
}
long end=new Date().getTime();
System.out.println("took time:"+(end-start));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: