内存索引库

特点

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

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. openSUSE设置局域网的时间同步

  2. thymeleaf-迭代

    迭代 1.迭代基础 要在/WEB-INF/templates/product/list.html页面中显示产品,将使用一个表格.都将显示在一行(一个<tr>元素),所以对于我们的模板,我们 ...

  3. [HNOI 2009]有趣的数列

    Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...

  4. [HNOI2010]PLANAR

    题目描述 若能将无向图G=(V,E)画在平面上使得任意两条无重合顶点的边不相交,则称G是平面图.判定一个图是否为平面图的问题是图论中的一个重要问题.现在假设你要判定的是一类特殊的图,图中存在一个包含所 ...

  5. [SCOI2010]幸运数字

    题目描述 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运 ...

  6. poj 3384 半平面交

    Feng Shui Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5183   Accepted: 1548   Speci ...

  7. bzoj 3174: [Tjoi2013]拯救小矮人

    Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个小矮人, ...

  8. mysql中binlog与存储引擎的2PC

    mysql内部的2PC mysql开启binlog后实际上可以认为其数据有两份,binlog中一份,引擎中一份(这里先把存储引擎中数据看成整体的单独一份,另外也可以把binlog看成是一个引擎).既然 ...

  9. left join 连表时,on后多条件无效问题

    http://www.cnblogs.com/guixiaoming/p/6516261.html left join 连表时,on后多条件无效问题 最近开发素材库项目,各种关系复杂的表,一度6张表的 ...

  10. centos7 支持中文显示

    http://www.linuxidc.com/Linux/2017-07/145572.htm这篇文章比较全.我印证了一下,没有问题 centos7的与centos6有少许不同: 1.安装中文包: ...