内存索引库

特点

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

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. [HNOI 2015]亚瑟王

    Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂 亮.众所周知,亚瑟王是一 ...

  2. 计蒜客NOIP模拟赛4 D1T2小X的密室

    小 X 正困在一个密室里,他希望尽快逃出密室. 密室中有 N 个房间,初始时,小 X 在 1 号房间,而出口在 N 号房间. 密室的每一个房间中可能有着一些钥匙和一些传送门,一个传送门会单向地创造一条 ...

  3. 洛谷P1446 [HNOI2008]Cards

    置换群+dp #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring& ...

  4. ●BZOJ 3529 [Sdoi2014]数表

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3529 题解: 莫比乌斯反演. 按题目的意思,令$f(i)$表示i的所有约数的和,就是要求: ...

  5. 【模版 Floyd最小环】

    输入 输入中有多组数据.请用SeekEof判断是否到达文件结束. 对于每组数据: 第一行有两个正整数N,M,分别表示学校的景点个数和有多少对景点之间直接有边相连.(N< =100,M< = ...

  6. bzoj2839: 集合计数 容斥+组合

    2839: 集合计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 523  Solved: 287[Submit][Status][Discuss] ...

  7. Codeforces 666E Forensic Examination

    题意:给定主串s和m个模式串,每次询问[l,r]的模式串中出现s[pl...pr]次数最多的串和次数. 这题挺简单的,先把所有模式串拿来建广义后缀自动机,询问相当于子树众数,用线段树合并即可. 那我为 ...

  8. 如何彻底删除mysql

    MySQL的卸载确实很让人头疼,很多时候都无法彻底卸载干净,这样会导致我们无法重新安装新的MySQL. 下面介绍,在Windows10系统下,如何彻底删除卸载MySQL... 1>停止MySQL ...

  9. DeepMoji:机器学习模型分析情绪, 情感

    DeepMoji 是一个模型,接受12亿个带有表情的推文,以了解语言如何表达情绪. 通过转移学习,该模型可以在许多情感相关的文本建模任务上获得最先进的表现. 在 http://deepmoji.mit ...

  10. salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型

    看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...