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

Lucene检索目录

2013-11-03 
Lucene检索索引package comimport java.io.Fileimport java.io.IOExceptionimport java.util.HashMapi

Lucene检索索引

package com;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.wltea.analyzer.lucene.IKAnalyzer;import org.wltea.analyzer.lucene.IKQueryParser;import org.wltea.analyzer.lucene.IKSimilarity;public class Search {public static void main(String[] args) throws Exception {String fieldName = "discript";String keyword = "人民";/*Query IkQuery = searchIKQuery(fieldName, keyword);executeQuery(fieldName, keyword,IkQuery);*/BooleanQuery booleanQuery = searchBooleanQuery(keyword);executeQuery(fieldName, booleanQuery);}public static BooleanQuery searchBooleanQuery( String keyword) throws Exception {BooleanQuery bq = new BooleanQuery();Analyzer analyzer = new IKAnalyzer();String[] fn = {"name"};// 增加权重Map<String, Float> filedmap = new HashMap();filedmap.put("name", 50f);MultiFieldQueryParser multField = new MultiFieldQueryParser(Version.LUCENE_30, fn, analyzer, filedmap);Query muquery = multField.parse(keyword);Query ikquery = IKQueryParser.parseMultiField(fn,keyword);Term term1 = new Term("discript",keyword);TermQuery tq = new TermQuery(term1);  bq.add(tq,BooleanClause.Occur.MUST);bq.add(muquery, BooleanClause.Occur.SHOULD);//bq.add(ikquery, BooleanClause.Occur.MUST);        System.out.println(bq);return bq;}    public static Query searchIKQuery(String fieldName, String keyword) throws IOException{// 使用IKQueryParser查询分析器构造Query对象Query query = IKQueryParser.parse(fieldName, keyword);return query;}public static IndexSearcher createSeacher() throws Exception {IndexSearcher isearcher = null;Directory directory = null;File file = new File("C:/commFile/lucence");directory = FSDirectory.open(file);// 实例化搜索器isearcher = new IndexSearcher(directory);// 在索引器中使用IKSimilarity相似度评估器isearcher.setSimilarity(new IKSimilarity());if (directory != null) {directory.close();}return isearcher;}public static void executeQuery(String fieldName,Query query)throws Exception {// 实例化IKAnalyzer分词器Analyzer analyzer = new IKAnalyzer();IndexSearcher isearcher = createSeacher();try {Sort sort = new Sort();// 按字段的排序(一个或多个)SortField[] f = { new SortField("name", SortField.STRING, false) };sort.setSort(f);// 搜索相似度最高的10条记录//TopDocs topDocs = isearcher.search(query, null, 10, sort);TopDocs topDocs = isearcher.search(query, 10);// 关键字高亮显示Formatter formatter = new SimpleHTMLFormatter("<font color="red">", "</font>"); // 前缀和后缀Scorer scorer = new QueryScorer(query);Highlighter highlighter = new Highlighter(formatter, scorer);highlighter.setTextFragmenter(new SimpleFragmenter(200)); // 字长度System.out.println("命中文章的篇数:" + topDocs.totalHits);// 输出结果ScoreDoc[] scoreDocs = topDocs.scoreDocs;for (int i = 0; i < topDocs.totalHits; i++) {int docId =scoreDocs[i].doc;Document targetDoc = isearcher.doc(docId);   System.out.println("内容:" + targetDoc.get(fieldName)+"===="+targetDoc.get("name"));//输出相应字段的内容String hupName = highlighter.getBestFragment(analyzer,fieldName,targetDoc.get(fieldName));//System.out.println(hupName);}} catch (Exception e) {e.printStackTrace();} finally {if (analyzer != null) {analyzer.close();}if (isearcher != null) {isearcher.close();}}}}

热点排行