|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.codec; |
| 11 | + |
| 12 | +import org.apache.lucene.codecs.DocValuesFormat; |
| 13 | +import org.apache.lucene.codecs.KnnVectorsFormat; |
| 14 | +import org.apache.lucene.codecs.PostingsFormat; |
| 15 | +import org.apache.lucene.codecs.StoredFieldsFormat; |
| 16 | +import org.apache.lucene.codecs.lucene101.Lucene101Codec; |
| 17 | +import org.apache.lucene.codecs.lucene101.Lucene101PostingsFormat; |
| 18 | +import org.apache.lucene.codecs.lucene90.Lucene90DocValuesFormat; |
| 19 | +import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat; |
| 20 | +import org.apache.lucene.codecs.perfield.PerFieldPostingsFormat; |
| 21 | +import org.elasticsearch.index.codec.perfield.XPerFieldDocValuesFormat; |
| 22 | +import org.elasticsearch.index.codec.vectors.es910.ES819HnswReducedHeapVectorsFormat; |
| 23 | +import org.elasticsearch.index.codec.zstd.Zstd814StoredFieldsFormat; |
| 24 | + |
| 25 | +/** |
| 26 | + * Elasticsearch codec as of 9.0 relying on Lucene 10.1. This extends the Lucene 10.1 codec to compressed |
| 27 | + * stored fields with ZSTD instead of LZ4/DEFLATE. See {@link Zstd814StoredFieldsFormat}. |
| 28 | + */ |
| 29 | +public class Elasticsearch910Lucene102Codec extends CodecService.DeduplicateFieldInfosCodec { |
| 30 | + |
| 31 | + static final PostingsFormat DEFAULT_POSTINGS_FORMAT = new Lucene101PostingsFormat(); |
| 32 | + |
| 33 | + private final StoredFieldsFormat storedFieldsFormat; |
| 34 | + |
| 35 | + private final PostingsFormat defaultPostingsFormat; |
| 36 | + private final PostingsFormat postingsFormat = new PerFieldPostingsFormat() { |
| 37 | + @Override |
| 38 | + public PostingsFormat getPostingsFormatForField(String field) { |
| 39 | + return Elasticsearch910Lucene102Codec.this.getPostingsFormatForField(field); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + private final DocValuesFormat defaultDVFormat; |
| 44 | + private final DocValuesFormat docValuesFormat = new XPerFieldDocValuesFormat() { |
| 45 | + @Override |
| 46 | + public DocValuesFormat getDocValuesFormatForField(String field) { |
| 47 | + return Elasticsearch910Lucene102Codec.this.getDocValuesFormatForField(field); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + private final KnnVectorsFormat defaultKnnVectorsFormat; |
| 52 | + private final KnnVectorsFormat knnVectorsFormat = new PerFieldKnnVectorsFormat() { |
| 53 | + @Override |
| 54 | + public KnnVectorsFormat getKnnVectorsFormatForField(String field) { |
| 55 | + return Elasticsearch910Lucene102Codec.this.getKnnVectorsFormatForField(field); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + /** Public no-arg constructor, needed for SPI loading at read-time. */ |
| 60 | + public Elasticsearch910Lucene102Codec() { |
| 61 | + this(Zstd814StoredFieldsFormat.Mode.BEST_SPEED); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Constructor. Takes a {@link Zstd814StoredFieldsFormat.Mode} that describes whether to optimize for retrieval speed at the expense of |
| 66 | + * worse space-efficiency or vice-versa. |
| 67 | + */ |
| 68 | + public Elasticsearch910Lucene102Codec(Zstd814StoredFieldsFormat.Mode mode) { |
| 69 | + super("Elasticsearch900Lucene101", new Lucene101Codec()); |
| 70 | + this.storedFieldsFormat = mode.getFormat(); |
| 71 | + this.defaultPostingsFormat = DEFAULT_POSTINGS_FORMAT; |
| 72 | + this.defaultDVFormat = new Lucene90DocValuesFormat(); |
| 73 | + this.defaultKnnVectorsFormat = new ES819HnswReducedHeapVectorsFormat(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public StoredFieldsFormat storedFieldsFormat() { |
| 78 | + return storedFieldsFormat; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public final PostingsFormat postingsFormat() { |
| 83 | + return postingsFormat; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public final DocValuesFormat docValuesFormat() { |
| 88 | + return docValuesFormat; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public final KnnVectorsFormat knnVectorsFormat() { |
| 93 | + return knnVectorsFormat; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns the postings format that should be used for writing new segments of <code>field</code>. |
| 98 | + * |
| 99 | + * <p>The default implementation always returns "Lucene912". |
| 100 | + * |
| 101 | + * <p><b>WARNING:</b> if you subclass, you are responsible for index backwards compatibility: |
| 102 | + * future version of Lucene are only guaranteed to be able to read the default implementation, |
| 103 | + */ |
| 104 | + public PostingsFormat getPostingsFormatForField(String field) { |
| 105 | + return defaultPostingsFormat; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the docvalues format that should be used for writing new segments of <code>field</code> |
| 110 | + * . |
| 111 | + * |
| 112 | + * <p>The default implementation always returns "Lucene912". |
| 113 | + * |
| 114 | + * <p><b>WARNING:</b> if you subclass, you are responsible for index backwards compatibility: |
| 115 | + * future version of Lucene are only guaranteed to be able to read the default implementation. |
| 116 | + */ |
| 117 | + public DocValuesFormat getDocValuesFormatForField(String field) { |
| 118 | + return defaultDVFormat; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Returns the vectors format that should be used for writing new segments of <code>field</code> |
| 123 | + * |
| 124 | + * <p>The default implementation always returns "Lucene912". |
| 125 | + * |
| 126 | + * <p><b>WARNING:</b> if you subclass, you are responsible for index backwards compatibility: |
| 127 | + * future version of Lucene are only guaranteed to be able to read the default implementation. |
| 128 | + */ |
| 129 | + public KnnVectorsFormat getKnnVectorsFormatForField(String field) { |
| 130 | + return defaultKnnVectorsFormat; |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments