lucene学习备份---创建索引

来源:互联网 发布:金山软件管家免费 编辑:程序博客网 时间:2024/05/29 16:40

首先从创建索引入手,内容概要有那么几点 :

1、如何建立索引入口

2、如何导入索引文件

3、如何存储索引内容


以上三点其实就是都和写的问题,以下分五步开始讲解: 

第一步:创建 
Directory

在新版本中创建 Directory 的创建接口如下:

  内容来自www.itxxz.com

  1. // Directory directory = new RAMDirectory();  
  2.  Directory directory = FSDirectory.open(Paths.get("E:/itxxz/lucene"));

  官网:http://www.itxxz.com 
这两种是索引文件的存储地方,第一个(注释中内容)是存放于内存中,第二个是存放于 
E:/itxxz/lucene 目录下

第二步 : 创建 
IndexWriterConfig

用的分析器是lucene标准分词类 StandardAnalyzer

当然我们也可以更换其它,再后续篇章中会进行讲解,以下代码便是创建IndexWriterConfig


 

  1. Analyzer analyzer = new StandardAnalyzer();  
  2. IndexWriterConfig iwc = new IndexWriterConfig(analyzer); 


第三步 : 创建 Document

如果以我们常用的数据库为参考,这里的document就相当于一张表,而field便相当于表里的字段 (见第四步)

第四步 :添加 Field

Field在lucene的后续版本中进行了细分,比如int、long、string及text类型,根据需要可进行筛选使用


 

  1. document.add(new LongField("modified", f.lastModified(), Field.Store.NO));  
  2. document.add(new TextField("contents"new FileReader(file)));  
  3. document.add(new StringField("path", file.toString(), Field.Store.YES)); 

copyright www.itxxz.com 
第五步:写入索引

也就是 
writer.addDocument(document);  操作

这样一个创建索引的过程就完成了(
代码见文末),我们先看下运行效果,:


1、需要检索的文件



2、运行java类,开始对文件进行检索,并控制台打印文件名



3、查看索引目录下是否生成了索引文件

源码如下:
 

  1. /**   
  2.  * 文件名:IndexWriter.java   
  3.  *    
  4.  * 日期:2015年5月28日   
  5.  *   
  6.  */  
  7.   
  8. package com.itxxz.lucene.chapter1;  
  9.   
  10. import java.io.File;  
  11. import java.io.FileReader;  
  12. import java.io.IOException;  
  13. import java.nio.file.Paths;  
  14.   
  15. import org.apache.lucene.analysis.Analyzer;  
  16. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  17. import org.apache.lucene.document.Document;  
  18. import org.apache.lucene.document.Field;  
  19. import org.apache.lucene.document.LongField;  
  20. import org.apache.lucene.document.StringField;  
  21. import org.apache.lucene.document.TextField;  
  22. import org.apache.lucene.index.IndexWriter;  
  23. import org.apache.lucene.index.IndexWriterConfig;  
  24. import org.apache.lucene.store.Directory;  
  25. import org.apache.lucene.store.FSDirectory;  
  26. import org.apache.lucene.store.RAMDirectory;  
  27.   
  28. /** 
  29.  * @author: IT学习者 
  30.  * @官网 www.itxxz.com 
  31.  * @version: 2015年5月28日 下午8:54:48 
  32.  */  
  33. public class IndexWriterDemo {  
  34.   
  35.     /** 
  36.      * @author: IT学习者 
  37.      * @官网 www.itxxz.com 
  38.      * @version: 2015年5月28日 下午8:54:48 
  39.      */  
  40.     public static void main(String[] args) {  
  41.         createIndex();  
  42.     }  
  43.   
  44.     public static void createIndex() {  
  45.   
  46.         IndexWriter writer = null;  
  47.   
  48.         try {  
  49.             // Directory directory = new RAMDirectory();  
  50.             Directory directory = FSDirectory  
  51.                     .open(Paths.get("E:/itxxz/lucene"));   
  52.             Analyzer analyzer = new StandardAnalyzer();  
  53.             IndexWriterConfig iwc = new IndexWriterConfig(analyzer);  
  54.   
  55.             writer = new IndexWriter(directory, iwc);  
  56.             Document document = null;  
  57.             File f = new File("E:/itxxz/data");  
  58.             for (File file : f.listFiles()) {  
  59.                 System.out.println("filename:" + file.getName());  
  60.                 document = new Document();  
  61.                 document.add(new LongField("modified", f.lastModified(),  
  62.                         Field.Store.NO));  
  63.                 document.add(new TextField("contents"new FileReader(file)));  
  64.                 document.add(new StringField("path", file.toString(),  
  65.                         Field.Store.YES));  
  66.                 writer.addDocument(document);  
  67.   
  68.             }  
  69.         } catch (IOException e) {  
  70.             // TODO Auto-generated catch block  
  71.             e.printStackTrace();  
  72.         } finally {  
  73.             if (writer != null) {  
  74.                 try {  
  75.                     writer.close();  
  76.                 } catch (IOException e) {  
  77.                     // TODO Auto-generated catch block  
  78.                     e.printStackTrace();  
  79.                 }  
  80.             }  
  81.         }  
  82.   
  83.     }  
  84.   
  85. }

官网 

0 0
原创粉丝点击