|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.lucene.sandbox.codecs.faiss; |
| 18 | + |
| 19 | +import static org.apache.lucene.util.hnsw.HnswGraphBuilder.DEFAULT_BEAM_WIDTH; |
| 20 | +import static org.apache.lucene.util.hnsw.HnswGraphBuilder.DEFAULT_MAX_CONN; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Locale; |
| 24 | +import org.apache.lucene.codecs.KnnVectorsFormat; |
| 25 | +import org.apache.lucene.codecs.KnnVectorsReader; |
| 26 | +import org.apache.lucene.codecs.KnnVectorsWriter; |
| 27 | +import org.apache.lucene.codecs.hnsw.FlatVectorScorerUtil; |
| 28 | +import org.apache.lucene.codecs.hnsw.FlatVectorsFormat; |
| 29 | +import org.apache.lucene.codecs.lucene99.Lucene99FlatVectorsFormat; |
| 30 | +import org.apache.lucene.index.SegmentReadState; |
| 31 | +import org.apache.lucene.index.SegmentWriteState; |
| 32 | + |
| 33 | +/** |
| 34 | + * A Faiss-based format to create and search vector indexes, using {@link LibFaissC} to interact |
| 35 | + * with the native library. |
| 36 | + * |
| 37 | + * <p>The Faiss index is configured using its flexible <a |
| 38 | + * href="https://github.com/facebookresearch/faiss/wiki/The-index-factory">index factory</a>, which |
| 39 | + * allows creating arbitrary indexes by "describing" them. These indexes can be tuned by <a |
| 40 | + * href="https://github.com/facebookresearch/faiss/wiki/Index-IO,-cloning-and-hyper-parameter-tuning">setting |
| 41 | + * relevant parameters</a>. |
| 42 | + * |
| 43 | + * <p>A separate Faiss index is created per-segment, and uses the following files: |
| 44 | + * |
| 45 | + * <ul> |
| 46 | + * <li><code>.faissm</code> (metadata file): stores field number, offset and length of actual |
| 47 | + * Faiss index in data file. |
| 48 | + * <li><code>.faissd</code> (data file): stores concatenated Faiss indexes for all fields. |
| 49 | + * <li>All files required by {@link Lucene99FlatVectorsFormat} for storing raw vectors. |
| 50 | + * </ul> |
| 51 | + * |
| 52 | + * <p>Note: Set the {@code $OMP_NUM_THREADS} environment variable to control <a |
| 53 | + * href="https://github.com/facebookresearch/faiss/wiki/Threads-and-asynchronous-calls">internal |
| 54 | + * threading</a>. |
| 55 | + * |
| 56 | + * <p>TODO: There is no guarantee of backwards compatibility! |
| 57 | + * |
| 58 | + * @lucene.experimental |
| 59 | + */ |
| 60 | +public final class FaissKnnVectorsFormat extends KnnVectorsFormat { |
| 61 | + public static final String NAME = FaissKnnVectorsFormat.class.getSimpleName(); |
| 62 | + static final int VERSION_START = 0; |
| 63 | + static final int VERSION_CURRENT = VERSION_START; |
| 64 | + static final String META_CODEC_NAME = NAME + "Meta"; |
| 65 | + static final String DATA_CODEC_NAME = NAME + "Data"; |
| 66 | + static final String META_EXTENSION = "faissm"; |
| 67 | + static final String DATA_EXTENSION = "faissd"; |
| 68 | + |
| 69 | + private final String description; |
| 70 | + private final String indexParams; |
| 71 | + private final FlatVectorsFormat rawVectorsFormat; |
| 72 | + |
| 73 | + /** |
| 74 | + * Constructs an HNSW-based format using default {@code maxConn}={@value |
| 75 | + * org.apache.lucene.util.hnsw.HnswGraphBuilder#DEFAULT_MAX_CONN} and {@code beamWidth}={@value |
| 76 | + * org.apache.lucene.util.hnsw.HnswGraphBuilder#DEFAULT_BEAM_WIDTH}. |
| 77 | + */ |
| 78 | + public FaissKnnVectorsFormat() { |
| 79 | + this( |
| 80 | + String.format(Locale.ROOT, "IDMap,HNSW%d", DEFAULT_MAX_CONN), |
| 81 | + String.format(Locale.ROOT, "efConstruction=%d", DEFAULT_BEAM_WIDTH)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Constructs a format using the specified index factory string and index parameters (see class |
| 86 | + * docs for more information). |
| 87 | + * |
| 88 | + * @param description the index factory string to initialize Faiss indexes. |
| 89 | + * @param indexParams the index params to set on Faiss indexes. |
| 90 | + */ |
| 91 | + public FaissKnnVectorsFormat(String description, String indexParams) { |
| 92 | + super(NAME); |
| 93 | + this.description = description; |
| 94 | + this.indexParams = indexParams; |
| 95 | + this.rawVectorsFormat = |
| 96 | + new Lucene99FlatVectorsFormat(FlatVectorScorerUtil.getLucene99FlatVectorsScorer()); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException { |
| 101 | + return new FaissKnnVectorsWriter( |
| 102 | + description, indexParams, state, rawVectorsFormat.fieldsWriter(state)); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException { |
| 107 | + return new FaissKnnVectorsReader(state, rawVectorsFormat.fieldsReader(state)); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int getMaxDimensions(String fieldName) { |
| 112 | + return DEFAULT_MAX_DIMENSIONS; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String toString() { |
| 117 | + return String.format( |
| 118 | + Locale.ROOT, "%s(description=%s indexParams=%s)", NAME, description, indexParams); |
| 119 | + } |
| 120 | +} |
0 commit comments