Namespace Lucene.Net.Codecs.Lucene41
Lucene 4.1 file format.
Apache Lucene - Index File Formats
Introduction
This document defines the index file formats used in this version of Lucene. If you are using a different version of Lucene, please consult the copy of docs/
that was distributed with the version you are using.
Apache Lucene is written in Java, but several efforts are underway to write versions of Lucene in other programming languages including this implementation in .NET. If these versions are to remain compatible with Apache Lucene, then a language-independent definition of the Lucene index format is required. This document thus attempts to provide a complete and independent definition of the Apache Lucene file formats.
As Lucene evolves, this document should evolve. Versions of Lucene in different programming languages should endeavor to agree on file formats, and generate new versions of this document.
Definitions
The fundamental concepts in Lucene are index, document, field and term.
An index contains a sequence of documents.
A document is a sequence of fields.
A field is a named sequence of terms.
A term is a sequence of bytes.
The same sequence of bytes in two different fields is considered a different term. Thus terms are represented as a pair: the string naming the field, and the bytes within the field.
Inverted Indexing
The index stores statistics about terms in order to make term-based search more efficient. Lucene's index falls into the family of indexes known as an inverted index. This is because it can list, for a term, the documents that contain it. This is the inverse of the natural relationship, in which documents list terms.
Types of Fields
In Lucene, fields may be stored, in which case their text is stored in the index literally, in a non-inverted manner. Fields that are inverted are called indexed. A field may be both stored and indexed.
The text of a field may be tokenized into terms to be indexed, or the text of a field may be used literally as a term to be indexed. Most fields are tokenized, but sometimes it is useful for certain identifier fields to be indexed literally.
See the Field docs for more information on Fields.
Segments
Lucene indexes may be composed of multiple sub-indexes, or segments. Each segment is a fully independent index, which could be searched separately. Indexes evolve by:
Creating new segments for newly added documents.
Merging existing segments.
Searches may involve multiple segments and/or multiple indexes, each index potentially composed of a set of segments.
Document Numbers
Internally, Lucene refers to documents by an integer document number. The first document added to an index is numbered zero, and each subsequent document added gets a number one greater than the previous.
Note that a document's number may change, so caution should be taken when storing these numbers outside of Lucene. In particular, numbers may change in the following situations:
The numbers stored in each segment are unique only within the segment, and must be converted before they can be used in a larger context. The standard technique is to allocate each segment a range of values, based on the range of numbers used in that segment. To convert a document number from a segment to an external value, the segment's base document number is added. To convert an external value back to a segment-specific value, the segment is identified by the range that the external value is in, and the segment's base value is subtracted. For example two five document segments might be combined, so that the first segment has a base value of zero, and the second of five. Document three from the second segment would have an external value of eight.
When documents are deleted, gaps are created in the numbering. These are eventually removed as the index evolves through merging. Deleted documents are dropped when segments are merged. A freshly-merged segment thus has no gaps in its numbering.
Index Structure Overview
Each segment index maintains the following:
Segment info. This contains metadata about a segment, such as the number of documents, what files it uses,
Field names. This contains the set of field names used in the index.
Stored Field values. This contains, for each document, a list of attribute-value pairs, where the attributes are field names. These are used to store auxiliary information about the document, such as its title, url, or an identifier to access a database. The set of stored fields are what is returned for each hit when searching. This is keyed by document number.
Term dictionary. A dictionary containing all of the terms used in all of the indexed fields of all of the documents. The dictionary also contains the number of documents which contain the term, and pointers to the term's frequency and proximity data.
Term Frequency data. For each term in the dictionary, the numbers of all the documents that contain that term, and the frequency of the term in that document, unless frequencies are omitted (IndexOptions.DOCS_ONLY)
Term Proximity data. For each term in the dictionary, the positions that the term occurs in each document. Note that this will not exist if all fields in all documents omit position data.
Normalization factors. For each field in each document, a value is stored that is multiplied into the score for hits on that field.
Term Vectors. For each field in each document, the term vector (sometimes called document vector) may be stored. A term vector consists of term text and term frequency. To add Term Vectors to your index see the Field constructors
Per-document values. Like stored values, these are also keyed by document number, but are generally intended to be loaded into main memory for fast access. Whereas stored values are generally intended for summary results from searches, per-document values are useful for things like scoring factors.
Deleted documents. An optional file indicating which documents are deleted.
Details on each of these are provided in their linked pages.
File Naming
All files belonging to a segment have the same name with varying extensions. The extensions correspond to the different file formats described below. When using the Compound File format (default in 1.4 and greater) these files (except for the Segment info file, the Lock file, and Deleted documents file) are collapsed into a single .cfs file (see below for details)
Typically, all segments in an index are stored in a single directory, although this is not required.
As of version 2.1 (lock-less commits), file names are never re-used (there is one exception, "segments.gen", see below). That is, when any file is saved to the Directory it is given a never before used filename. This is achieved using a simple generations approach. For example, the first segments file is segments_1
, then segments_2
, etc. The generation is a sequential long integer represented in alpha-numeric (base 36) form.
Summary of File Extensions
The following table summarizes the names and extensions of the files in Lucene:
Name | Extension | Brief Description |
---|---|---|
[Segments File](xref:Lucene.Net.Index.SegmentInfos) | segments.gen, segments_N | Stores information about a commit point |
[Lock File](#lock-file) | write.lock | The Write lock prevents multiple IndexWriters from writing to the same file. |
[Segment Info](xref:Lucene.Net.Codecs.Lucene40.Lucene40SegmentInfoFormat) | .si | Stores metadata about a segment |
[Compound File](xref:Lucene.Net.Store.CompoundFileDirectory) | .cfs, .cfe | An optional "virtual" file consisting of all the other index files for systems that frequently run out of file handles. |
[Fields](xref:Lucene.Net.Codecs.Lucene40.Lucene40FieldInfosFormat) | .fnm | Stores information about the fields |
[Field Index](xref:Lucene.Net.Codecs.Lucene41.Lucene41StoredFieldsFormat) | .fdx | Contains pointers to field data |
[Field Data](xref:Lucene.Net.Codecs.Lucene41.Lucene41StoredFieldsFormat) | .fdt | The stored fields for documents |
[Term Dictionary](xref:Lucene.Net.Codecs.Lucene41.Lucene41PostingsFormat) | .tim | The term dictionary, stores term info |
[Term Index](xref:Lucene.Net.Codecs.Lucene41.Lucene41PostingsFormat) | .tip | The index into the Term Dictionary |
[Frequencies](xref:Lucene.Net.Codecs.Lucene41.Lucene41PostingsFormat) | .doc | Contains the list of docs which contain each term along with frequency |
[Positions](xref:Lucene.Net.Codecs.Lucene41.Lucene41PostingsFormat) | .pos | Stores position information about where a term occurs in the index |
[Payloads](xref:Lucene.Net.Codecs.Lucene41.Lucene41PostingsFormat) | .pay | Stores additional per-position metadata information such as character offsets and user payloads |
[Norms](xref:Lucene.Net.Codecs.Lucene40.Lucene40NormsFormat) | .nrm.cfs, .nrm.cfe | Encodes length and boost factors for docs and fields |
[Per-Document Values](xref:Lucene.Net.Codecs.Lucene40.Lucene40DocValuesFormat) | .dv.cfs, .dv.cfe | Encodes additional scoring factors or other per-document information. |
[Term Vector Index](xref:Lucene.Net.Codecs.Lucene40.Lucene40TermVectorsFormat) | .tvx | Stores offset into the document data file |
[Term Vector Documents](xref:Lucene.Net.Codecs.Lucene40.Lucene40TermVectorsFormat) | .tvd | Contains information about each document that has term vectors |
[Term Vector Fields](xref:Lucene.Net.Codecs.Lucene40.Lucene40TermVectorsFormat) | .tvf | The field level info about term vectors |
[Deleted Documents](xref:Lucene.Net.Codecs.Lucene40.Lucene40LiveDocsFormat) | .del | Info about what files are deleted |
Lock File
The write lock, which is stored in the index directory by default, is named
write.lock
. If the lock directory is different from the index directory then
the write lock will be named XXXX-write.lock
where XXXX is a unique prefix
derived from the full path to the index directory. When this file is present, a
writer is currently modifying the index (adding or removing documents). This
lock file ensures that only one writer is modifying the index at a time.
History
Compatibility notes are provided in this document, describing how file formats have changed from prior versions:
In version 2.1, the file format was changed to allow lock-less commits (ie, no more commit lock). The change is fully backwards compatible: you can open a pre-2.1 index for searching or adding/deleting of docs. When the new segments file is saved (committed), it will be written in the new file format (meaning no specific "upgrade" process is needed). But note that once a commit has occurred, pre-2.1 Lucene will not be able to read the index.
In version 2.3, the file format was changed to allow segments to share a single set of doc store (vectors & stored fields) files. This allows for faster indexing in certain cases. The change is fully backwards compatible (in the same way as the lock-less commits change in 2.1).
In version 2.4, Strings are now written as true UTF-8 byte sequence, not Java's modified UTF-8. See LUCENE-510 for details.
In version 2.9, an optional opaque
IDictionary<string, string>
CommitUserData may be passed to IndexWriter's commit methods (and later retrieved), which is recorded in thesegments_N
file. See LUCENE-1382 for details. Also, diagnostics were added to each segment written recording details about why it was written (due to flush, merge; which OS/JRE was used; etc.). See issue LUCENE-1654 for details.In version 3.0, compressed fields are no longer written to the index (they can still be read, but on merge the new segment will write them, uncompressed). See issue LUCENE-1960 for details.
In version 3.1, segments records the code version that created them. See LUCENE-2720 for details. Additionally segments track explicitly whether or not they have term vectors. See LUCENE-2811 for details.
In version 3.2, numeric fields are written as natively to stored fields file, previously they were stored in text format only.
In version 3.4, fields can omit position data while still indexing term frequencies.
In version 4.0, the format of the inverted index became extensible via the Codec api. Fast per-document storage (
DocValues
) was introduced. Normalization factors need no longer be a single byte, they can be any NumericDocValues. Terms need not be unicode strings, they can be any byte sequence. Term offsets can optionally be indexed into the postings lists. Payloads can be stored in the term vectors.In version 4.1, the format of the postings list changed to use either of FOR compression or variable-byte encoding, depending upon the frequency of the term. Terms appearing only once were changed to inline directly into the term dictionary. Stored fields are compressed by default.
Limitations
Lucene uses a .NET int
to refer to document numbers, and the index file format uses an Int32
on-disk to store document numbers. This is a limitation of both the index file format and the current implementation. Eventually these should be replaced with either UInt64
values, or better yet, VInt values which have no limit.
Classes
Lucene41Codec
Implements the Lucene 4.1 index format, with configurable per-field postings formats.
If you want to reuse functionality of this codec in another codec, extend FilterCodec. See Lucene.Net.Codecs.Lucene41 package documentation for file format details.Note
This API is experimental and might change in incompatible ways in the next release.
Lucene41PostingsBaseFormat
Provides a PostingsReaderBase and PostingsWriterBase.
Note
This API is experimental and might change in incompatible ways in the next release.
Lucene41PostingsFormat
Lucene 4.1 postings format, which encodes postings in packed integer blocks for fast decode.
NOTE: this format is still experimental and subject to change without backwards compatibility.
Basic idea:
- Packed Blocks and VInt Blocks:
In packed blocks, integers are encoded with the same bit width packed format (PackedInt32s): the block size (i.e. number of integers inside block) is fixed (currently 128). Additionally blocks that are all the same value are encoded in an optimized way.
In VInt blocks, integers are encoded as VInt (WriteVInt32(int)): the block size is variable.
- Block structure:
When the postings are long enough, Lucene41PostingsFormat will try to encode most integer data as a packed block.
Take a term with 259 documents as an example, the first 256 document ids are encoded as two packed blocks, while the remaining 3 are encoded as one VInt block.
Different kinds of data are always encoded separately into different packed blocks, but may possibly be interleaved into the same VInt block.
This strategy is applied to pairs: <document number, frequency>, <position, payload length>, <position, offset start, offset length>, and <position, payload length, offsetstart, offset length>.
- Skipdata settings:
The structure of skip table is quite similar to previous version of Lucene. Skip interval is the same as block size, and each skip entry points to the beginning of each block. However, for the first block, skip data is omitted.
- Positions, Payloads, and Offsets:
A position is an integer indicating where the term occurs within one document. A payload is a blob of metadata associated with current position. An offset is a pair of integers indicating the tokenized start/end offsets for given term in current position: it is essentially a specialized payload.
When payloads and offsets are not omitted, numPositions==numPayloads==numOffsets (assuming a null payload contributes one count). As mentioned in block structure, it is possible to encode these three either combined or separately.
In all cases, payloads and offsets are stored together. When encoded as a packed block, position data is separated out as .pos, while payloads and offsets are encoded in .pay (payload metadata will also be stored directly in .pay). When encoded as VInt blocks, all these three are stored interleaved into the .pos (so is payload metadata).
With this strategy, the majority of payload and offset data will be outside .pos file. So for queries that require only position data, running on a full index with payloads and offsets, this reduces disk pre-fetches.
Files and detailed format:
.tim
: Term Dictionary.tip
: Term Index.doc
: Frequencies and Skip Data.pos
: Positions.pay
: Payloads and Offsets
- Term Dictionary
The .tim file contains the list of terms in each field along with per-term statistics (such as docfreq) and pointers to the frequencies, positions, payload and skip data in the .doc, .pos, and .pay files. See BlockTreeTermsWriter<TSubclassState> for more details on the format.
NOTE: The term dictionary can plug into different postings implementations: the postings writer/reader are actually responsible for encoding and decoding the PostingsHeader and TermMetadata sections described here:
- PostingsHeader --> Header, PackedBlockSize
- TermMetadata --> (DocFPDelta|SingletonDocID), PosFPDelta?, PosVIntBlockFPDelta?, PayFPDelta?, SkipFPDelta?
- Header, --> CodecHeader (WriteHeader(DataOutput, string, int))
- PackedBlockSize, SingletonDocID --> VInt (WriteVInt32(int))
- DocFPDelta, PosFPDelta, PayFPDelta, PosVIntBlockFPDelta, SkipFPDelta --> VLong (WriteVInt64(long))
- Footer --> CodecFooter (WriteFooter(IndexOutput))
Notes:
- Header is a CodecHeader (WriteHeader(DataOutput, string, int)) storing the version information for the postings.
- PackedBlockSize is the fixed block size for packed blocks. In packed block, bit width is determined by the largest integer. Smaller block size result in smaller variance among width of integers hence smaller indexes. Larger block size result in more efficient bulk i/o hence better acceleration. This value should always be a multiple of 64, currently fixed as 128 as a tradeoff. It is also the skip interval used to accelerate Advance(int).
- DocFPDelta determines the position of this term's TermFreqs within the .doc file. In particular, it is the difference of file offset between this term's data and previous term's data (or zero, for the first term in the block).On disk it is stored as the difference from previous value in sequence.
- PosFPDelta determines the position of this term's TermPositions within the .pos file. While PayFPDelta determines the position of this term's <TermPayloads, TermOffsets?> within the .pay file. Similar to DocFPDelta, it is the difference between two file positions (or neglected, for fields that omit payloads and offsets).
- PosVIntBlockFPDelta determines the position of this term's last TermPosition in last pos packed block within the .pos file. It is synonym for PayVIntBlockFPDelta or OffsetVIntBlockFPDelta. This is actually used to indicate whether it is necessary to load following payloads and offsets from .pos instead of .pay. Every time a new block of positions are to be loaded, the PostingsReader will use this value to check whether current block is packed format or VInt. When packed format, payloads and offsets are fetched from .pay, otherwise from .pos. (this value is neglected when total number of positions i.e. totalTermFreq is less or equal to PackedBlockSize).
- SkipFPDelta determines the position of this term's SkipData within the .doc file. In particular, it is the length of the TermFreq data. SkipDelta is only stored if DocFreq is not smaller than SkipMinimum (i.e. 128 in Lucene41PostingsFormat).
- SingletonDocID is an optimization when a term only appears in one document. In this case, instead of writing a file pointer to the .doc file (DocFPDelta), and then a VIntBlock at that location, the single document ID is written to the term dictionary.
- Term Index
The .tip file contains an index into the term dictionary, so that it can be accessed randomly. See BlockTreeTermsWriter<TSubclassState> for more details on the format.
- Frequencies and Skip Data
The .doc file contains the lists of documents which contain each term, along with the frequency of the term in that document (except when frequencies are omitted: DOCS_ONLY). It also saves skip data to the beginning of each packed or VInt block, when the length of document list is larger than packed block size.
- docFile(.doc) --> Header, <TermFreqs, SkipData?>TermCount, Footer
- Header --> CodecHeader (WriteHeader(DataOutput, string, int))
- TermFreqs --> <PackedBlock> PackedDocBlockNum, VIntBlock?
- PackedBlock --> PackedDocDeltaBlock, PackedFreqBlock?
- VIntBlock --> <DocDelta[, Freq?]>DocFreq-PackedBlockSize*PackedDocBlockNum
- SkipData --> <<SkipLevelLength, SkipLevel> NumSkipLevels-1, SkipLevel>, SkipDatum?
- SkipLevel --> <SkipDatum> TrimmedDocFreq/(PackedBlockSize^(Level + 1))
- SkipDatum --> DocSkip, DocFPSkip, <PosFPSkip, PosBlockOffset, PayLength?, PayFPSkip?>?, SkipChildLevelPointer?
- PackedDocDeltaBlock, PackedFreqBlock --> PackedInts (PackedInt32s)
- DocDelta, Freq, DocSkip, DocFPSkip, PosFPSkip, PosBlockOffset, PayByteUpto, PayFPSkip --> VInt (WriteVInt32(int))
- SkipChildLevelPointer --> VLong (WriteVInt64(long))
- Footer --> CodecFooter (WriteFooter(IndexOutput))
Notes:
- PackedDocDeltaBlock is theoretically generated from two steps:
- Calculate the difference between each document number and previous one, and get a d-gaps list (for the first document, use absolute value);
- For those d-gaps from first one to PackedDocBlockNum*PackedBlockSizeth, separately encode as packed blocks.
- VIntBlock stores remaining d-gaps (along with frequencies when possible) with a format
that encodes DocDelta and Freq:
DocDelta: if frequencies are indexed, this determines both the document number and the frequency. In particular, DocDelta/2 is the difference between this document number and the previous document number (or zero when this is the first document in a TermFreqs). When DocDelta is odd, the frequency is one. When DocDelta is even, the frequency is read as another VInt. If frequencies are omitted, DocDelta contains the gap (not multiplied by 2) between document numbers and no frequency information is stored.
For example, the TermFreqs for a term which occurs once in document seven and three times in document eleven, with frequencies indexed, would be the following sequence of VInts:
15, 8, 3
If frequencies were omitted (DOCS_ONLY) it would be this sequence of VInts instead:
7,4
- PackedDocBlockNum is the number of packed blocks for current term's docids or frequencies. In particular, PackedDocBlockNum = floor(DocFreq/PackedBlockSize)
- TrimmedDocFreq = DocFreq % PackedBlockSize == 0 ? DocFreq - 1 : DocFreq. We use this trick since the definition of skip entry is a little different from base interface. In MultiLevelSkipListWriter, skip data is assumed to be saved for skipIntervalth, 2*skipIntervalth ... posting in the list. However, in Lucene41PostingsFormat, the skip data is saved for skipInterval+1th, 2*skipInterval+1th ... posting (skipInterval==PackedBlockSize in this case). When DocFreq is multiple of PackedBlockSize, MultiLevelSkipListWriter will expect one more skip data than Lucene41SkipWriter.
- SkipDatum is the metadata of one skip entry. For the first block (no matter packed or VInt), it is omitted.
- DocSkip records the document number of every PackedBlockSizeth document number in the postings (i.e. last document number in each packed block). On disk it is stored as the difference from previous value in the sequence.
- DocFPSkip records the file offsets of each block (excluding )posting at PackedBlockSize+1th, 2*PackedBlockSize+1th ... , in DocFile. The file offsets are relative to the start of current term's TermFreqs. On disk it is also stored as the difference from previous SkipDatum in the sequence.
- Since positions and payloads are also block encoded, the skip should skip to related block first, then fetch the values according to in-block offset. PosFPSkip and PayFPSkip record the file offsets of related block in .pos and .pay, respectively. While PosBlockOffset indicates which value to fetch inside the related block (PayBlockOffset is unnecessary since it is always equal to PosBlockOffset). Same as DocFPSkip, the file offsets are relative to the start of current term's TermFreqs, and stored as a difference sequence.
- PayByteUpto indicates the start offset of the current payload. It is equivalent to the sum of the payload lengths in the current block up to PosBlockOffset
- Positions
The .pos file contains the lists of positions that each term occurs at within documents. It also sometimes stores part of payloads and offsets for speedup.
- PosFile(.pos) --> Header, <TermPositions> TermCount, Footer
- Header --> CodecHeader (WriteHeader(DataOutput, string, int))
- TermPositions --> <PackedPosDeltaBlock> PackedPosBlockNum, VIntBlock?
- VIntBlock --> <PositionDelta[, PayloadLength?], PayloadData?, OffsetDelta?, OffsetLength?>PosVIntCount
- PackedPosDeltaBlock --> PackedInts (PackedInt32s)
- PositionDelta, OffsetDelta, OffsetLength --> VInt (WriteVInt32(int))
- PayloadData --> byte (WriteByte(byte))PayLength
- Footer --> CodecFooter (WriteFooter(IndexOutput))
Notes:
- TermPositions are order by term (terms are implicit, from the term dictionary), and position values for each term document pair are incremental, and ordered by document number.
- PackedPosBlockNum is the number of packed blocks for current term's positions, payloads or offsets. In particular, PackedPosBlockNum = floor(totalTermFreq/PackedBlockSize)
- PosVIntCount is the number of positions encoded as VInt format. In particular, PosVIntCount = totalTermFreq - PackedPosBlockNum*PackedBlockSize
- The procedure how PackedPosDeltaBlock is generated is the same as PackedDocDeltaBlock in chapter Frequencies and Skip Data.
- PositionDelta is, if payloads are disabled for the term's field, the difference between the position of the current occurrence in the document and the previous occurrence (or zero, if this is the first occurrence in this document). If payloads are enabled for the term's field, then PositionDelta/2 is the difference between the current and the previous position. If payloads are enabled and PositionDelta is odd, then PayloadLength is stored, indicating the length of the payload at the current term position.
- For example, the TermPositions for a term which occurs as the fourth term in
one document, and as the fifth and ninth term in a subsequent document, would
be the following sequence of VInts (payloads disabled):
4, 5, 4
- PayloadData is metadata associated with the current term position. If PayloadLength is stored at the current position, then it indicates the length of this payload. If PayloadLength is not stored, then this payload has the same length as the payload at the previous position.
- OffsetDelta/2 is the difference between this position's startOffset from the previous occurrence (or zero, if this is the first occurrence in this document). If OffsetDelta is odd, then the length (endOffset-startOffset) differs from the previous occurrence and an OffsetLength follows. Offset data is only written for DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS.
- Payloads and Offsets
The .pay file will store payloads and offsets associated with certain term-document positions. Some payloads and offsets will be separated out into .pos file, for performance reasons.
- PayFile(.pay): --> Header, <TermPayloads, TermOffsets?> TermCount, Footer
- Header --> CodecHeader (WriteHeader(DataOutput, string, int))
- TermPayloads --> <PackedPayLengthBlock, SumPayLength, PayData> PackedPayBlockNum
- TermOffsets --> <PackedOffsetStartDeltaBlock, PackedOffsetLengthBlock> PackedPayBlockNum
- PackedPayLengthBlock, PackedOffsetStartDeltaBlock, PackedOffsetLengthBlock --> PackedInts (PackedInt32s)
- SumPayLength --> VInt (WriteVInt32(int))
- PayData --> byte (WriteByte(byte)) SumPayLength
- Footer --> CodecFooter (WriteFooter(IndexOutput))
Notes:
- The order of TermPayloads/TermOffsets will be the same as TermPositions, note that part of payload/offsets are stored in .pos.
- The procedure how PackedPayLengthBlock and PackedOffsetLengthBlock are generated is the same as PackedFreqBlock in chapter Frequencies and Skip Data. While PackedStartDeltaBlock follows a same procedure as PackedDocDeltaBlock.
- PackedPayBlockNum is always equal to PackedPosBlockNum, for the same term. It is also synonym for PackedOffsetBlockNum.
- SumPayLength is the total length of payloads written within one block, should be the sum of PayLengths in one packed block.
- PayLength in PackedPayLengthBlock is the length of each payload associated with the current position.
Note
This API is experimental and might change in incompatible ways in the next release.
Lucene41PostingsReader
Concrete class that reads docId(maybe frq,pos,offset,payloads) list with postings format.
Note
This API is experimental and might change in incompatible ways in the next release.
Lucene41PostingsWriter
Concrete class that writes docId(maybe frq,pos,offset,payloads) list with postings format.
Postings list for each term will be stored separately.Note
This API is experimental and might change in incompatible ways in the next release.
Lucene41PostingsWriter.Int32BlockTermState
NOTE: This was IntBlockTermState in Lucene
Lucene41StoredFieldsFormat
Lucene 4.1 stored fields format.
Principle
This StoredFieldsFormat compresses blocks of 16KB of documents in order to improve the compression ratio compared to document-level compression. It uses the LZ4 compression algorithm, which is fast to compress and very fast to decompress data. Although the compression method that is used focuses more on speed than on compression ratio, it should provide interesting compression ratios for redundant inputs (such as log files, HTML or plain text).
File formats
Stored fields are represented by two files:
A fields data file (extension
.fdt
). this file stores a compact representation of documents in compressed blocks of 16KB or more. When writing a segment, documents are appended to an in-memorybyte[]
buffer. When its size reaches 16KB or more, some metadata about the documents is flushed to disk, immediately followed by a compressed representation of the buffer using the LZ4compression format.Here is a more detailed description of the field data file format:
- FieldData (.fdt) --> <Header>, PackedIntsVersion, <Chunk>ChunkCount
- Header --> CodecHeader (WriteHeader(DataOutput, string, int))
- PackedIntsVersion --> VERSION_CURRENT as a VInt (WriteVInt32(int))
- ChunkCount is not known in advance and is the number of chunks necessary to store all document of the segment
- Chunk --> DocBase, ChunkDocs, DocFieldCounts, DocLengths, <CompressedDocs>
- DocBase --> the ID of the first document of the chunk as a VInt (WriteVInt32(int))
- ChunkDocs --> the number of documents in the chunk as a VInt (WriteVInt32(int))
- DocFieldCounts --> the number of stored fields of every document in the chunk, encoded as followed:
- if chunkDocs=1, the unique value is encoded as a VInt (WriteVInt32(int))
- else read a VInt (WriteVInt32(int)) (let's call it
bitsRequired
)- if
bitsRequired
is0
then all values are equal, and the common value is the following VInt (WriteVInt32(int)) - else
bitsRequired
is the number of bits required to store any value, and values are stored in a packed (PackedInt32s) array where every value is stored on exactlybitsRequired
bits
- if
- DocLengths --> the lengths of all documents in the chunk, encoded with the same method as DocFieldCounts
- CompressedDocs --> a compressed representation of <Docs> using the LZ4 compression format
- Docs --> <Doc>ChunkDocs
- Doc --> <FieldNumAndType, Value>DocFieldCount
- FieldNumAndType --> a VLong (WriteVInt64(long)), whose 3 last bits are Type and other bits are FieldNum
- Type -->
- 0: Value is String
- 1: Value is BinaryValue
- 2: Value is Int
- 3: Value is Float
- 4: Value is Long
- 5: Value is Double
- 6, 7: unused
- FieldNum --> an ID of the field
- Value --> String (WriteString(string)) | BinaryValue | Int | Float | Long | Double depending on Type
- BinaryValue --> ValueLength <Byte>ValueLength
Notes
- If documents are larger than 16KB then chunks will likely contain only one document. However, documents can never spread across several chunks (all fields of a single document are in the same chunk).
- When at least one document in a chunk is large enough so that the chunk is larger than 32KB, the chunk will actually be compressed in several LZ4 blocks of 16KB. this allows StoredFieldVisitors which are only interested in the first fields of a document to not have to decompress 10MB of data if the document is 10MB, but only 16KB.
- Given that the original lengths are written in the metadata of the chunk, the decompressor can leverage this information to stop decoding as soon as enough data has been decompressed.
- In case documents are incompressible, CompressedDocs will be less than 0.5% larger than Docs.
A fields index file (extension
.fdx
).- FieldsIndex (.fdx) --> <Header>, <ChunkIndex>
- Header --> CodecHeader (WriteHeader(DataOutput, string, int))
- ChunkIndex: See CompressingStoredFieldsIndexWriter
Known limitations
This StoredFieldsFormat does not support individual documents
larger than (231 - 214
) bytes. In case this
is a problem, you should use another format, such as
Lucene40StoredFieldsFormat.
Note
This API is experimental and might change in incompatible ways in the next release.