博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lucene-索引详解(3)
阅读量:4037 次
发布时间:2019-05-24

本文共 4374 字,大约阅读时间需要 14 分钟。

IndexWriter详解

  创建API详解图示

  代码示例

import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.core.SimpleAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.StoredField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import java.io.File;import java.io.IOException;/*** *@author dongsheng *@date 2020/3/19 15:10 *@version 1.0.0 *@Description */public class CreateIndexTest {    public static void main(String[] args) throws IOException {        // 创建使用的分词器        Analyzer analyzer = new SimpleAnalyzer();        // 索引配置对象        IndexWriterConfig config = new IndexWriterConfig(analyzer);        // 设置索引库的打开模式:新建、追加、新建或追加        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);        // 索引存放目录        // 存放到文件系统中        Directory directory = FSDirectory                .open((new File("f:/test/indextest")).toPath());        // 存放到内存中        // Directory directory = new RAMDirectory();        // 创建索引写对象        IndexWriter writer = new IndexWriter(directory, config);        // 创建document        Document doc = new Document();        // 往document中添加 商品id字段        doc.add(new StoredField("productId", "00001"));        // 往document中添加 商品名称字段        String name = "ThinkPad X1 Carbon 20KH0009CD/25CD 超极本轻薄笔记本电脑联想";        doc.add(new TextField("name", name, Field.Store.YES));        writer.addDocument(doc);    }}

设计类图

IndexWriterCconfig 索引的配置

  • 使用分词器
  • 打开索引(创建、追加)
  • 可配置缓冲区大小或者存储多少个文档,再刷新
  • 可配置合并、删除等策略

Directory 存放的位置

从类结构看

内存、文件系统、数据库

Directory directory = FSDirectory.open(path文件目录地址)

创建、维护索引的API流程

// 创建索引写对象IndexWriter writer = new IndexWriter(directory, config);// 创建document// 将文档添加到索引writer.addDocument(doc);// 删除文档//writer.deleteDocuments(terms);//修改文档//writer.updateDocument(term, doc);// 刷新writer.flush();// 提交writer.commit();//indexwriter  是一个线程安全的,如果你要使用其它同步控制,请避免死锁,竟量不使用。

Document 文档详解

    索引的数据记录、文档在lucene中的表示,是索引、搜索的基本单元。一个Document由多个字段Field构成。就像数据库的记录-字段。IndexWriter按加入的顺序为Document指定一个递增的id(从0开始),称为文档id。反向索引中存储的是这个id,文档存储中正向索引也是这个id。业务数据的主键id只是文档的一个字段。

Field

字段:由字段名name、字段值value(fieldsData)、字段类型 type 三部分构成。
字段值可以是文本(String、Reader 或 预分析的 TokenStream)、二进制值(byte[])或数值。

IndexableFieldType

字 段 类 型 : 描 述 该 如 何 索 引 存 储 该 字 段

注意:未存储的字段,从索引中取得的document中是没有这些字段的。

IndexOptions  是否忽略标准化

 NONE  Not indexed 不索引

 DOCS  反向索引中只存储了包含该词的 文档id,没有词频、位置
 DOCS_AND_FREQS 反向索引中会存储 文档id、词频
 DOCS_AND_FREQS_AND_POSITIONS 反向索引中存储 文档id、词频、位置
 DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS  反向索引中存储 文档id、词频、位置、偏移量

storeTermVectors

对于不需要在搜索反向索引时用到,但在搜索结果处理时需要的位置、偏移量、附加数据(payLoad) 的字段,我们可以单独为该字段存储(文档id词项向量)的正向索引。

  1.  boolean storeTermVectors() 是否存储词项向量
  2.  boolean storeTermVectorPositions() 是否在词项向量中存储位置
  3.  boolean storeTermVectorOffsets() 是否在词项向量中存储偏移量
  4.  boolean storeTermVectorPayloads() 是否在词项向量中存储附加信息

附加信息Payloads

docValuesType
IndexableFieldType 中的 docValuesType方法 就是让你来为需要排序、分组、
聚合的字段指定如何为该字段创建文档->字段值的正向索引的。

空间换时间

  对这种需要排序、分组、聚合的字段,为其建立独立的文档->字段值的正向
索引、列式存储。这样我们要加载搜中文档的这个字段的数据就快很多,
耗内存少。

DocValuesType 选项说明

  • NONE 不开启docvalue
  • NUMERIC 单值、数值字段,用这个
  • BINARY 单值、字节数组字段用
  • SORTED 单值、字符字段用, 会预先对值字节进行排序、去重存储
  • SORTED_NUMERIC 单值、数值数组字段用,会预先对数值数组进行排序
  • SORTED_SET 多值字段用,会预先对值字节进行排序、去重存储

注:DocValuesType 是 强 类 型 要 求 的 ,字 段 的 值 必 须 保 证 同 类 型

具体的选择

  • 字符串+单值 会选择SORTED作为docvalue存储
  • 字符串+多值 会选择SORTED_SET作为docvalue存储
  • 数值或日期或枚举字段+单值 会选择NUMERIC 作为docvalue存储
  • 数值或日期或枚举字段+多值 会选择SORTED_SET作为docvalue存储

Lucene预定义的字段子类,你可灵活选用

  • TextField: Reader or String indexed for full-text search
  • StringField: String indexed verbatim as a single token
  • IntPoint: int indexed for exact/range queries.
  • LongPoint: long indexed for exact/range queries.
  • FloatPoint: float indexed for exact/range queries.
  • DoublePoint: double indexed for exact/range queries.
  • SortedDocValuesField: byte[] indexed column-wise for sorting/faceting
  • SortedSetDocValuesField: SortedSet<byte[]> indexed column-wise for sorting/faceting
  • NumericDocValuesField: long indexed column-wise for sorting/faceting
  • SortedNumericDocValuesField: SortedSet<long> indexed column-wise for sorting/faceting
  • StoredField: Stored-only value for retrieving in summary results

luke索引查看工具安装

下载地址: 

开箱即用

IndexWriter 索引更新 API

说明:

  •  Term 词项 指定字段的词项
  •  删除流程:根据Term、Query找到相关的文档id、同时删除索引信息,再根据文档id删除对应的文档存储。
  •  更新流程:先删除、再加入新的doc
  •  注意:只可根据索引的字段进行更新。

转载地址:http://aujdi.baihongyu.com/

你可能感兴趣的文章
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
补充自动屏蔽攻击ip
查看>>
谷歌走了
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>