Lucenefnm索引文件格式源码解析
2023-03-15 06:06:49 来源:易采站长站 作者:
目录
简介版本涉及的主要类代码示例文件结构全局示意图字段描述HeaderFieldCountFieldFieldName FieldNumber FieldBitsIndexOptionsDocValuesBitsDocValuesGenAttributesPointDimensionCountPointNumBytesVectorDimensionVectorSimilarityFunctionFooter简介
后缀为fnm文件是存储索引的字段的元信息,包含字段名称,字段类型,字段属性等信息。
版本
lucene>
涉及的主要类
fnm索引文件的生成源码比较简单,不贴了,主要逻辑在:
org.apache.lucene.codecs.lucene90.Lucene90FieldInfosFormat
代码示例
FieldType fieldType = new FieldType(); fieldType.setStored(true); fieldType.setStoreTermVectors(true); fieldType.setStoreTermVectorOffsets(true); fieldType.setStoreTermVectorPositions(true); fieldType.setStoreTermVectorPayloads(true); fieldType.setTokenized(true); fieldType.setOmitNorms(true); fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); Document doc = new Document(); doc.add(new Field("name", "maria", fieldType)); doc.add(new SortedDocValuesField("name", new BytesRef("maria"))); doc.add(new IntPoint("id", 1, 2, 3)); doc.add(new KnnVectorField("vector", new float[]{1.1f, 2.2f, 3.3f}, VectorSimilarityFunction.COSINE));
文件结构全局示意图
字段描述
Header
文件头部信息,主要是包括:
- 文件头魔数(同一lucene版本所有文件相同)该文件使用的codec名称:Lucene90FieldInfos(codec可以理解成文件的布局格式,不同版本lucene相同后缀文件有不一样的版本格式)codec版本segment后缀名(一般为空)segment>
FieldCount
该索引的field总数
Field
记录字段的元信息
FieldName
字段名称,比如示例代码中的name,id,vector都是字段名称
FieldNumber
字段的编号
FieldBits
部分属性的位图信息,是一个组合值,描述字段是否具有以下属性:
- 是否存储词向量(termVector):0x1是否要忽略norm值:0x2是否带有payload:0x4该字段是否是软删除字段(soft>
示例代码中的name字段的FieldBits的值为:0x1 | 0x2 | 0x4 = 0x7
IndexOptions
字段的索引选项,表示在索引该字段的时候存储的倒排信息有哪些,所有的类型:
- 0:NONE1:DOCS2:DOCS_AND_FREQS3:DOCS_AND_FREQS_AND_POSITIONS4:DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS
DocValuesBits
官方文档描述的是由norm和docValue类型的组合值,但是从源码看只存储了docValue类型。
- 0:NONE1:NUMERIC2:BINARY3:SORTED4:SORTED_SET5:SORTED_NUMERIC
DocValuesGen
可以理解为字段DocValues的版本号,通过IndexWriter.updateDocValues(...)会更新该版本号
Attributes
可能的值有:
PointDimensionCount
如果字段是IntPoint,LongPoint等类型,则记录维数。
PointNumBytes
如果字段是IntPoint,LongPoint等类型,则记录每一维数据存储需要的字节个数。
VectorDimension
向量字段记录向量的维数
VectorSimilarityFunction
向量相似度衡量函数:
- EUCLIDEAN:欧式距离DOT_PRODUCT:点积COSINE:consine距离
Footer
文件尾,主要包括
- 文件尾魔数(同一个lucene版本所有文件一样)0校验码
以上就是Lucene>