首先从创建索引入手,内容概要有那么几点 :
1、如何建立索引入口
2、如何导入索引文件
3、如何存储索引内容
以上三点其实就是都和写的问题,以下分五步开始讲解:
第一步:创建 Directory
在新版本中创建 Directory 的创建接口如下:
内容来自www.itxxz.com
- // Directory directory = new RAMDirectory();
- Directory directory = FSDirectory.open(Paths.get("E:/itxxz/lucene"));
官网:http://www.itxxz.com
这两种是索引文件的存储地方,第一个(注释中内容)是存放于内存中,第二个是存放于 E:/itxxz/lucene 目录下
第二步 : 创建 IndexWriterConfig
用的分析器是lucene标准分词类 StandardAnalyzer
当然我们也可以更换其它,再后续篇章中会进行讲解,以下代码便是创建IndexWriterConfig
- Analyzer analyzer = new StandardAnalyzer();
- IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
第三步 : 创建 Document
如果以我们常用的数据库为参考,这里的document就相当于一张表,而field便相当于表里的字段 (见第四步)
第四步 :添加 Field
Field在lucene的后续版本中进行了细分,比如int、long、string及text类型,根据需要可进行筛选使用
- document.add(new LongField("modified", f.lastModified(), Field.Store.NO));
- document.add(new TextField("contents", new FileReader(file)));
- document.add(new StringField("path", file.toString(), Field.Store.YES));
copyright www.itxxz.com
第五步:写入索引
也就是 writer.addDocument(document); 操作
这样一个创建索引的过程就完成了(代码见文末),我们先看下运行效果,:
1、需要检索的文件
2、运行java类,开始对文件进行检索,并控制台打印文件名
3、查看索引目录下是否生成了索引文件
源码如下:
- /**
- * 文件名:IndexWriter.java
- *
- * 日期:2015年5月28日
- *
- */
- package com.itxxz.lucene.chapter1;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.nio.file.Paths;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- 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.store.RAMDirectory;
- /**
- * @author: IT学习者
- * @官网 www.itxxz.com
- * @version: 2015年5月28日 下午8:54:48
- */
- public class IndexWriterDemo {
- /**
- * @author: IT学习者
- * @官网 www.itxxz.com
- * @version: 2015年5月28日 下午8:54:48
- */
- public static void main(String[] args) {
- createIndex();
- }
- public static void createIndex() {
- IndexWriter writer = null;
- try {
- // Directory directory = new RAMDirectory();
- Directory directory = FSDirectory
- .open(Paths.get("E:/itxxz/lucene"));
- Analyzer analyzer = new StandardAnalyzer();
- IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
- writer = new IndexWriter(directory, iwc);
- Document document = null;
- File f = new File("E:/itxxz/data");
- for (File file : f.listFiles()) {
- System.out.println("filename:" + file.getName());
- document = new Document();
- document.add(new LongField("modified", f.lastModified(),
- Field.Store.NO));
- document.add(new TextField("contents", new FileReader(file)));
- document.add(new StringField("path", file.toString(),
- Field.Store.YES));
- writer.addDocument(document);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- if (writer != null) {
- try {
- writer.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
官网