1212import org .elasticsearch .common .io .stream .StreamInput ;
1313import org .elasticsearch .common .io .stream .StreamOutput ;
1414import org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper ;
15+ import org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapperTestUtils ;
1516import org .elasticsearch .inference .Model ;
1617import org .elasticsearch .inference .ModelConfigurations ;
1718import org .elasticsearch .inference .ModelSecrets ;
2526import org .elasticsearch .xpack .inference .services .ServiceUtils ;
2627
2728import java .io .IOException ;
29+ import java .util .ArrayList ;
30+ import java .util .Arrays ;
2831import java .util .HashMap ;
32+ import java .util .List ;
2933import java .util .Map ;
3034
35+ import static org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper .BBQ_MIN_DIMS ;
3136import static org .elasticsearch .test .ESTestCase .randomAlphaOfLength ;
3237import static org .elasticsearch .test .ESTestCase .randomFrom ;
3338import static org .elasticsearch .test .ESTestCase .randomInt ;
@@ -39,9 +44,60 @@ public static TestModel createRandomInstance() {
3944 }
4045
4146 public static TestModel createRandomInstance (TaskType taskType ) {
42- var dimensions = taskType == TaskType .TEXT_EMBEDDING ? randomInt (64 ) : null ;
43- var similarity = taskType == TaskType .TEXT_EMBEDDING ? randomFrom (SimilarityMeasure .values ()) : null ;
44- var elementType = taskType == TaskType .TEXT_EMBEDDING ? randomFrom (DenseVectorFieldMapper .ElementType .values ()) : null ;
47+ return createRandomInstance (taskType , null , null );
48+ }
49+
50+ public static TestModel createRandomInstance (
51+ TaskType taskType ,
52+ List <DenseVectorFieldMapper .ElementType > excludedElementTypes ,
53+ List <SimilarityMeasure > excludedSimilarities
54+ ) {
55+ // Use a max dimension count that has a reasonable probability of being compatible with BBQ
56+ return createRandomInstance (taskType , excludedElementTypes , excludedSimilarities , BBQ_MIN_DIMS * 2 );
57+ }
58+
59+ public static TestModel createRandomInstance (
60+ TaskType taskType ,
61+ List <DenseVectorFieldMapper .ElementType > excludedElementTypes ,
62+ List <SimilarityMeasure > excludedSimilarities ,
63+ int maxDimensions
64+ ) {
65+ List <DenseVectorFieldMapper .ElementType > supportedElementTypes = new ArrayList <>(
66+ Arrays .asList (DenseVectorFieldMapper .ElementType .values ())
67+ );
68+ if (excludedElementTypes != null ) {
69+ supportedElementTypes .removeAll (excludedElementTypes );
70+ if (supportedElementTypes .isEmpty ()) {
71+ throw new IllegalArgumentException ("No supported element types with excluded element types " + excludedElementTypes );
72+ }
73+ }
74+
75+ var elementType = taskType == TaskType .TEXT_EMBEDDING ? randomFrom (supportedElementTypes ) : null ;
76+ var dimensions = taskType == TaskType .TEXT_EMBEDDING
77+ ? DenseVectorFieldMapperTestUtils .randomCompatibleDimensions (elementType , maxDimensions )
78+ : null ;
79+
80+ SimilarityMeasure similarity = null ;
81+ if (taskType == TaskType .TEXT_EMBEDDING ) {
82+ List <SimilarityMeasure > supportedSimilarities = new ArrayList <>(
83+ DenseVectorFieldMapperTestUtils .getSupportedSimilarities (elementType )
84+ );
85+ if (excludedSimilarities != null ) {
86+ supportedSimilarities .removeAll (excludedSimilarities );
87+ }
88+
89+ if (supportedSimilarities .isEmpty ()) {
90+ throw new IllegalArgumentException (
91+ "No supported similarities for combination of element type ["
92+ + elementType
93+ + "] and excluded similarities "
94+ + (excludedSimilarities == null ? List .of () : excludedSimilarities )
95+ );
96+ }
97+
98+ similarity = randomFrom (supportedSimilarities );
99+ }
100+
45101 return new TestModel (
46102 randomAlphaOfLength (4 ),
47103 taskType ,
0 commit comments