ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

4.Lucene创建索引

2020-10-24 19:36:11  阅读:224  来源: 互联网

标签:创建 writer Lucene 索引 文档 FieldType new IndexOptions


一、指定一个存放索引的目录

这里我指定了一个绝对的位置,这个位置要能读写数据。

// lucene索引目录位置
String indexDir = "E:\\develop\\demo\\lucene-learn\\lucene-index";
File luceneIndexDirectory = new File(indexDir);

二、创建一个索引写入类IndexWriter,用于写入索引和文档对象到索引目录

打开Directory时,有多种实现,我们常用有:

FSDirectory:直接操作磁盘(固态推荐)

MMapDirectory:内存映射(大内存推荐)

优化的参数MaxBufferedDocs和forceMerge根据磁盘的读写速度自行调整。

// 打开索引目录
Directory fsd = MMapDirectory.open(luceneIndexDirectory.toPath());
// 创建一个索引写入类IndexWriter,用于写入索引和文档对象到索引目录
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new IKAnalyzer());
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
// 设置缓存区大小,每n个文档写入一次
indexWriterConfig.setMaxBufferedDocs(1000);
IndexWriter writer = new IndexWriter(fsd, indexWriterConfig);
// 设置每个segments保存几个文档,该值越大创建索引时间越小,对应的搜索会变慢
writer.forceMerge(100);

注意:IndexWriter 只能存在一个,在上一个没有关闭之前创建会抛出异常。

三、创建存放数据的类型,指定是否需要存储、是否分词、是否建立索引等

IndexOptions说明:

属性 说明
NONE 不建立索引
DOCS 文档建立索引
DOCS_AND_FREQS 文档、词频建立索引
DOCS_AND_FREQS_AND_POSITIONS 文档、词频、词位置建立索引
DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS 文档、词频、词位置、偏移量建立索引

Stored:是否存储,如果存储了就可以在文档中读取内容,如果不存储就读取不到内容。

// 定义字段
// 1. id:存储、索引、不分词
FieldType idFieldType = new FieldType();
idFieldType.setStored(true);
idFieldType.setTokenized(false);
idFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);

// 2. title:存储、分词、索引
FieldType titleFieldType = new FieldType();
titleFieldType.setStored(true);
titleFieldType.setTokenized(true);
titleFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);

// 3. content:存储、分词、索引
FieldType contentFieldType = new FieldType();
contentFieldType.setStored(true);
contentFieldType.setTokenized(true);
contentFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);

三、创建存放数据的文档,把数据按照类型存放进去

如果有多个Document文档对象,创建一个 List<Document> ,把组装的Document写到 list 中即可。

// 创建文档对象,把字段内容写入到文档对象
Document document = new Document();
document.add(new Field("id", "1", idFieldType));
document.add(new Field("title", "IndexOptions类说明", titleFieldType));
document.add(new Field("content", "IndexOptions是在lucene-core-x.jar包下面,其作用是在新建索引时候选择索引属性。", contentFieldType));

四、把文档添加到到索引中

addDocument可保存单个Document,List<Document>也是可以直接保存的。

// 把文档添加到IndexWriter
writer.addDocument(document);

五、保存索引和数据到索引目录

添加后记得手动把缓存的数据保存到目录,不然会根据writer设置的参数进行缓存,到达阈值才触发保存操作,这样如果程序停止了,缓存中数据没保存的话就丢失了。

// 提交保存索引
writer.flush();
writer.commit();
writer.close();

六、使用luke查看创建的索引

我们把luke8.0.0下载下来,然后打开软件,选择索引目录即可查看。

2020-10-12_17-26-48

2020-10-12_17-30-59

附录:完整代码

@Test
public void buildIndex() {
    // lucene索引目录位置
    String indexDir = "E:\\develop\\demo\\lucene-learn\\lucene-index";
    File luceneIndexDirectory = new File(indexDir);
    // 打开索引目录
    // 可以使用 MMapDirectory(内存映射来加快查询速度,但是比较占用内储)
    //try (FSDirectory fsd = FSDirectory.open(luceneIndexDirectory.toPath())) {
    try (Directory fsd = MMapDirectory.open(luceneIndexDirectory.toPath())) {
        // 创建索引写入
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new IKAnalyzer());
        indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        // 设置缓存区大小,每n个文档写入一次
        indexWriterConfig.setMaxBufferedDocs(1000);

        IndexWriter writer = new IndexWriter(fsd, indexWriterConfig);
        // 设置每个segments保存几个文档,该值越大创建索引时间越小,对应的搜索会变慢
        writer.forceMerge(100);

        // 定义字段
        // 1. id:存储、索引、不分词
        FieldType idFieldType = new FieldType();
        idFieldType.setStored(true);
        idFieldType.setTokenized(false);
        idFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
        // 2. title:存储、分词、索引
        FieldType titleFieldType = new FieldType();
        titleFieldType.setStored(true);
        titleFieldType.setTokenized(true);
        titleFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
        // 3. content:存储、分词、索引
        FieldType contentFieldType = new FieldType();
        contentFieldType.setStored(true);
        contentFieldType.setTokenized(true);
        contentFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);

        // 创建文档对象,把字段内容写入到文档对象
        Document document = new Document();
        document.add(new Field("id", "1", idFieldType));
        document.add(new Field("title", "IndexOptions类说明", titleFieldType));
        document.add(new Field("content", "IndexOptions是在lucene-core-x.jar包下面,其作用是在新建索引时候选择索引属性。", contentFieldType));

        // 把文档添加到IndexWriter
        writer.addDocument(document);

        // 提交保存索引
        writer.flush();
        writer.commit();
        writer.close();
    } catch (IOException e) {
        System.err.println("打开索引目录失败");
        e.printStackTrace();
    }
}

标签:创建,writer,Lucene,索引,文档,FieldType,new,IndexOptions
来源: https://www.cnblogs.com/lixingwu/p/13870588.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有