|
| 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.vectors; |
| 11 | + |
| 12 | +import org.apache.lucene.codecs.Codec; |
| 13 | +import org.apache.lucene.codecs.KnnVectorsFormat; |
| 14 | +import org.apache.lucene.codecs.KnnVectorsReader; |
| 15 | +import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat; |
| 16 | +import org.apache.lucene.document.Document; |
| 17 | +import org.apache.lucene.document.KnnFloatVectorField; |
| 18 | +import org.apache.lucene.index.CodecReader; |
| 19 | +import org.apache.lucene.index.DirectoryReader; |
| 20 | +import org.apache.lucene.index.FloatVectorValues; |
| 21 | +import org.apache.lucene.index.IndexReader; |
| 22 | +import org.apache.lucene.index.IndexWriter; |
| 23 | +import org.apache.lucene.index.IndexWriterConfig; |
| 24 | +import org.apache.lucene.index.KnnVectorValues; |
| 25 | +import org.apache.lucene.index.LeafReader; |
| 26 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 27 | +import org.apache.lucene.search.AcceptDocs; |
| 28 | +import org.apache.lucene.search.TopDocs; |
| 29 | +import org.apache.lucene.store.Directory; |
| 30 | +import org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase; |
| 31 | +import org.apache.lucene.tests.util.TestUtil; |
| 32 | +import org.apache.lucene.util.SameThreadExecutorService; |
| 33 | +import org.apache.lucene.util.VectorUtil; |
| 34 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 35 | +import org.hamcrest.Matcher; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.concurrent.ExecutorService; |
| 40 | + |
| 41 | +import static org.apache.lucene.index.VectorSimilarityFunction.DOT_PRODUCT; |
| 42 | +import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS; |
| 43 | +import static org.hamcrest.Matchers.equalTo; |
| 44 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 45 | + |
| 46 | +public abstract class BaseHnswVectorsFormatTestCase extends BaseKnnVectorsFormatTestCase { |
| 47 | + |
| 48 | + static { |
| 49 | + LogConfigurator.loadLog4jPlugins(); |
| 50 | + LogConfigurator.configureESLogging(); // native access requires logging to be initialized |
| 51 | + } |
| 52 | + |
| 53 | + protected abstract KnnVectorsFormat createFormat(); |
| 54 | + |
| 55 | + protected abstract KnnVectorsFormat createFormat(int maxConn, int beamWidth); |
| 56 | + |
| 57 | + protected abstract KnnVectorsFormat createFormat(int maxConn, int beamWidth, int numMergeWorkers, ExecutorService service); |
| 58 | + |
| 59 | + private KnnVectorsFormat format; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setUp() throws Exception { |
| 63 | + format = createFormat(); |
| 64 | + super.setUp(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected Codec getCodec() { |
| 69 | + return TestUtil.alwaysKnnVectorsFormat(format); |
| 70 | + } |
| 71 | + |
| 72 | + public void testLimits() { |
| 73 | + expectThrows(IllegalArgumentException.class, () -> createFormat(-1, 20)); |
| 74 | + expectThrows(IllegalArgumentException.class, () -> createFormat(0, 20)); |
| 75 | + expectThrows(IllegalArgumentException.class, () -> createFormat(20, 0)); |
| 76 | + expectThrows(IllegalArgumentException.class, () -> createFormat(20, -1)); |
| 77 | + expectThrows(IllegalArgumentException.class, () -> createFormat(512 + 1, 20)); |
| 78 | + expectThrows(IllegalArgumentException.class, () -> createFormat(20, 3201)); |
| 79 | + expectThrows(IllegalArgumentException.class, () -> createFormat(20, 100, 1, new SameThreadExecutorService())); |
| 80 | + } |
| 81 | + |
| 82 | + public void testSingleVectorCase() throws Exception { |
| 83 | + float[] vector = randomVector(random().nextInt(12, 500)); |
| 84 | + for (VectorSimilarityFunction similarityFunction : VectorSimilarityFunction.values()) { |
| 85 | + try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) { |
| 86 | + Document doc = new Document(); |
| 87 | + if (similarityFunction == VectorSimilarityFunction.COSINE) { |
| 88 | + VectorUtil.l2normalize(vector); |
| 89 | + } |
| 90 | + doc.add(new KnnFloatVectorField("f", vector, similarityFunction)); |
| 91 | + w.addDocument(doc); |
| 92 | + w.commit(); |
| 93 | + try (IndexReader reader = DirectoryReader.open(w)) { |
| 94 | + LeafReader r = getOnlyLeafReader(reader); |
| 95 | + FloatVectorValues vectorValues = r.getFloatVectorValues("f"); |
| 96 | + KnnVectorValues.DocIndexIterator docIndexIterator = vectorValues.iterator(); |
| 97 | + assertThat(vectorValues.size(), equalTo(1)); |
| 98 | + while (docIndexIterator.nextDoc() != NO_MORE_DOCS) { |
| 99 | + assertArrayEquals(vector, vectorValues.vectorValue(docIndexIterator.index()), 0.00001f); |
| 100 | + } |
| 101 | + float[] randomVector = randomVector(vector.length); |
| 102 | + if (similarityFunction == VectorSimilarityFunction.COSINE) { |
| 103 | + VectorUtil.l2normalize(randomVector); |
| 104 | + } |
| 105 | + float trueScore = similarityFunction.compare(vector, randomVector); |
| 106 | + TopDocs td = r.searchNearestVectors( |
| 107 | + "f", |
| 108 | + randomVector, |
| 109 | + 1, |
| 110 | + AcceptDocs.fromLiveDocs(r.getLiveDocs(), r.maxDoc()), |
| 111 | + Integer.MAX_VALUE |
| 112 | + ); |
| 113 | + assertEquals(1, td.totalHits.value()); |
| 114 | + assertThat(td.scoreDocs[0].score, greaterThanOrEqualTo(0f)); |
| 115 | + // When it's the only vector in a segment, the score should be very close to the true score |
| 116 | + assertEquals(trueScore, td.scoreDocs[0].score, 0.01f); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + protected static void testSimpleOffHeapSize( |
| 123 | + Directory dir, |
| 124 | + IndexWriterConfig config, |
| 125 | + float[] vector, |
| 126 | + Matcher<? super Map<String, Long>> matchesMap |
| 127 | + ) throws IOException { |
| 128 | + try (IndexWriter w = new IndexWriter(dir, config)) { |
| 129 | + Document doc = new Document(); |
| 130 | + doc.add(new KnnFloatVectorField("f", vector, DOT_PRODUCT)); |
| 131 | + w.addDocument(doc); |
| 132 | + w.commit(); |
| 133 | + try (IndexReader reader = DirectoryReader.open(w)) { |
| 134 | + LeafReader r = getOnlyLeafReader(reader); |
| 135 | + if (r instanceof CodecReader codecReader) { |
| 136 | + KnnVectorsReader knnVectorsReader = codecReader.getVectorReader(); |
| 137 | + if (knnVectorsReader instanceof PerFieldKnnVectorsFormat.FieldsReader fieldsReader) { |
| 138 | + knnVectorsReader = fieldsReader.getFieldReader("f"); |
| 139 | + } |
| 140 | + var fieldInfo = r.getFieldInfos().fieldInfo("f"); |
| 141 | + var offHeap = knnVectorsReader.getOffHeapByteSize(fieldInfo); |
| 142 | + assertThat(offHeap, matchesMap); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments