`

lucene中FSDirectory、RAMDirectory的用法

 
阅读更多

package com.ljq.directory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

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.NumberTools;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.Test;

public class DirectoryTest {
// 数据源路径
String dspath = "F:\\android\\luceneprj\\luceneds\\IndexWriter addDocument's a javadoc .txt";
//存放索引文件的位置,即索引库
String indexpath = "F:\\android\\luceneprj\\luceneindex";
//分词器
Analyzer analyzer = new StandardAnalyzer();

/**
* 创建索引,会抛异常,因为没对索引库进行保存
*
* IndexWriter 用来操作(增、删、改)索引库的
*/
@Test
public void createIndex() throws Exception {
//Directory dir=FSDirectory.getDirectory(indexpath);
//内存存储:优点速度快,缺点程序退出数据就没了,所以记得程序退出时保存索引库,已FSDirectory结合使用
//由于此处只暂时保存在内存中,程序退出时没进行索引库保存,因此在搜索时程序会报错
Directory dir=new RAMDirectory();
File file
= new File(dspath);
//Document存放经过组织后的数据源,只有转换为Document对象才可以被索引和搜索到
Document doc = new Document();
//文件名称
doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
//检索到的内容
doc.add(new Field("content", readFileContent(file), Store.YES, Index.ANALYZED));
//文件大小
doc.add(new Field("size", NumberTools.longToString(file.length()),
Store.YES, Index.NOT_ANALYZED));
//检索到的文件位置
doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NOT_ANALYZED));

// 建立索引
//第一种方式
//IndexWriter indexWriter = new IndexWriter(indexpath, analyzer, MaxFieldLength.LIMITED);
//第二种方式
IndexWriter indexWriter = new IndexWriter(dir, analyzer, MaxFieldLength.LIMITED);
indexWriter.addDocument(doc);
indexWriter.close();
}

/**
* 创建索引,不会抛异常
*
* IndexWriter 用来操作(增、删、改)索引库的
*/
@Test
public void createIndex2() throws Exception {
//数据源
File file = new File(dspath);

Directory fsDir
= FSDirectory.getDirectory(indexpath);
// 1,启动时读取
Directory ramDir = new RAMDirectory(fsDir);

// 运行程序时操作ramDir
IndexWriter ramIndexWriter = new IndexWriter(ramDir, analyzer, MaxFieldLength.LIMITED);
// 添加 Document
Document doc = new Document();
//文件名称
doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
//检索到的内容
doc.add(new Field("content", readFileContent(file), Store.YES, Index.ANALYZED));
//文件大小
doc.add(new Field("size", NumberTools.longToString(file.length()), Store.YES, Index.NOT_ANALYZED));
//检索到的文件位置
doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NOT_ANALYZED));
ramIndexWriter.addDocument(doc);
ramIndexWriter.close();

// 2,退出时保存
IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer, true, MaxFieldLength.LIMITED);
fsIndexWriter.addIndexesNoOptimize(
new Directory[]{ramDir});
// 优化
// fsIndexWriter.flush();
// fsIndexWriter.optimize();
fsIndexWriter.close();
}

/**
* 优化操作
*
*
@throws Exception
*/
@Test
public void createIndex3() throws Exception{
Directory fsDir
= FSDirectory.getDirectory(indexpath);
IndexWriter fsIndexWriter
= new IndexWriter(fsDir, analyzer, MaxFieldLength.LIMITED);

fsIndexWriter.optimize();
fsIndexWriter.close();
}

/**
* 搜索
*
* IndexSearcher 用来在索引库中进行查询
*/
@Test
public void search() throws Exception {
//请求字段
//String queryString = "document";
String queryString = "adddocument";

// 1,把要搜索的文本解析为 Query
String[] fields = { "name", "content" };
QueryParser queryParser
= new MultiFieldQueryParser(fields, analyzer);
Query query
= queryParser.parse(queryString);

// 2,进行查询,从索引库中查找
IndexSearcher indexSearcher = new IndexSearcher(indexpath);
Filter filter
= null;
TopDocs topDocs
= indexSearcher.search(query, filter, 10000);
System.out.println(
"总共有【" + topDocs.totalHits + "】条匹配结果");

// 3,打印结果
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
// 文档内部编号
int index = scoreDoc.doc;
// 根据编号取出相应的文档
Document doc = indexSearcher.doc(index);
System.out.println(
"------------------------------");
System.out.println(
"name = " + doc.get("name"));
System.out.println(
"content = " + doc.get("content"));
System.out.println(
"size = " + NumberTools.stringToLong(doc.get("size")));
System.out.println(
"path = " + doc.get("path"));
}
}

/**
* 读取文件内容
*/
public static String readFileContent(File file) {
try {
BufferedReader reader
= new BufferedReader(new InputStreamReader(new FileInputStream(file)));
StringBuffer content
= new StringBuffer();
for (String line = null; (line = reader.readLine()) != null;) {
content.append(line).append(
"\n");
}
reader.close();
return content.toString();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}

分享到:
评论

相关推荐

    lucene.NET 中文分词

    lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮

    Lucene中文分词器包

    来自“猎图网 www.richmap.cn”基于IKAnalyzer分词算法的准商业化Lucene中文分词器。 1. 正向全切分算法,42万... 对未知词汇采用自识别结合二元切分算法,确保搜索召回率 (使用方法请参考IKAnalyzer V1.1版)

    Lucene中的FST算法描述

    描述了Lucene中如何使用FST算法构建term的内存索引,使用了很多图,直观的展现了FST图的构建流程,能够对想了解lucene内部实现机制原理的同学有帮助。

    lucene,lucene教程,lucene讲解

    第一个是 FSDirectory,它表示一个存储在文件系统中的索引的位置。 第二个是 RAMDirectory,它表示一个存储在内存当中的索引的位置。 public void add(Query query, BooleanClause.Occur occur) BooleanClause...

    lucene-core-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-core-7.2.1.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    Lucene.Net基本用法

    Lucene.Net基本用法.pdf

    Lucene中文分词器组件

    Lucene中文分词器组件,不错的。

    lucene 3.0 API 中文帮助文档 chm

    lucene 3.0 API中文帮助,学习的人懂得的

    lucene-core-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-core-7.7.0.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    lucene中的SpanQuery和PhraseQuery详解(有图示)

    本文档详细讲解了各种SpanQuery的用法,以及它跟PhraseQuery的区别

    lucene中文分词器(paoding解牛)

    lucene搜索引擎中文分词器,版本2.0.4,强大的中文分词效果在其它中文分词器当中独领风骚

    lucene +中文分词

    Lucene 与中文分词的结合

    lucene实例lucene实例

    lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例

    lucene中文下载

    lucene中文下载 学搜索引擎的来 lucene中文下载 学搜索引擎的来 lucene中文下载 学搜索引擎的来

    Lucene中文分词源码详解

    Lucene,作为一种全文搜索的辅助工具,为我们进行条件搜索,无论是像Google,Baidu之类的搜索引 擎,还是论坛中的搜索功能,还是其它C/S架构的搜索,都带来了极大的便利和比较高的效率。本文主要是利用Lucene对MS Sql...

    lucene-spatial3d-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-spatial3d-7.2.1.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    Lucene中文分词组件 JE-Analysis 1.4.0

    该组件免费安装使用传播,无限制商业应用,但暂不开源,也不提供任何保证 分词效率: 第一次分词需要1-2秒(读取词典),之后速度基本与Lucene自带分词持平 运行环境: Lucene 1.9+ 内存消耗: 30M+ ...

    lucene2.9.1所有最新开发包及源码及文档

    开源全文搜索工具包Lucene2.9.1的使用。 1. 搭建Lucene的开发环境:在classpath中添加lucene-core-2.9.1.jar包 2. 全文搜索的两个工作: 建立索引文件,搜索索引. 3. Lucene的索引文件逻辑结构 1) 索引(Index)由...

    引入局部统计识别高频词汇的Lucene中文分词程序STUSegmentConfig.rar

    引入局部统计识别高频词汇的Lucene中文分词程序STUSegmentConfig.rar

    Lucene中文分词组件 JE-Analysis 1.5.1

    全面支持Lucene 2.0 增强了词典维护的API 增加了商品编码的匹配 增加了Mail地址的匹配 实现了词尾消歧算法第二层的过滤 整理优化了词库 1.4.0 —— 2006-08-21 增加词典的动态扩展能力 1.3.3 ...

Global site tag (gtag.js) - Google Analytics