内存索引库

特点

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

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. 【BZOJ2705】【Sdoi2012】Longge的问题

    Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出\(\Sigma gcd(i, N) (1 \leq i \leq N ...

  2. [UOJ] #217. 【UNR #1】奇怪的线段树

    题解见大佬博客 我的丑陋代码: #include<cstdio> #include<cstring> #include<cstdlib> inline int re ...

  3. ●POJ 3608 Bridge Across Islands

    题链: http://poj.org/problem?id=3608 题解: 计算几何,求两个凸包间的最小距离,旋转卡壳 两个凸包间的距离,无非下面三种情况: 所以可以基于旋转卡壳的思想,去求最小距离 ...

  4. LCT模板(BZOJ2631)

    用LCT实现路径加,路径乘,断开及加上一条边(保证是树),查询路径和. #include <cstdio> #include <algorithm> #define l(x) ...

  5. 【OCP|052】OCP最新题库解析(052)--小麦苗解答版

    [OCP|052]OCP最新题库解析(052)--小麦苗解答版 OCP最新题库解析历史连接(052):http://mp.weixin.qq.com/s/bUgn4-uciSndji_pUbLZfA ...

  6. jquery easyui datagrid 排序列

    点击排序列,将获取参数有:page=1&rows=10&sort=UserName&order=desc c#后台获取sort跟order参数 string sortColum ...

  7. localeCompare() 方法实现中文的拼音排序

    google了很多次才发现在国外网站上有提示如何比较中文,原文地址:http://www.webdeveloper.com/forum/showthread.php?t=9365 前提:使用Unico ...

  8. 网站用户身份识别俩大招之cookie

    导航: 原理介绍 代码实现 过程分析 追踪Cookie 原理介绍 众所周知,http协议是无状态的协议,简单理解是用户的前一步操作和后一步操作之间没有关系,互相不知道,不干扰.而在很多场景下,浏览网页 ...

  9. ACM Self Number

    In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. Fo ...

  10. jQuery 效果 – 停止动画

    jQuery stop() 方法用于在动画或效果完成前对它们进行停止. 点击这里,向上/向下滑动面板 实例 jQuery stop() 滑动 演示 jQuery stop() 方法. jQuery s ...