赞
踩
中文分词程序
package test03; import java.io.IOException; import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryparser.classic.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 com.hankcs.lucene.HanLPAnalyzer; public class Demo2 { public static void main(String[] args) { try { Directory directory=FSDirectory.open(Paths.get("F:/index")); IndexReader reader=DirectoryReader.open(directory); IndexSearcher searcher=new IndexSearcher(reader); String text="我想要苹果手机"; String field="title"; Analyzer analyzer=new HanLPAnalyzer(); QueryParser parser=new QueryParser(field,analyzer); Query query=parser.parse(text); TopDocs docs=searcher.search(query, 100); System.out.println("命中的记录数:"+docs.totalHits); ScoreDoc[] array=docs.scoreDocs; for(ScoreDoc one:array) { Document document=searcher.doc(one.doc); String id=document.get("id"); String title=document.get("title"); System.out.println("商品编号"+id); System.out.println("商品名称"+title); } reader.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。