内存索引库

特点

在内存中开辟一块空间,专门为索引库存放。这样有以下几个特征:

1)    因为索引库在内存中,所以访问速度更快。

2)    在程序退出时,索引库中的文件也相应的消失了。

3)    如果索引库比较大,必须得保证足够多的内存空间。

编码

在cn.hqu.directory 下新建:DirectoryTest

/**

* 1、能不能设置很多个索引库

*    可以设置很多个索引库

* 2、索引库能不能合并起来

*    如果是内存索引库

*      Directory ramDirectory = new RamDirectory(Directory d);

*         这样就可以把一个索引库放入到内存索引库中

*    利用IndexWriter.addIndexesNoOptimize方法可以把很多个索引库进行合并操作

* 3、应用程序能不能在内存中和索引库进行交互

* @author Administrator

*

*/

public
class
DirectoryTest {

/**

* 内存索引库

*   *  速度比较快

*   *  数据是暂时的

*   *  内存索引库和文件索引库的特点正好互补

*/

@Test

public
void
testRam() throws Exception{

Directory directory = new RAMDirectory();

IndexWriter indexWriter = new IndexWriter(directory,LuceneUtils.analyzer,

MaxFieldLength.LIMITED);

Article article = new Article();

article.setId(1L);

article.setTitle("lucene可以做搜索引擎");

article.setContent("baidu,google都是很好的搜索引擎");

indexWriter.addDocument(DocumentUtils.article2Document(article));

indexWriter.close();

this.showData(directory);

}

private
void
showData(Directory directory) throws Exception{

IndexSearcher indexSearcher = new IndexSearcher(directory);

QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30,

new String[]{"title","content"},LuceneUtils.analyzer);

Query query = queryParser.parse("lucene");

TopDocs topDocs =indexSearcher.search(query, 10);

ScoreDoc[] scoreDocs = topDocs.scoreDocs;

List<Article> articleList = new ArrayList<Article>();

for(ScoreDoc scoreDoc:scoreDocs){

Document document =indexSearcher.doc(scoreDoc.doc);

articleList.add(DocumentUtils.document2Article(document));

}

for(Article article:articleList){

System.out.println(article.getId());

System.out.println(article.getTitle());

System.out.println(article.getContent());

}

}

}

在执行完这段代码以后,并没有在磁盘上出现索引库。所以单独使用内存索引库没有任何意义。

文件索引库与内存索引库的结合


当应用程序启动的时候,从文件索引库加载文件到内存索引库。应用程序直接与内存索引库交互。当应用程序退出的时候,内存索引库把数据再次保存到文件索引库,完成文件的保存工作。

/**

* 文件索引库和内存索引库的结合,提高效率

*/

@Test

public
void
testRamAndFile() throws Exception{

/**

*1、当应用程序启动的时候,把文件索引库的内容复制到内存库中

*2、让内存索引库和应用程序交互

*3、把内存索引库的内容同步到文件索引库

*/

Directory fileDirectory =FSDirectory.open(new File("./indexDir"));

Directory ramDirectory = new RAMDirectory(fileDirectory);

IndexWriter ramIndexWriter = new IndexWriter(ramDirectory,

LuceneUtils.analyzer,MaxFieldLength.LIMITED);

IndexWriter fileIndexWriter = new IndexWriter(fileDirectory,

LuceneUtils.analyzer,true,MaxFieldLength.LIMITED);

/**

在内存索引库中根据关键词查询在

*启动的时候,把文件目录的索引库加载到内存目录中,

* 退出时把内存目录的索引库保存到文件目录

*/

this.showData(ramDirectory);

System.out.println("上面的是从内存索引库中查询出来的");

/**

*把一条信息插入到内存索引库

*/

Article article = new Article();

article.setId(1L);

article.setTitle("lucene可以做搜索引擎");

article.setContent("baidu,google都是很好的搜索引擎");

ramIndexWriter.addDocument(DocumentUtils.article2Document(article));

ramIndexWriter.close();

/*

*把内存索引库中的内容同步到文件索引库中

*/

fileIndexWriter.addIndexesNoOptimize(ramDirectory);

fileIndexWriter.close();

this.showData(fileDirectory);

System.out.println("上面的是从文件索引库中查询出来的");

}

说明:

