lucene学习备份---创建索引
来源:互联网 发布:金山软件管家免费 编辑:程序博客网 时间:2024/05/29 16:40
首先从创建索引入手,内容概要有那么几点 :
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();
- }
- }
- }
- }
- }
官网
0 0
- lucene学习备份---创建索引
- lucene学习----创建索引
- lucene学习之创建索引
- 新一篇: lucene学习----创建索引 |
- Lucene学习-创建索引(一)
- Lucene学习-创建索引(二)
- lucene学习之创建索引的方式
- lucene学习--创建索引与搜索
- lucene学习笔记1--索引创建
- lucene学习笔记1-创建索引
- Lucene学习-创建索引、关键词查询
- Lucene学习教程——Lucene索引创建
- lucene创建索引
- Java_ABC_3.Lucene创建索引
- Lucene 索引创建
- LUCENE创建索引【鸡蛋】
- Lucene 索引创建
- lucene--创建索引,搜索
- linux 技巧:使用 screen 管理你的远程会话
- JAVA目录:
- MDK 5.14软件仿真时Logic Analyzer添加信号失败解决方法
- ubuntu下安装LAMP+wordpress
- 显示当前时间.html
- lucene学习备份---创建索引
- 网页版计算器
- 归并排序算法
- Android学习系列(二): 短信发送器
- Android工程缺少SDK解决方案
- IP地址DHCP与STATIC切换批处理脚本
- Java学习笔记(三)循环
- Java 多线程应用小结(一)
- Android 网络通信——HttpClient