最近在研究关于lucene检索文档的问题,参考网上一些人的例子,但是结果只能检索英文的,有人说要通过中文分词,但我也用了,结果是一样的,不能检索中文。呵呵。。。后来经过一些高手的指点,解决了中文的问题。我用的lucene版本为3.0.2,中文分词是IKAnalyzer3.2.下面是我的一些代码。仅供参考。
第一步:
建立文件索引:
public class IndexProcesser {
// 成员变量存储创建的索引文件存放的位置
private static String INDEX_STORE_PATH = "G:\\学习\\Lucene相关\\IndexWriter";
private static String DATA_DIR = "G:\\学习\\Lucene相关\\IndexWriter\\searchFolder";
/**
* 索引dataDir下.txt文件,并储存在indexDir下,返回索引的文件数量
*
* @param indexDir
* @param dataDir
* @return
* @throws Exception
*/
public static int createIndex(File indexDir, File dataDir) throws Exception {
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir
+ " does not exist or is not a directory");
}
Analyzer analyzer = new IKAnalyzer();//IK分词器,网上还有别的分词器。
IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir),
analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
writer.setMergeFactor(1000); //合并因子
writer.setMaxBufferedDocs(1000); //最大缓存文档数
writer.setMaxMergeDocs(Integer.MAX_VALUE); //最大合并文档数
writer.setMaxFieldLength(99999999);//增加内存域长度限制
indexDirectory(writer, dataDir);
int numIndexed = writer.numDocs();
writer.optimize();
writer.close();
return numIndexed;
}
private static void indexDirectory(IndexWriter writer, File dataDir) {
File[] files = dataDir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
indexDirectory(writer, f);
} else {
try {
indexFile(writer, f);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void indexFile(IndexWriter writer, File f)
throws IOException {
if (f.isHidden() || !f.canRead() || !f.exists()) {
return;
}
System.out.println("indexIng>>" + f.getCanonicalPath());
Document doc = new Document();
doc.add(new Field("filePath", f.getAbsolutePath(), Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("content", readFile(f), Field.Store.YES,
Field.Index.ANALYZED));
writer.addDocument(doc);
}
//readFile()方法主要对你所要检索的文档进行解析,会用到一些相应的组件,如pdf有pdfBox组件,pdfBox对中文支持不好。我用的是xpdf,关于xpdf的配置,会在后面进行说明。word用到的是POI组件,还有别的格式,此处不在详说。
private static String readFile(File f) {
StringBuffer content = null;
FileInputStream is = null;
if (f.getName().endsWith(".doc")) {
content=new StringBuffer();
try{
is=new FileInputStream(f);
WordExtractor wordExtractor=new WordExtractor(is);
content=content.append(wordExtractor.getText());
is.close();
}catch(Exception e){
e.printStackTrace();
}
} else if (f.getName().endsWith(".pdf")) {
String PATH_TO_XPDF="C:\\xpdftest\\xpdf\\pdftotext.exe";
String[] cmd=new String[]{ PATH_TO_XPDF, "-enc", "UTF-8", "-q",f.getAbsoluteFile().toString(), "-" };
try {
Process p=Runtime.getRuntime().exec(cmd);
BufferedInputStream bis=new BufferedInputStream(p.getInputStream());
InputStreamReader reader=new InputStreamReader(bis,"UTF-8");
int len=0;
content=new StringBuffer();
while((len=reader.read())!=-1){
content.append((char)len);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}else{
try {
content = new StringBuffer();
is = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(
is, "GBK"));
for (String line = null; (line = br.readLine()) != null;) {
content.append(line).append("\n");
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return content.toString();
}
public static void main(String[] args) {
long start = new Date().getTime();
int numIndexed = 0;
try {
numIndexed = createIndex(new File(INDEX_STORE_PATH), new File(DATA_DIR));
} catch (Exception e) {
e.printStackTrace();
}
long end = new Date().getTime();
System.out.println("Indexing " + numIndexed + " files took "
+ (end - start) + " milliseconds");
}
}
//xpdf配置说明:
1.从http://www.foolabs.com/xpdf/download.html上下载xpdf3.02(xpdf-3.02pl2-win32.zip)和xpdf-chinese-simplified.tar.gz。
2.将xpdf-3.02pl2-win32.zip解压放入c:/xpdf,同时将xpdf-chinese-simplified.tar.gz解压放入到该文件夹内。
3.打开解压后的xpdf-chinese-simplified文件夹下的add-to-xpdfrc文件,将其内容拷贝到
xpdfrc.txt中,
修改如下代码:
#----- begin Chinese Simplified support package (2004-jul-27)
cidToUnicode Adobe-GB1 C:/xpdf/xpdf-chinese-simplified/Adobe-GB1.cidToUnicode
unicodeMap ISO-2022-CN C:/xpdf/xpdf-chinese-simplified/ISO-2022-CN.unicodeMap
unicodeMap EUC-CN C:/xpdf/xpdf-chinese-simplified/EUC-CN.unicodeMap
unicodeMap GBK C:/xpdf/xpdf-chinese-simplified/GBK.unicodeMap
cMapDir Adobe-GB1 C:/xpdf/xpdf-chinese-simplified/CMap
toUnicodeDir C:/xpdf/xpdf-chinese-simplified/CMap
fontDir c:/windows/fonts
displayCIDFontTT Adobe-GB1 c:/windows/fonts/simhei(truetype)
textEOL CR+LF
#----- end Chinese Simplified support package
注意“ C:/xpdf”部分路径,要和你本机的路径一致。
下面就是检索了:
public class Search {
private static String IndexDir="G:\\学习\\Lucene相关\\IndexWriter";
private static String keyWord="努力";
private static int TOP_NUM = 100;
public static void doSearch(File indexDir,String key) throws Exception{
IndexSearcher searcher=new IndexSearcher(FSDirectory.open(indexDir),true);
String field="content";
Query query=IKQueryParser.parse(field, keyWord);
//=========================================================================
long start=new Date().getTime();
TopDocs hits=searcher.search(query, TOP_NUM);
long end = new Date().getTime();//end time
System.out.println("共找到文档数:"+hits.totalHits);
System.out.println("搜索完毕用时:" + (end - start) + "毫秒");
if(hits.totalHits==0){
System.out.println("没有找到您需要的结果!");
}else{
for(int i=0;i<hits.scoreDocs.length;i++){
try{
ScoreDoc scoreDoc = hits.scoreDocs[i];// 有变化的地方
Document doc = searcher.doc(scoreDoc.doc);// 有变化的地方
System.out.print("这是第" + (i+1) + "个检索结果,文件路径为:");
System.out.println(doc.get("filePath"));
}catch(Exception e){
}
}
}
searcher.close();
}
public static void main(String[] args) throws Exception {
File indexDir=new File(IndexDir);
if(!indexDir.isDirectory()||!indexDir.exists()){
throw new Exception(indexDir +
" does not exist or is not a directory。");
}
doSearch(indexDir, keyWord);
}
}
分享到:
相关推荐
lucenetest.rar,lucene,全文检索,lucene例子 lucenetest.rar,lucene,全文检索,lucene例子lucenetest.rar,lucene,全文检索,lucene例子
基于lucene 2.4简单的一个索引和搜索实例
利用paoding进行中文分词,可进行自定义词典。利用lucene对文本内容进行索引和搜索并进行高亮处理。
快速有效地索引企业累积的大量的信息资源,是提供高质量检索服务的基础。Lucene 是一个用Java 写的全文索 引引擎工具包,访问索引时间快,支持多用户...Lucene 的索引文件格式,实现了一个基于Lucene 文档检索的应用实例。
Lucene是apache软件基金会 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎...
资源全免费,我的博客有相关的说明,这个是一个eclipse项目,是一个使用lucene全文检索word2007的例子,可以直接运行,但是需要在数据目录下放入word文件 才能创建索引.
Lucene 是为文本类型的数据建立索引的,所以只要能把需要索引的数据格式转化的文本的,Lucene 就能对文档进行索引和搜索。比如HTML、PDF,都可以转换文本再交给Lucene进行索引。
Lucene3.0和Lucene2.0API有多处改动,该实例用Luence3.0实现
lucene 全文检索数据库,非常不错的一个例子!
Eclipse工程文件,全文检索 lucene-5.2.1 入门Eclipse工程实例,福利放送,与lucene3结果比对
lucene 创建中文索引,运用创建的索引进行全文检索实例
lucene 全文检索 包结构 lucene开发实例
各种分词器都用了,有ikAnalyzer和smartChineseAnalyzer等等分词器。使用jdk7,Lucene-core4.5。非常不错。
该文件提供了lucene实例查询的案例。
程序展现了lucene包强大的建索引和搜索功能!
该例子包括ssh2注解 Lucene 全文检索与WebService,技术是拿来分享的,以后有什么好例子我都会拿出来与大家分享的,真心技术交流 QQ:4218380
Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎...
包含最新lucene5.2.1的入门经典案例,以及程序中所需的lucene-core-5.2.1.jar、lucene-analyzers-common-5.2.1.jar等几个jar包。代码可运行。
Lucene.Net 文件检索doc,xls,ppt,txt,pdf文件(实例),对需要文件检索的朋友很有帮助,10分很值的
lucene全文检索的功能,很强,比如baidu搜索功能,超级完美的例子,包括中文分词绝对有价值