lucene学习-创建索引
本文的lucene是基于lucene3.5版本.
使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索。本节主要是记录创建索引部分的内容。
创建的索引结构如图所示。

创建索引的步骤分为以下几个步骤:
1、建立索引器IndexWriter
2、创建文档对象Document
3、建立信息对象字段Field
4、将Field对象添加到Document
5、将Document对象添加到IndexWriter对象中
下面简要介绍几个核心对象。
(1)、创建IndexWriter对象。
IndexWriter writer=new IndexWriter(directory, iwc)。
directory是创建的索引要保存的路径,如果要保存在硬盘中则使用Directory directory = FSDirectory.open(new File(path))创建一个directory对象。
如果要保存在内存中则使用:RAMDirectory directory=new RAMDirectory()创建一个directory对象。
(2)、创建Document对象。
Document doc =new Document();创建了一个不含有任何Field的空Document,如果要要Field添加到Document中,则使用add(Field)方法即可实现。
doc.add(field)。
(3)、创建Field对象。
Field field=new Field(Field名称,Field内容,存储方式,索引方式);
存储方式分为3种:1、完全存储(Field.Store.YES);2、不存储(Field.Store.NO);3、压缩存储(Field.Store.COMPRESS)。
索引方式分为4种:1、不索引(Field.Index.NO);2、 Field.Index.ANALYZED ;3、 Field.Index.NOT_ANALYZED;4、Field.Index.NOT_ANALYZED_NO_NORMS
创建一个简单的索引程序代码如下所示:
public void Index() {
String[] ids = { "1", "2", "3", "4" };
String[] names = { "aa", "bb", "cc", "dd" };
String[] contents = {
"Using AbstractJExcelView to export data to Excel file via JExcelAPI library",
"Using AbstractPdfView to export data to Pdf file via Bruno Lowagie’s iText library. ",
"Example to integrate Log4j into the Spring MVC application. ",
"Using Hibernate validator (JSR303 implementation) to validate bean in Spring MVC. " };
IndexWriter writer = null;
try {
Directory directory = FSDirectory.open(new File(path));
// RAMDirectory directory=new RAMDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35));
writer = new IndexWriter(directory, iwc);
Document doc = null;
for (int i = 0; i < ids.length; i++) {
doc = new Document();
doc.add(new Field("id", ids[i], Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("name", names[i], Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("contents", contents[i], Field.Store.YES,
Field.Index.ANALYZED));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
doc.add(new Field("date", sdf.format(new Date()),
Field.Store.YES, Field.Index.NOT_ANALYZED));
// Field.Index.ANALYZED;
writer.addDocument(doc);
writer.commit();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
lucene学习-创建索引的更多相关文章
- lucene简介 创建索引和搜索初步
lucene简介 创建索引和搜索初步 一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引 ...
- lucene中创建索引库
package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...
- lucene入门创建索引——(二)
1.程序宏观结构图
- 搜索引擎系列 ---lucene简介 创建索引和搜索初步
一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子 ...
- 搜索引擎学习(二)Lucene创建索引
PS:需要用到的jar包: 代码实现 1.工程结构 2.设置工程依赖的jar包 3.代码实现 /** * Lucene入门 * 创建索引 */ public class CreateIndex { / ...
- HBase中创建索引
hbasene(https://github.com/akkumar/hbasene)是开源项目,在hbase存储上封装使用Lucene来创建索引,代码API非常简单,熟悉lucene的朋友可以很方便 ...
- lucene&solr学习——创建和查询索引(代码篇)
1. Lucene的下载 Lucene是开发全文检索功能的工具包,从官网下载Lucene4.10.3并解压. 官网:http://lucene.apache.org/ 版本:lucene7.7.0 ( ...
- lucene&solr学习——创建和查询索引(理论)
1.Lucene基础 (1) 简介 Lucene是apache下的一个开放源代码的全文检索引擎工具包.提供完整的查询引擎和索引引擎:部分文本分析引擎. Lucene的目的是为软件开发人员提供一个简单易 ...
- Apache Lucene(全文检索引擎)—创建索引
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...
随机推荐
- virtualenv、virtualenvwrapper安装和使用;Mac os的特殊性
[sudo] pip install virtualenv 或者[sudo] pip3 install virtualenv [sudo]可用可不用 pip/pip3 install virtuale ...
- 一天干掉一只Monkey计划(序)【转】
http://www.cnblogs.com/Zephyroal/archive/2011/10/10/2206509.html 一天干掉一只Monkey计划(序) 一天干掉一只Monkey计划(序) ...
- 报错:numRecords must not be negative
报错的原因:删除已经使用过的kafka topic,然后新建同名topic 解决方法:把topic名字换一下 (有其他更好的解决方法,可以不修改topic名)
- Foundation框架 - NSNumber类
NSNumber类 NSFormatter #import <Foundation/Foundation.h> int main(int argc, const char * argv[] ...
- mybatis常用jdbcType数据类型以及对应的JavaType
1.MyBatis 通过包含的jdbcType类型 BIT.FLOAT.CHAR .TIMESTAMP . OTHER .UNDEFINEDTINYINT .REAL .VARCHAR .BINARY ...
- 倍福TwinCAT(贝福Beckhoff)应用教程11.1 TwinCAT应用小程序1 贝福IO模块介绍
EL1002,EL1004,EL1008都是数字输入模块(2个点,4个点,8个点),输入高的范围是15V到30V,低的范围是-3V到5V EL2002,EL2004,EL2008都是数 ...
- css - margin-padding
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CSS Flexible 布局兼容性以及解决方案
1.兼容性 https://caniuse.com/#feat=flexbox IE11以及以下不支持. 2.解决方案 flexibility库 https://github.com/jonathan ...
- js 根据身份证获取出生日期及性别
js根据身份证获取出生日期及性别 CreateTime--2017年6月2日11:45:16Author:Marydon 第一步:身份证号格式校验 /** * 身份证号格式校验 */ functi ...
- QT调用C#写的Dll
参见: https://blog.csdn.net/weixin_42420155/article/details/81060945 C#写的dll是没有dllMain入口函数的,是一种中间语言,需要 ...