Skip to content

Commit 342d769

Browse files
committed
Cleanup & tests
1 parent 67b7623 commit 342d769

File tree

5 files changed

+201
-98
lines changed

5 files changed

+201
-98
lines changed

server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
import java.util.Map;
8989
import java.util.Objects;
9090
import java.util.Optional;
91-
import java.util.Set;
9291
import java.util.function.Function;
9392
import java.util.function.Supplier;
9493
import java.util.stream.Stream;
@@ -128,18 +127,6 @@ public static boolean isNotUnitVector(float magnitude) {
128127
public static final int MAGNITUDE_BYTES = 4;
129128
public static final int NUM_CANDS_OVERSAMPLE_LIMIT = 10_000; // Max oversample allowed for k and num_candidates
130129

131-
// Supported types for index options for dense_vector field and semantic_text fields based on dense inference IDs
132-
public static final Set<String> SUPPORTED_TYPES = Set.of(
133-
"hnsw",
134-
"int8_hnsw",
135-
"int4_hnsw",
136-
"bbq_hnsw",
137-
"flat",
138-
"int8_flat",
139-
"int4_flat",
140-
"bbq_flat"
141-
);
142-
143130
private static DenseVectorFieldMapper toType(FieldMapper in) {
144131
return (DenseVectorFieldMapper) in;
145132
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextField.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ public String type() {
222222
return type;
223223
}
224224

225+
@Override
226+
public String toString() {
227+
return asMap().toString();
228+
}
229+
225230
public abstract Map<String, Object> asMap();
226231
}
227232

@@ -238,6 +243,18 @@ public DenseVectorIndexOptions(String type, Integer m, Integer efConstruction, I
238243
this.confidenceInterval = confidenceInterval;
239244
}
240245

246+
public Integer m() {
247+
return m;
248+
}
249+
250+
public Integer efConstruction() {
251+
return efConstruction;
252+
}
253+
254+
public Integer confidenceInterval() {
255+
return confidenceInterval;
256+
}
257+
241258
public Map<String, Object> asMap() {
242259
Map<String, Object> map = new HashMap<>();
243260
if (type != null) {
@@ -254,6 +271,22 @@ public Map<String, Object> asMap() {
254271
}
255272
return map;
256273
}
274+
275+
@Override
276+
public boolean equals(Object o) {
277+
if (this == o) return true;
278+
if (o == null || getClass() != o.getClass()) return false;
279+
DenseVectorIndexOptions that = (DenseVectorIndexOptions) o;
280+
return Objects.equals(type, that.type)
281+
&& Objects.equals(m, that.m)
282+
&& Objects.equals(efConstruction, that.efConstruction)
283+
&& Objects.equals(confidenceInterval, that.confidenceInterval);
284+
}
285+
286+
@Override
287+
public int hashCode() {
288+
return Objects.hash(type, m, efConstruction, confidenceInterval);
289+
}
257290
}
258291

259292
public static String getOriginalTextFieldName(String fieldName) {
@@ -314,7 +347,7 @@ static IndexOptions parseIndexOptionsFromMap(String fieldName, Object node) {
314347
);
315348
IndexOptions indexOptions = INDEX_OPTIONS_PARSER.parse(parser, null);
316349

317-
if (indexOptions.type != null && DenseVectorFieldMapper.SUPPORTED_TYPES.contains(indexOptions.type)) {
350+
if (indexOptions.type != null && isDenseVectorIndexType(indexOptions.type)) {
318351
// Run dense vector index options through the dense vector field mapper validation
319352
// so we error on invalid options at index creation time
320353
DenseVectorFieldMapper.parseIndexOptions(fieldName, node);
@@ -326,6 +359,10 @@ static IndexOptions parseIndexOptionsFromMap(String fieldName, Object node) {
326359
}
327360
}
328361

362+
private static boolean isDenseVectorIndexType(String type) {
363+
return DenseVectorFieldMapper.VectorIndexType.fromString(type).isPresent();
364+
}
365+
329366
@Override
330367
public List<String> originalValues() {
331368
return originalValues != null ? originalValues : Collections.emptyList();
@@ -437,7 +474,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
437474
true,
438475
args -> {
439476
String type = (String) args[0];
440-
if (type != null && DenseVectorFieldMapper.SUPPORTED_TYPES.contains(type)) {
477+
if (type != null && isDenseVectorIndexType(type)) {
441478
Integer m = (Integer) args[1];
442479
Integer efConstruction = (Integer) args[2];
443480
Integer confidenceInterval = (Integer) args[3];

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,13 @@
8888
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKED_EMBEDDINGS_FIELD;
8989
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKED_OFFSET_FIELD;
9090
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKS_FIELD;
91+
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CONFIDENCE_INTERVAL_FIELD;
92+
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.EF_CONSTRUCTION_FIELD;
9193
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INDEX_OPTIONS_FIELD;
9294
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INFERENCE_FIELD;
9395
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INFERENCE_ID_FIELD;
9496
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.MODEL_SETTINGS_FIELD;
97+
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.M_FIELD;
9598
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.SEARCH_INFERENCE_ID_FIELD;
9699
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.TEXT_FIELD;
97100
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.TYPE_FIELD;
@@ -986,7 +989,55 @@ private static boolean canMergeIndexOptions(
986989
if (previous == null || current == null) {
987990
return true;
988991
}
989-
conflicts.addConflict(INDEX_OPTIONS_FIELD, "");
992+
993+
if (previous.getClass() != current.getClass()) {
994+
conflicts.addConflict(INDEX_OPTIONS_FIELD, "Incompatible classes");
995+
return false;
996+
}
997+
998+
if (current instanceof SemanticTextField.DenseVectorIndexOptions) {
999+
return canMergeDenseVectorIndexOptions(
1000+
(SemanticTextField.DenseVectorIndexOptions) previous,
1001+
(SemanticTextField.DenseVectorIndexOptions) current,
1002+
conflicts
1003+
);
1004+
}
1005+
1006+
conflicts.addConflict(INDEX_OPTIONS_FIELD, "previous: " + previous + ", current: " + current);
9901007
return false;
9911008
}
1009+
1010+
private static boolean canMergeDenseVectorIndexOptions(
1011+
SemanticTextField.DenseVectorIndexOptions previous,
1012+
SemanticTextField.DenseVectorIndexOptions current,
1013+
Conflicts conflicts
1014+
) {
1015+
1016+
if (Objects.equals(previous.type(), current.type()) == false) {
1017+
conflicts.addConflict(TYPE_FIELD, "");
1018+
return false;
1019+
}
1020+
1021+
if (previous.m() != null && current.m() != null && previous.m().equals(current.m()) == false) {
1022+
conflicts.addConflict(M_FIELD, "");
1023+
return false;
1024+
}
1025+
1026+
if (previous.efConstruction() != null
1027+
&& current.efConstruction() != null
1028+
&& previous.efConstruction().equals(current.efConstruction()) == false) {
1029+
conflicts.addConflict(EF_CONSTRUCTION_FIELD, "");
1030+
return false;
1031+
}
1032+
1033+
if (previous.confidenceInterval() != null
1034+
&& current.confidenceInterval() != null
1035+
&& previous.confidenceInterval().equals(current.confidenceInterval()) == false) {
1036+
conflicts.addConflict(CONFIDENCE_INTERVAL_FIELD, "");
1037+
return false;
1038+
}
1039+
1040+
return true;
1041+
1042+
}
9921043
}

0 commit comments

Comments
 (0)