首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

lucene入门范例二(检索)

2012-11-19 
lucene入门实例二(检索)copy一个lucene in action的例子?package com.s.luceneimport java.io.Fileimpor

lucene入门实例二(检索)

copy一个lucene in action的例子

?

package com.s.lucene;import java.io.File;import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class IndexReader {public static void main(String[] args) throws IOException, ParseException {String indexDir = "F:\\workspace\\JavaDemo\\src\\com\\s\\lucene\\index";//存储lucene索引的文件夹String queryKey = "最优秀";//查询词//查询search(indexDir,queryKey);}public static void search(String indexDir,String queryKey) throws IOException, ParseException{Directory dir = FSDirectory.open(new File(indexDir));//创建索引文件夹IndexSearcher is = new IndexSearcher(dir);//查询QueryParser parser = new QueryParser(Version.LUCENE_30,"contents",new StandardAnalyzer(Version.LUCENE_30));Query query = parser.parse(queryKey);//封装查询为querylong start = System.currentTimeMillis();TopDocs hits = is.search(query, 10);//查询long end = System.currentTimeMillis();System.out.println(end-start);for (ScoreDoc sd : hits.scoreDocs) {//遍历查询结果Document doc = is.doc(sd.doc);System.out.println(doc.get("fullpath"));System.out.println(doc.get("body"));}is.close();}}
?

热点排行