标签:
* */
package ch2.lucenedemo.process;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
public class Search {
//定义索引生成的文件夹
private String INDEX_STORE_PATH = "E:\\Lucene项目\\索引文件夹";
//利用Lucene的搜索
public void indexSearch(String searchType, String searchKey){
try{
System.out.println("++使用索引方式搜索++");
System.out.println("----------------------------------");
//依据索引位置建立IndexSearcher
IndexSearcher searcher = new IndexSearcher("INDEX_STORE_PATH");
//Term是搜索的最小单元,类似于一个汉字或者词语
//创建搜索单元,searchType代表要搜索的Field, searchKey代表keyword
Term t = new Term(searchType, searchKey);
//由Term生成一个Query
Query q = new TermQuery(t);
//搜索開始的时间
Date beginTime = new Date();
//获取一个<document, frequency>的枚举对象TermDocs
TermDocs termDocs = searcher.getIndexReader().termDocs(t);
while(termDocs.next()){
//输出在文档中出现的keyword的次数
System.out.println("find " + termDocs.freq() + "matches in ");
//输出搜索到关键词的文档
System.out.println(searcher.getIndexReader().document(termDocs.doc()).getField("fileName").stringValue());
}
//搜索完毕时间
Date endTime = new Date();
//搜索所耗使劲
long timeofSearch = endTime.getTime() - beginTime.getTime();
System.out.println("使用索引方式搜索总耗时 : " + timeofSearch + "ms");
}catch(IOException e){
e.printStackTrace();
}
}
//利用String的搜索
public void stringSearch(String keyword, String searchDir){
System.out.println("++使用字符串匹配方式搜索++");
System.out.println("---------------------------------");
File filesDir = new File(searchDir);
//返回文件夹文件夹全部文件数组
File[] files = filesDir.listFiles();
//HashMap保存文件名称和匹配次数对
Map rs = new LinkedHashMap();
//记录搜索開始时间
Date beginTime = new Date();
//遍历全部文件
for(int i = 0; i < files.length; i++){
//初始化匹配次数
int hits = 0;
try{
//读取文件内容
BufferedReader br = new BufferedReader(new FileReader(files[i]));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while(line != null){
sb.append(line);
line = br.readLine();
}
br.close();
//将Stringbuffer转化为String, 以便于搜索
String stringToSearch = sb.toString();
//初始化fromIndex
int fromIndex = -keyword.length();
//逐个匹配keyword
while((fromIndex = stringToSearch.indexOf(keyword, fromIndex + keyword.length())) != -1){
hits++;
}
//将文件名称和匹配次数增加到hashMap
rs.put(files[i].getName(), new Integer(hits));
}catch(IOException e){
e.printStackTrace();
}
}
//输出查询结果
Iterator it = rs.keySet().iterator();
while(it.hasNext()){
String fileName = (String)it.next();
Integer hits = (Integer) rs.get(fileName);
System.out.println("find " + hits.intValue() + "matches in " + fileName);
}
//记录结束时间
Date endTime = new Date();
//得到搜索耗费时间
long timeOfSearch = endTime.getTime() - beginTime.getTime();
System.out.println("使用字符串匹配方式总耗时 " + timeOfSearch + " ms");
}
public static void main(String[] args){
Search search = new Search();
//通过索引速锁keyword
search.indexSearch("content", "保尔");
//插入一个分隔行
System.out.println("=========================");
System.out.println("=========================");
//通过String的API搜索关键词
search.stringSearch("保尔", "INDEX_STORE_PATH");
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
标签:
原文地址:http://www.cnblogs.com/hrhguanli/p/4872287.html