1)    Directory ramdirectory = newRAMDirectory(filedirectory);把filedirectory这个索引库加载到ramdirectory内存库中

2)    IndexWriter的构造函数:

IndexWriterfileIndexWriter = new IndexWriter(fileDirectory,

LuceneUtils.analyzer,true,MaxFieldLength.LIMITED);

True     重新创建一个或者覆盖(选择)

False    追加

1.             分词器

1.1英文分词器

步骤:Creates a searcher searching the index in the nameddirectory

1)    切分关键词

Creates

a

searcher

searching

the

index

the

named

directory

2)    去除停用词

停用词:有些词在文本中出现的频率非常高。但对本文的语义产生不了多大的影响。例如英文的a、an、the、of等。或中文的”的、了、呢等”。这样的词称为停用词。停用词经常被过滤掉,不会被进行索引。在检索的过程中,如果用户的查询词中含有停用词,系统会自动过滤掉。停用词可以加快索引的速度,减少索引库文件的大小。

Creates

searcher

searching

index

named

directory

3)    转为小写(搜索时不区分大小写,因为分词器会帮你转化)

creates

searcher

searching

index

named

directory

1.1.1代码:

@Test

public
void
testEn() throwsException{

/**

* Creates a searcher searching the index inthe named directory

*/

/**

* 1、切分关键词

* 2、去掉停用词

* 3、把大写转化成小写

*/

Stringtext = "Creates a searcher searching the index in the nameddirectory";

Analyzeranalyzer = new StandardAnalyzer(Version.LUCENE_30);

this.testAnalyzer(analyzer,text);

}

/**

* 经过该方法可以把分词后的结果输出

* @param analyzer

* @param text

* @throws Exception

*/

private
void
testAnalyzer(Analyzer analyzer,String text)throwsException{

TokenStream tokenStream = analyzer.tokenStream("content",new StringReader(text));

tokenStream.addAttribute(TermAttribute.class);

while(tokenStream.incrementToken()){

TermAttribute termAttribute =tokenStream.getAttribute(TermAttribute.class);

System.out.println(termAttribute.term());

}

}

1.2中文分词器

1.2.1单字分词

/**

* 单字分词

*/

Analyzeranalyzer = newChineseAnalyzer();

Stringtext = "新北校区有一个是UFO";

this.testAnalyzer(analyzer,text);

把汉字一个字一个字分解出来。效率比较低。

1.2.2二分法分词

Analyzeranalyzer = newCJKAnalyzer(Version.LUCENE_30);

Stringtext = "新北校区有一个是UFO";

this.testAnalyzer(analyzer, text);

把相邻的两个字组成词分解出来,效率也比较低。而且很多情况下分的词不对。

1.2.3词库分词(IKAnalyzer)

Analyzeranalyzer = newIKAnalyzer();

Stringtext = "北京美女";

this.testAnalyzer(analyzer, text);

导入IKAnalyzer的jar包。

网盘年下载:http://pan.baidu.com/s/1nt9eqVZ

基本上可以把词分出来(经常用的分词器)

1.2.4词库的扩充

“新北小去的阿尔法四了”分此后的结果为:

新、北、小、去、阿尔法、四、了

在src下新建:ext_stopword.dic、IKAnalyzer.cfg.xml、mydict.dic。

ext_stopword.dic为停止词的词库,词库里的词都被当作为停止词使用。

IKAnalyzer.cfg.xml为IKAnalyzer的配置文件。

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEpropertiesSYSTEM"http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>IK Analyzer 扩展配置</comment>

<entrykey="ext_dict">/mydict.dic</entry>

<entrykey="ext_stopwords">/ext_stopword.dic</entry>

</properties>

Key为ext_stopwords 为停止词所在的位置。

Key为ext_dict为配置自己的扩展字典所在的位置。如图所示可以在mydict.dic中添加自己所需要的词。如:”新北小去”

添加完以后分词器分” “新北小去的阿尔法四了”结果为:

新北小去、阿尔法、四、了

ext_stopword.dic如下:











使

















































mydict.dic 内容如下:

新北小去

1.2.5修改LuceneUtils类

analyzer =
new
IKAnalyzer();

以后用的分词库为IKAnalyzer中文分词库。

