5
5
6
6
package org .opensearch .knn .index .engine ;
7
7
8
+ import com .google .common .annotations .VisibleForTesting ;
8
9
import org .apache .logging .log4j .LogManager ;
9
10
import org .apache .logging .log4j .Logger ;
11
+ import org .opensearch .Version ;
10
12
import org .opensearch .knn .index .mapper .CompressionLevel ;
11
13
import org .opensearch .knn .index .mapper .Mode ;
12
14
@@ -22,47 +24,83 @@ public final class EngineResolver {
22
24
23
25
private EngineResolver () {}
24
26
27
+ @ VisibleForTesting
28
+ KNNEngine resolveEngine (KNNMethodConfigContext knnMethodConfigContext , KNNMethodContext knnMethodContext , boolean requiresTraining ) {
29
+ return logAndReturnEngine (resolveKNNEngine (knnMethodConfigContext , knnMethodContext , requiresTraining , Version .CURRENT ));
30
+ }
31
+
25
32
/**
26
33
* Based on the provided {@link Mode} and {@link CompressionLevel}, resolve to a {@link KNNEngine}.
27
34
*
28
35
* @param knnMethodConfigContext configuration context
29
36
* @param knnMethodContext KNNMethodContext
30
37
* @param requiresTraining whether config requires training
38
+ * @param version opensearch index version
31
39
* @return {@link KNNEngine}
32
40
*/
33
41
public KNNEngine resolveEngine (
34
42
KNNMethodConfigContext knnMethodConfigContext ,
35
43
KNNMethodContext knnMethodContext ,
36
- boolean requiresTraining
44
+ boolean requiresTraining ,
45
+ Version version
37
46
) {
38
- // User configuration gets precedence
39
- if (knnMethodContext != null && knnMethodContext .isEngineConfigured ()) {
40
- return logAndReturnEngine (knnMethodContext .getKnnEngine ());
47
+ return logAndReturnEngine (resolveKNNEngine (knnMethodConfigContext , knnMethodContext , requiresTraining , version ));
48
+ }
49
+
50
+ /**
51
+ * Based on the provided {@link Mode} and {@link CompressionLevel}, resolve to a {@link KNNEngine}.
52
+ *
53
+ * @param knnMethodConfigContext configuration context
54
+ * @param knnMethodContext KNNMethodContext
55
+ * @param requiresTraining whether config requires training
56
+ * @param version opensearch index version
57
+ * @return {@link KNNEngine}
58
+ */
59
+ private KNNEngine resolveKNNEngine (
60
+ KNNMethodConfigContext knnMethodConfigContext ,
61
+ KNNMethodContext knnMethodContext ,
62
+ boolean requiresTraining ,
63
+ Version version
64
+ ) {
65
+ // Check user configuration first
66
+ if (hasUserConfiguredEngine (knnMethodContext )) {
67
+ return knnMethodContext .getKnnEngine ();
41
68
}
42
69
43
- // Faiss is the only engine that supports training, so we default to faiss here for now
70
+ // Handle training case
44
71
if (requiresTraining ) {
45
- return logAndReturnEngine (KNNEngine .FAISS );
72
+ // Faiss is the only engine that supports training, so we default to faiss here for now
73
+ return KNNEngine .FAISS ;
46
74
}
47
75
48
76
Mode mode = knnMethodConfigContext .getMode ();
49
77
CompressionLevel compressionLevel = knnMethodConfigContext .getCompressionLevel ();
78
+
50
79
// If both mode and compression are not specified, we can just default
51
80
if (Mode .isConfigured (mode ) == false && CompressionLevel .isConfigured (compressionLevel ) == false ) {
52
- return logAndReturnEngine ( KNNEngine .DEFAULT ) ;
81
+ return KNNEngine .DEFAULT ;
53
82
}
54
83
55
- // For 1x, we need to default to faiss if mode is provided and use nmslib otherwise
84
+ if (compressionLevel == CompressionLevel .x4 ) {
85
+ // Lucene is only engine that supports 4x - so we have to default to it here.
86
+ return KNNEngine .LUCENE ;
87
+ }
56
88
if (CompressionLevel .isConfigured (compressionLevel ) == false || compressionLevel == CompressionLevel .x1 ) {
57
- return logAndReturnEngine (mode == Mode .ON_DISK ? KNNEngine .FAISS : KNNEngine .NMSLIB );
89
+ // For 1x or no compression, we need to default to faiss if mode is provided and use nmslib otherwise based on version check
90
+ return resolveEngineForX1OrNoCompression (mode , version );
58
91
}
92
+ return KNNEngine .FAISS ;
93
+ }
59
94
60
- // Lucene is only engine that supports 4x - so we have to default to it here.
61
- if (compressionLevel == CompressionLevel .x4 ) {
62
- return logAndReturnEngine (KNNEngine .LUCENE );
63
- }
95
+ private boolean hasUserConfiguredEngine (KNNMethodContext knnMethodContext ) {
96
+ return knnMethodContext != null && knnMethodContext .isEngineConfigured ();
97
+ }
64
98
65
- return logAndReturnEngine (KNNEngine .FAISS );
99
+ private KNNEngine resolveEngineForX1OrNoCompression (Mode mode , Version version ) {
100
+ if (version != null && version .onOrAfter (Version .V_2_19_0 )) {
101
+ return KNNEngine .FAISS ;
102
+ }
103
+ return mode == Mode .ON_DISK ? KNNEngine .FAISS : KNNEngine .NMSLIB ;
66
104
}
67
105
68
106
private KNNEngine logAndReturnEngine (KNNEngine knnEngine ) {
0 commit comments