Namespace Lucene.Net.Codecs.Lucene45
Lucene 4.5 file format.
Apache Lucene - Index File Formats
Introduction
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. 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 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
File Naming
Summary of File Extensions
Name | Extension | Brief Description |
---|---|---|
Segments File | segments.gen, segments_N | Stores information about a commit point |
Lock File | write.lock | The Write lock prevents multiple IndexWriters from writing to the same file. |
Segment Info | .si | Stores metadata about a segment |
Compound File | .cfs, .cfe | An optional "virtual" file consisting of all the other index files for systems that frequently run out of file handles. |
Fields | .fnm | Stores information about the fields |
Field Index | .fdx | Contains pointers to field data |
Field Data | .fdt | The stored fields for documents |
Term Dictionary | .tim | The term dictionary, stores term info |
Term Index | .tip | The index into the Term Dictionary |
Frequencies | .doc | Contains the list of docs which contain each term along with frequency |
Positions | .pos | Stores position information about where a term occurs in the index |
Payloads | .pay | Stores additional per-position metadata information such as character offsets and user payloads |
Norms | .nvd, .nvm | Encodes length and boost factors for docs and fields |
Per-Document Values | .dvd, .dvm | Encodes additional scoring factors or other per-document information. |
Term Vector Index | .tvx | Stores offset into the document data file |
Term Vector Documents | .tvd | Contains information about each document that has term vectors |
Term Vector Fields | .tvf | The field level info about term vectors |
Deleted Documents | .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 Map<String,String> CommitUserData may be passed to IndexWriter's commit methods (and later retrieved), which is recorded in the segments_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 ({@code 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.
In version 4.2, term vectors are compressed by default. DocValues has a new multi-valued type (SortedSet), that can be used for faceting/grouping/joining on multi-valued fields.
In version 4.5, DocValues were extended to explicitly represent missing values.
Limitations
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
Lucene45Codec
Implements the Lucene 4.5 index format, with configurable per-field postings and docvalues formats.
If you want to reuse functionality of this codec in another codec, extend FilterCodec.
See Lucene.Net.Codecs.Lucene45 package documentation for file format details.
@lucene.experimental
Lucene45DocValuesConsumer
Writer for Lucene45DocValuesFormat
Lucene45DocValuesFormat
Lucene 4.5 DocValues format.
Encodes the four per-document value types (Numeric,Binary,Sorted,SortedSet) with these strategies:
- Delta-compressed: per-document integers written in blocks of 16k. For each block the minimum value in that block is encoded, and each entry is a delta from that minimum value. Each block of deltas is compressed with bitpacking. For more information, see BlockPackedWriter.
- Table-compressed: when the number of unique values is very small (< 256), and when there are unused "gaps" in the range of values used (such as SmallSingle), a lookup table is written instead. Each per-document entry is instead the ordinal to this table, and those ordinals are compressed with bitpacking (PackedInt32s).
- GCD-compressed: when all numbers share a common divisor, such as dates, the greatest common denominator (GCD) is computed, and quotients are stored using Delta-compressed Numerics.
- Fixed-width Binary: one large concatenated byte[] is written, along with the fixed length.
Each document's value can be addressed directly with multiplication (
docID * length
). - Variable-width Binary: one large concatenated byte[] is written, along with end addresses for each document. The addresses are written in blocks of 16k, with the current absolute start for the block, and the average (expected) delta per entry. For each document the deviation from the delta (actual - expected) is written.
- Prefix-compressed Binary: values are written in chunks of 16, with the first value written completely and other values sharing prefixes. Chunk addresses are written in blocks of 16k, with the current absolute start for the block, and the average (expected) delta per entry. For each chunk the deviation from the delta (actual - expected) is written.
- Sorted: a mapping of ordinals to deduplicated terms is written as Prefix-Compressed Binary, along with the per-document ordinals written using one of the numeric strategies above.
- SortedSet: a mapping of ordinals to deduplicated terms is written as Prefix-Compressed Binary, an ordinal list and per-document index into this list are written using the numeric strategies above.
Files:
.dvd
: DocValues data.dvm
: DocValues metadata
-
The DocValues metadata or .dvm file.
For DocValues field, this stores metadata, such as the offset into the DocValues data (.dvd)
DocValues metadata (.dvm) --> Header,<Entry>NumFields,Footer
- Entry --> NumericEntry | BinaryEntry | SortedEntry | SortedSetEntry
- NumericEntry --> GCDNumericEntry | TableNumericEntry | DeltaNumericEntry
- GCDNumericEntry --> NumericHeader,MinValue,GCD
- TableNumericEntry --> NumericHeader,TableSize,Int64 (WriteInt64(Int64)) TableSize
- DeltaNumericEntry --> NumericHeader
- NumericHeader --> FieldNumber,EntryType,NumericType,MissingOffset,PackedVersion,DataOffset,Count,BlockSize
- BinaryEntry --> FixedBinaryEntry | VariableBinaryEntry | PrefixBinaryEntry
- FixedBinaryEntry --> BinaryHeader
- VariableBinaryEntry --> BinaryHeader,AddressOffset,PackedVersion,BlockSize
- PrefixBinaryEntry --> BinaryHeader,AddressInterval,AddressOffset,PackedVersion,BlockSize
- BinaryHeader --> FieldNumber,EntryType,BinaryType,MissingOffset,MinLength,MaxLength,DataOffset
- SortedEntry --> FieldNumber,EntryType,BinaryEntry,NumericEntry
- SortedSetEntry --> EntryType,BinaryEntry,NumericEntry,NumericEntry
- FieldNumber,PackedVersion,MinLength,MaxLength,BlockSize,ValueCount --> VInt (WriteVInt32(Int32)
- EntryType,CompressionType --> Byte (WriteByte(Byte)
- Header --> CodecHeader (WriteHeader(DataOutput, String, Int32))
- MinValue,GCD,MissingOffset,AddressOffset,DataOffset --> Int64 (WriteInt64(Int64))
- TableSize --> vInt (WriteVInt32(Int32))
- Footer --> CodecFooter (WriteFooter(IndexOutput))
Sorted fields have two entries: a Lucene45DocValuesProducer.BinaryEntry with the value metadata, and an ordinary Lucene45DocValuesProducer.NumericEntry for the document-to-ord metadata.
SortedSet fields have three entries: a Lucene45DocValuesProducer.BinaryEntry with the value metadata, and two Lucene45DocValuesProducer.NumericEntrys for the document-to-ord-index and ordinal list metadata.
FieldNumber of -1 indicates the end of metadata.
EntryType is a 0 (Lucene45DocValuesProducer.NumericEntry) or 1 (Lucene45DocValuesProducer.BinaryEntry)
DataOffset is the pointer to the start of the data in the DocValues data (.dvd)
NumericType indicates how Numeric values will be compressed:
- 0 --> delta-compressed. For each block of 16k integers, every integer is delta-encoded from the minimum value within the block.
- 1 --> gcd-compressed. When all integers share a common divisor, only quotients are stored using blocks of delta-encoded ints.
- 2 --> table-compressed. When the number of unique numeric values is small and it would save space, a lookup table of unique values is written, followed by the ordinal for each document.
BinaryType indicates how Binary values will be stored:
- 0 --> fixed-width. All values have the same length, addressing by multiplication.
- 1 --> variable-width. An address for each value is stored.
- 2 --> prefix-compressed. An address to the start of every interval'th value is stored.
MinLength and MaxLength represent the min and max byte[] value lengths for Binary values. If they are equal, then all values are of a fixed size, and can be addressed as DataOffset + (docID * length). Otherwise, the binary values are of variable size, and packed integer metadata (PackedVersion,BlockSize) is written for the addresses.
MissingOffset points to a byte[] containing a bitset of all documents that had a value for the field. If its -1, then there are no missing values.
Checksum contains the CRC32 checksum of all bytes in the .dvm file up until the checksum. this is used to verify integrity of the file on opening the index.
-
The DocValues data or .dvd file.
For DocValues field, this stores the actual per-document data (the heavy-lifting)
DocValues data (.dvd) --> Header,<NumericData | BinaryData | SortedData>NumFields,Footer
- NumericData --> DeltaCompressedNumerics | TableCompressedNumerics | GCDCompressedNumerics
- BinaryData --> Byte (WriteByte(Byte)) DataLength,Addresses
- SortedData --> FST<Int64> (FST<T>)
- DeltaCompressedNumerics --> BlockPackedInts(blockSize=16k) (BlockPackedWriter)
- TableCompressedNumerics --> PackedInts (PackedInt32s)
- GCDCompressedNumerics --> BlockPackedInts(blockSize=16k) (BlockPackedWriter)
- Addresses --> MonotonicBlockPackedInts(blockSize=16k) (MonotonicBlockPackedWriter)
- Footer --> CodecFooter (WriteFooter(IndexOutput))
SortedSet entries store the list of ordinals in their BinaryData as a sequences of increasing vLongs (WriteVInt64(Int64)), delta-encoded.
Lucene45DocValuesProducer
Reader for Lucene45DocValuesFormat.
Lucene45DocValuesProducer.BinaryEntry
Metadata entry for a binary docvalues field.
Lucene45DocValuesProducer.NumericEntry
Metadata entry for a numeric docvalues field.
Lucene45DocValuesProducer.SortedSetEntry
Metadata entry for a sorted-set docvalues field.