-
Notifications
You must be signed in to change notification settings - Fork 68
/
AOSCreateIndexOpDispenser.java
93 lines (82 loc) · 3.87 KB
/
AOSCreateIndexOpDispenser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (c) 2024 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.adapter.opensearch.dispensers;
import io.nosqlbench.adapter.opensearch.AOSAdapter;
import io.nosqlbench.adapter.opensearch.ops.AOSCreateIndexOp;
import io.nosqlbench.adapters.api.templating.ParsedOp;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.mapping.*;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import java.util.Map;
import java.util.function.LongFunction;
public class AOSCreateIndexOpDispenser extends AOSBaseOpDispenser {
private final ParsedOp pop;
private final int dimensions;
private final int ef_construction;
private final int m;
public AOSCreateIndexOpDispenser(AOSAdapter adapter, ParsedOp op, LongFunction<String> targetF) {
super(adapter, op, targetF);
this.pop = op;
this.dimensions = pop.getStaticValue("dimensions",Integer.class).intValue();
this.ef_construction = pop.getStaticValue("ef_construction",Integer.class).intValue();
this.m = pop.getStaticValue("m",Integer.class).intValue();
}
@Override
public LongFunction<AOSCreateIndexOp> createOpFunc(LongFunction<OpenSearchClient> clientF, ParsedOp op,
LongFunction<String> targetF) {
CreateIndexRequest.Builder eb = new CreateIndexRequest.Builder();
LongFunction<CreateIndexRequest.Builder> bfunc =
l -> new CreateIndexRequest.Builder()
.settings(b -> b.knn(true))
.index(targetF.apply(1));
bfunc = op.enhanceFunc(bfunc, "mappings", Map.class, this::resolveTypeMapping);
LongFunction<CreateIndexRequest.Builder> finalBfunc = bfunc;
return (long l) -> new AOSCreateIndexOp(clientF.apply(l), finalBfunc.apply(l).build());
}
// https://opensearch.org/docs/latest/search-plugins/knn/knn-index/
private CreateIndexRequest.Builder resolveTypeMapping(CreateIndexRequest.Builder eb, Map<?, ?> mappings) {
TypeMapping.Builder builder = new TypeMapping.Builder().properties(
Map.of(
"key",
new Property.Builder()
.text(b -> b)
.build(),
"value",
new Property.Builder()
.knnVector(new KnnVectorProperty.Builder()
.dimension(dimensions)
.method(
new KnnVectorMethod.Builder()
.name("hnsw")
.engine("faiss")
.spaceType("l2")
.parameters(
Map.of(
"ef_construction", JsonData.of(ef_construction),
"m", JsonData.of(m)
)
)
.build()
).build()
).build()
))
.fieldNames(new FieldNamesField.Builder()
.enabled(true).build()
);
return eb.mappings(b -> builder);
}
}