`
TRAMP_ZZY
  • 浏览: 133622 次
社区版块
存档分类
最新评论

Lucene 建立索引

阅读更多
/** 
 * Project Name:docsearch 
 * File Name:Index.java 
 * Package Name:cn.tramp.docsearch.index 
 * Date:2014年2月27日 下午5:10:15 
 * Copyright (c) 2014, zhangzhaoyu0524@163.com All Rights Reserved. 
 * 
*/  
  
package cn.tramp.docsearch.index;  

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.DateTools.Resolution;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import cn.tramp.docsearch.dao.IDocumentMapper;
import cn.tramp.docsearch.domain.DocumentInfo;
import cn.tramp.docsearch.util.IndexPropertyUtil;
import cn.tramp.docsearch.util.PdfUtil;

/** 
 * ClassName:Index <br/> 
 * Function: Index. <br/> 
 * Reason:   Index. <br/> 
 * Date:     2014年2月27日 下午5:10:15 <br/> 
 * @author   zhangzhaoyu 
 * @version   
 * @since    JDK 1.7
 * @see       
 */
public class Index {
	private final static Log logger  = LogFactory.getLog(Index.class);
	private Directory directory;
	private String indexPath;
	private String docmentPath;
	
	private IDocumentMapper mapper;
	
	/**
	 * 
	 * 构造的时候初始化 indexPath 和 directory
	 *
	 */
	public Index() {
		try {
			indexPath = IndexPropertyUtil.getKeyValueByName("indexPath");
			docmentPath = IndexPropertyUtil.getKeyValueByName("docmentPath");
			directory = FSDirectory.open(new File(indexPath));
		} catch (IOException e) {
			e.printStackTrace();
			logger.info("init Index class error");
		}
	}
	
	private IndexWriter getIndexWriter() throws IOException {
		// 标准分词器 一元分词
		//Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
		Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_43);
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43, analyzer);
		// 设置缓冲区大小
		iwc.setRAMBufferSizeMB(3);
		return new IndexWriter(directory, iwc);
	}
	
	private IndexWriter getIndexWriter(Analyzer analyzer) throws IOException {
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43, analyzer);
		// 设置缓冲区大小
		iwc.setRAMBufferSizeMB(3);
		return new IndexWriter(directory, iwc);
	}

	public void index(List<DocumentInfo> docList, boolean flag) {
		IndexWriter writer = null;
		try {
			writer = getIndexWriter();
			if (flag) {
				writer.deleteAll();
				logger.info("delete all");
				writer.commit();
			}
			for (DocumentInfo docInfo : docList) {
				Document doc = new Document();
				
				doc.add(new StringField("doc_id", docInfo.getDoc_id().toString(), Store.YES));
				doc.add(new TextField("doc_name", docInfo.getDoc_name(), Store.YES));
				doc.add(new StringField("doc_type", docInfo.getDocType().getType_name(), Store.YES));
				
				String docContent = "";
				if (docInfo.getDocType().getType_name().equals(".pdf")) {
					//doc.add(new TextField("content", docInfo.getDoc_name(), Store.YES));
					docContent = PdfUtil.getPdfContent(docmentPath + docInfo.getDoc_location());
				} else {
					docContent = this.getFileContent(docInfo.getDoc_location());
					//doc.add(new TextField("content", docContent, Store.YES));
				}
				//String docContent = PdfUtil.getPdfContent(docmentPath + docInfo.getDoc_location());
				doc.add(new StringField("content", docContent, Store.YES));
				doc.add(new StringField("doc_location", docInfo.getDoc_location(), Store.YES));
				doc.add(new StringField("add_datetime", DateTools.dateToString(docInfo.getAdd_datetime(), Resolution.DAY), Store.YES)); 
				doc.add(new StringField("modify_datetime", DateTools.dateToString(docInfo.getModify_datetime(), Resolution.DAY), Store.YES)); 
				doc.add(new StringField("upload_author", docInfo.getUpload_author(), Store.YES));
				doc.add(new StringField("author", docInfo.getAuthor(), Store.YES));
				writer.addDocument(doc);
			}
			
			writer.forceMerge(1);
			writer.commit();
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 
	 * close:<br />
	 * 关闭Derectory
	 *
	 * @author zhangzhaoyu
	 */
	public void close() {
		try {
			this.directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	private String getFileContent(String filePath) throws IOException {
		String fileAbPath = this.docmentPath + filePath;
		return FileUtils.readFileToString(new File(fileAbPath), "utf-8");
	}
	
	public IDocumentMapper getMapper() {
		return mapper;
	}

	public void setMapper(IDocumentMapper mapper) {
		this.mapper = mapper;
	}
	
	
}
 
分享到:
评论

相关推荐

    Lucene建立索引jar包和Paoding分词jar包

    Lucene建立索引jar包和Paoding庖丁分词jar包,Lucene结合Paoding庖丁分词创建索引索引jar包汇总

    Lucene建立索引

    使用lucene,建索引。倒排索引现在在搜索引擎涌出很大,本工程为入门提供参考

    lucene全文检索简单索引和搜索实例

    基于lucene 2.4简单的一个索引和搜索实例

    Lucene建立索引及查询包含“java”关键字 示例代码

    根据博客调试的程序,比较简单,英文分词和检索,希望对大家学习有帮助。

    lucene 对 xml建立索引

    lucene 对 xml建立索引 建立索引就是怎么简单 呵呵

    Lucene in Action 中文版

     《Lucene实战 第2版 》基于Apache的Lucene 3 0 从Lucene核心 Lucene应用 案例分析3个方面详细系统地介绍了Lucene 包括认识Lucene 建立索引 为应用程序添加搜索功能 高级搜索技术 扩展搜索 使用Tika提取文本 Lucene...

    lucene索引结构原理.docx

    结构化数据和非结构化数据。 结构化数据:指具有固定格式或有限长度的数据,如数据库,元数据等。 非结构化数据:指不定长或无固定格式的数据,如邮件,word文档等

    Lucene索引和查询

    该程序代码属于本人2015所写,虽然尚有不足,却实现了对多个文件夹下的数据进行Lucene建立索引和查询功能,并包含了所需的所有jar包,工程直接导入即可运行。

    关于lucene建立数据库索引的更新说明

    lucene建立数据库索引的问题,在这里,把其中的一些问题补充说明,希望对大家有新的帮助,希望大家相互提出问题,相互学习,共同进步!!

    最简单的Lucene建立、搜索索引的方法

    最简单的Lucene建立、搜索索引的方法,工程基于Myeclipse10

    Lucene索引建立和搜索

    主要将如何使用Lucene建立索引以及搜索进行了代码的实现,有利于初学者熟悉Lucene的基本功能。

    Lucene结合Sql建立索引Demo源码.rar

    本源码演示了Lucene结合Sql建立索引,把Sql中的数据通过建立索引用Lucene来检索 【该源码由51aspx提供】   源码 " onerror="this.src='/images/ifnoimg.gif'" src="/uploads/allimg/090904/1039152O5-0.jpg...

    毕设 Lucene解析索引PDF文档的内容

    iTextPDFExtractor.java ------ ...--PDFBox创建PDF文件的Lucene索引 PDFBoxPathIndex.java ------- --PDFBox创建指定目录PDF文档索引 POIOfficeExtractor.java ----- -- POI处理Excel和Word文档代码

    lucene1.0.doc

    3. lucene建立索引的方式 3 4. lucence分词的原理与种类 3 4.1、基于字符串匹配的分词方法 3 4.2、基于理解的分词方法 4 4.3、基于统计的分词方法 4 4.4几种具体的分词方式: 4 5. lucene可检索的内容 4 6. lucence与...

    Lucene结合Sql建立索引

    Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个...本源码演示了Lucene结合Sql建立索引,把Sql中的数据通过建立索引用Lucene来检索 支持简单的中文分词,同时提供了Lucene.Net-2.0-004版本的源码给大家

    Lucene搜索引擎开发权威经典 光盘

    介绍了Lucene建立索引的过程,索引的查看和删除,索引的同步,索引的合并和优化等内容。第4部分:搜索的高级知识。介绍使用不同的Query对象构建搜索请求,使用QueryParser解析用户的搜索请求,搜索结果的过滤和排序...

    Lucene.net建立索引,检索分页Demo

    利用Lucene.net做的建立索引,检索分页

    Lucene结合Sql建立索引Demo源码

    Lucene结合Sql建立索引Demo源码 Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的...

    用lucene对数据库建立索引及搜索

    用lucene对数据库建立索引及搜索.doc

    lucene4.6建立索引

    lucene

Global site tag (gtag.js) - Google Analytics