首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Lucene 实战:高速开始 简单查询

2012-10-29 
Lucene 实战:快速开始 简单查询/** ** 查询 ** @throws IOException * @throws ParseException */@Testpub

Lucene 实战:快速开始 简单查询

/** *  * 查询 *  * @throws IOException * @throws ParseException */@Testpublic void search() throws IOException, ParseException{//创建分词器Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);//索引库Directory dir = FSDirectory.open(new File(indexPath));//IndexSearcherIndexSearcher searcher = new IndexSearcher(dir);//查询解析QueryParser parser = new QueryParser(Version.LUCENE_34, "content", analyzer);Query query = parser.parse("WYSIWYG");//查询TopDocs topDocs = searcher.search(query, 100);System.out.println("查询结果:"+topDocs.totalHits+"条记录");//打印结果for(ScoreDoc doc : topDocs.scoreDocs){Document document = searcher.doc(doc.doc);System.out.println(document.get("filepath"));}searcher.close();}
?

需要注意的是:建立索引的分词器要和查询的分词器是同一个,这个才能保证分词一致。

?

热点排行