`
Brooke
  • 浏览: 1180624 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Lucene学习笔记一(基础)

 
阅读更多
在创建索引的时候,
Field.Store,表示对域的存储
       Field.Store.YES:存储字段值(未分词前的字段值)
        Field.Store.NO:不存储,存储与索引没有关系
        Field.Store.COMPRESS:压缩存储,用于长文本或二进制,但性能受损
Field.Index,表示对域的搜索
       Field.Index.ANALYZED:分词建索引
       Field.Index.ANALYZED_NO_NORMS:分词建索引,但是Field的值不像通常那样
                                      被保 存,而是只取一个byte,这样节约存储空间
       Field.Index.NOT_ANALYZED:不分词且索引
       Field.Index.NOT_ANALYZED_NO_NORMS:不分词建索引,Field的值去一个byte保存


TermVector表示文档的条目(由一个Document和Field定位)和它们在当前文档中所出现的次数
      Field.TermVector.YES:为每个文档(Document)存储该字段的TermVector
     Field.TermVector.NO:不存储TermVector
     Field.TermVector.WITH_POSITIONS:存储位置
      Field.TermVector.WITH_OFFSETS:存储偏移量
      Field.TermVector.WITH_POSITIONS_OFFSETS:存储位置和偏移量



创建索引的简单示例:

/**
	 * 创建索引(Indexing),生成索引文件
	 */
	private static void createTextIndex() {
//		public final static String INDEX_SOURCE_DIR = "D:\\tomcat-7.0.22";// 要检索的文件夹
//
//		public final static String INDEX_STORE_DIR = "d:\\fileIndex";// 索引文件的存放位置
		//获取要索引的文件
		File baseDir = new File(INDEX_SOURCE_DIR);
		List<File> subFiles = new ArrayList<File>();
		if (!baseDir.exists()) {
			System.out.println("不存在" + INDEX_SOURCE_DIR + "目录");
			System.exit(1);
		} else if (baseDir.isDirectory()) {
			subFiles = FileUtils.searchFilesByType(baseDir, subFiles, ".html");
		}
		
		//创建存放索引文件的目录
		File indexDir = new File(INDEX_STORE_DIR);
		if (!indexDir.exists()) {
			indexDir.mkdir();
		}

		//索引分析器
		Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
		
		//创建索引器(核心)  
		IndexWriter indexWriter = null;
		long startTime = 0;
		try {
			indexWriter = new IndexWriter(FSDirectory.open(indexDir),luceneAnalyzer, true, IndexWriter.MaxFieldLength.LIMITED);
			indexWriter.setMaxMergeDocs(5);
			indexWriter.setMergeFactor(5);
			//不建立复合式索引文件,默认的情况下是复合式的索引文件   
			indexWriter.setUseCompoundFile(false);
			
			startTime = new Date().getTime();
			// 增加document到索引去
			for (int i = 0; i < subFiles.size(); i++) {
				if(subFiles.get(i).getName().indexOf(".")<=0){
					continue;
				}
				System.out.println(new Date()+"    File " + subFiles.get(i).getCanonicalPath()+ "  正在被索引....");
					
				// 将txt 文件写到 Document中
				Document document = new Document();
				
				String fileName=subFiles.get(i).getName();//.substring(0,subFiles.get(i).getName().indexOf("."));
				Field fieldName = new Field("fileName",fileName,Field.Store.NO,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
				document.add(fieldName);
				
				String filePath=subFiles.get(i).getPath();
				Field FieldPath = new Field("filePath",filePath,Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
				document.add(FieldPath);
				
				String contents = FileUtils.fileReaderAll(subFiles.get(i).getCanonicalPath(),"GBK");// 转化成GBK
				Field FieldBody = new Field("contents", contents, Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
				document.add(FieldBody);
				
				indexWriter.addDocument(document);
			}

		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// optimize()方法是对索引进行优化
				indexWriter.optimize();
				indexWriter.close();
				// 测试一下索引的时间
				long endTime = new Date().getTime();
				System.out.println("这花费了" + (endTime - startTime)+" 毫秒来把文档增加到索引里面去!\n"+indexDir.getPath());
				File[] fils=indexDir.listFiles();
				for(int i=0;i<fils.length;i++){
					System.out.println(fils[i].getCanonicalPath());
				}
			} catch (CorruptIndexException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
分享到:
评论

相关推荐

    lucene学习lucene学习

    lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习...

    本人的Lucene2.9学习笔记

    本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记本人的Lucene2.9学习笔记本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记

    lucene学习笔记

    lucene学习笔记,lucene入门必备材料

    lucene基础学习笔记&源码

    lucene基础学习笔记&源码

    Lucene学习笔记(一)Lucene入门实例

    NULL 博文链接:https://kylinsoong.iteye.com/blog/719415

    Lucene学习笔记.doc

    很好的Lucene学习入门资料。lucene是纯java开发的,支持索引的建立和搜索

    Lucene 3.6 学习笔记

    第一章 LUCENE基础 2 1.1 索引部分的核心类 2 1.2 分词部分的核心类 2 1.3 搜索部分的核心类 2 第二章 索引建立 3 2.1 创建Directory 3 2.2 创建Writer 3 2.3 创建文档并且添加索引 4 2.4 查询索引的基本信息 5 2.5 ...

    【大搜集:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    lucene学习笔记 1 .txt lucene学习笔记 2.txt lucene学习笔记 3 .txt lucene入门实战.txt Lucene 的学习 .txt Lucene-2.0学习文档 .txt Lucene入门与使用 .txt lucene性能.txt 大富翁全文索引和查询的例子...

    lucene使用总结笔记

    lucene使用总结笔记lucene使用总结笔记lucene使用总结笔记lucene使用总结笔记lucene使用总结笔记

    Lucene 学习笔记 1

    NULL 博文链接:https://menglh.iteye.com/blog/347467

    lucene3.5学习笔记

    介绍lucene3.5的相关技术,包括基本用法、分析器、索引建立与查询,扩展的高亮、分页、以及solr3.5的相关用法

    Lucene 课堂笔记

    传智播客lucene课堂笔记,和大家分享下,就是上课的时候记的

    Lucene学习笔记

    这是本人学习lucene时候做的笔记,内容较详细,通俗易懂,是入门Lucene的好帮手!欢迎下载!

    lucene学习

    Lucene的基础知识 1、案例分析:什么是全文检索,如何实现全文检索 2、Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3、配置开发环境 4、创建索引库 5、查询索引库 6、分析器的分析过程 a) 测试分析器的分词...

    lucene笔记.pdf

    lucene笔记

Global site tag (gtag.js) - Google Analytics