lucene内存索引库、分词器的更多相关文章

  1. Lucene 03 - 什么是分词器 + 使用IK中文分词器

    目录 1 分词器概述 1.1 分词器简介 1.2 分词器的使用 1.3 中文分词器 1.3.1 中文分词器简介 1.3.2 Lucene提供的中文分词器 1.3.3 第三方中文分词器 2 IK分词器的 ...

  2. lucene 内存索引 和文件索引 合并

    IndexWriter.addIndexes(ramDirectory); http://blog.csdn.net/qq_28042463/article/details/51538283 在luc ...

  3. Lucene全文搜索之分词器:使用IK Analyzer中文分词器(修改IK Analyzer源码使其支持lucene5.5.x)

    注意:基于lucene5.5.x版本 一.简单介绍下IK Analyzer IK Analyzer是linliangyi2007的作品,再此表示感谢,他的博客地址:http://linliangyi2 ...

  4. lucene查询索引库、分页、过滤、排序、高亮

    2.查询索引库 插入测试数据 xx.xx. index. ArticleIndex @Test public void testCreateIndexBatch() throws Exception{ ...

  5. Net Core使用Lucene.Net和盘古分词器 实现全文检索

    Lucene.net Lucene.net是Lucene的.net移植版本,是一个开源的全文检索引擎开发包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎, ...

  6. Lucene 07 - 对Lucene的索引库进行增删改查

    目录 1 添加索引 2 删除索引 2.1 根据Term删除索引 2.2 删除全部索引(慎用) 3 更新索引 数据保存在关系型数据库中, 需要实现增.删.改.查操作; 索引保存在索引库中, 也需要实现增 ...

  7. lucene定义自己的分词器将其分成单个字符

    问题描写叙述:将一句话拆分成单个字符.而且去掉空格. package com.mylucene; import java.io.IOException; import java.io.Reader; ...

  8. Lucene之索引库的维护:添加,删除,修改

    索引添加 Field域属性分类 添加文档的时候,我们文档当中包含多个域,那么域的类型是我们自定义的,上个案例使用的TextField域,那么这个域他会自动分词,然后存储 我们要根据数据类型和数据的用途 ...

  9. lucene 内存索引存储每个field里内容的相关代码

    相关的类调用关系 DocumentsWriterPerThread ——>DocFieldProcessor   DocumentsWriterPerThread里的consumer对象(类型是 ...

随机推荐

  1. [测试题]gentree

    Description 给你一个有向连通图G,每点有个权值Di(0<Di),要求生成一棵树根为1号节点的有根树T.对于树中边E,E的代价为所有从根出发的且包含E的路径的终点权值的和.现求生成树T ...

  2. [HNOI2004]L语言

    题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...

  3. 【QAQ的Minecraft】

    树套树被QAQ用木斧挖了,只剩二维RMQ了. 题目:      QAQ最近爱上了一款很平凡的游戏,叫做<Minecraft>.目前游戏更新到了1.12版本,他发现了一条新的指令:/fill ...

  4. bzoj3309DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1240  Solved: 777[Submit][Statu ...

  5. poj3237 树链部分 边权模板

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 7384   Accepted: 2001 Description ...

  6. Mac上安装brew 包管理工具

    Mac 上的包管理工具对于开发者来说是一件非常方便的工具,能够有效的对包进行管理. 所以这篇博客就来简单的讲一下brew 的安装和一些基础命令. brew 全称叫做Homebrew . 1. 首先来说 ...

  7. Java 反射 Method threw 'java.lang.InstantiationException' exception.

    查看这个InstantiationException:异常的api所说的是: 当应用程序试图使用 Class 类中的 newInstance 方法创建一个类的实例,而指定的类对象无法被实例化时,抛出该 ...

  8. PostgreSQL 中如何实现group_concat

    之前在MySQL中使用group_concat,觉得超级好用. 今天在PostgreSQL需要用到这样的场景,就去学习了一下. 在PostgreSQL中提供了arr_agg的函数来实现聚合,不过返回的 ...

  9. Java内存分配、管理小结

    转载自:http://java-mzd.iteye.com/blog/848635

  10. 取list的值

    list.get(0):之类的我就不写了 我就写一个我老忘记的 Iterator it = list.iterator(); while(it.hasNext()){ Student stu = it ...