1、学习Lucene3.5之创建索引
2018-02-06 20:34
435 查看
创建索引:
/** * 建立索引 */ public void index(){ IndexWriter indexWriter = null; try { //1、创建Directory(索引文件的物理位置,放在内存中还是硬盘上) // Directory directory = new RAMDirectory();//第一种方式:创建索引到内存中 //FSDirectory.open():根据当前环境,选择最好的打开方式 Directory directory = FSDirectory.open(new File("e:/lucene/index01"));//第二种方式:创建索引到硬盘中 //2、创建IndexWriter(写索引工具) /** * IndexWriterConfig(Version matchVersion, Analyzer analyzer)方法解析: * 参数一:适配的版本 * 参数二:分词器(StandardAnalyzer:标准分词器) */ IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)); indexWriter = new IndexWriter(directory, conf); //3、创建Document对象 (写索引) Document document = null; //4、为Document添加Field(是Document子元素) //为文档创建索引 File file = new File("e:/lucene");//找到文档的位置,创建File对象 File[] files = file.listFiles(); if (files!=null && files.length>0){ for (File tempFile : files){ document = new Document(); /** * 为什么要加这一步判断? * 原因: * 因为“FSDirectory.open(new File("e:/lucene/index01"))”这一步的时候会直接在“e:/lucene”创建 * "index01"文件夹,在我们遍历“lucene”文件夹的时候,就会遍历到这个文件夹,而我们的目的是为文档建立 * 索引,而不包括"index01"文件夹,并且遍历到目录的时候也会报错。 */ if (tempFile.isFile()){ /** * Field(String name, Reader reader)方法解析: * 参数一:key * 参数二:输入流 */ document.add(new Field("content", new FileReader(tempFile)));//将文档内容添加到索引域中 /** * Field(String name, String value, Field.Store store, Field.Index index)方法解析: * 参数一:key * 参数二:value * 参数三:是否存储到硬盘(存储域选项) * 1.Field.Store.YES:表示把这个域中的内容完全存储到文件中,方便进行文本的还原 * 2.Field.Store.NO:表示把这个域中的内容不存储到文件中,但是可以被索引。此时内容无法进行完全 * 还原(无法通过document.get()获取) * 一般来说,我们是对id进行索引,然后通过id进数据库中进行搜索,找到对应的url,来获取文章内容, * 所以一般对id索引并存储,文章的内容进行索引,但不存储 * 参数四:索引类型(索引域选项) * 1.Field.Index.ANALYZED:进行分词和索引,适用于标题、内容等 * 2.Field.Index.NOT_ANALYZED:进行索引,但不进行分词,如果身份证号、姓名、ID等,适用于精确搜索 * 3.Field.Index.ANALYZED_NOT_NORMS:进行分词但是不存储norms信息,这个norms中包括了创建索引的时间和权值等信息 * 4.Field.Index.NOT_ANALYZED_NOT_NORMS:既不进行分词也不存储norms信息 * 5.Field.Index.NO:不进行索引 * * 存储域和索引域最佳实践: * 索引域 存储域 案例 * NOT_ANALYZED_NOT_NORMS YES 标识符(主键、文件名),电话号码,身份证号,姓名,日期 * ANALYZED YES 文档标题和摘要 * ANALYZED NO 文档正文 * NO YES 文档类型,数据库主键(不进行分词) * NOT_ANAL 4000 YZED NO 隐藏关键字 */ document.add(new Field("fileName", tempFile.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));//将文件名添加到索引域中 //将文件路径添加到索引域中 document.add(new Field("filePath", tempFile.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED)); //5、通过IndexWriter添加Document到索引中 indexWriter.addDocument(document); } } } } catch (IOException e) { e.printStackTrace(); }finally { if (indexWriter != null){ try { indexWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
相关文章推荐
- lucene3.5学习笔记02--创建索引和建立搜索
- 3、学习Lucene3.5之索引创建--域选项
- 21、学习Lucene3.5索引之近实时搜索常用工具类
- Lucene学习-创建索引(二)
- 6、学习Lucene3.5之对日期和数字进行索引
- lucene学习笔记1-创建索引
- Lucene3.5 的索引的创建,删除,更新,加权
- 《Lucene in Action》(第二版) 第一章节的学习总结 ---- 用最少的代码创建索引和搜索
- 8、学习Lucene3.5索引搜索之TermRange
- 一步一步跟我学习lucene(1)---lucene的IndexWriter对象创建和索引策略的选择
- Lucene.net(4.8.0) 学习问题记录三: 索引的创建 IndexWriter 和索引速度的优化
- Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询
- 14、学习Lucene3.5索引之同义词分词器设计思路
- 16、学习Lucene3.5索引之自定义排序
- 17、学习Lucene3.5索引之使用Filter(过滤器)搜索
- Lucene学习-创建索引(一)
- lucene学习笔记1--索引创建
- 9、学习Lucene3.5索引搜索之其他搜索
- 12、学习Lucene3.5索引之分词原理解析
- Lucene学习笔记(1)-索引创建和简单的查询