2626import org .elasticsearch .xpack .inference .services .ServiceUtils ;
2727
2828import java .io .IOException ;
29+ import java .util .ArrayList ;
2930import java .util .HashMap ;
31+ import java .util .List ;
3032import java .util .Map ;
3133
3234import static org .elasticsearch .test .ESTestCase .randomAlphaOfLength ;
@@ -40,13 +42,35 @@ public static TestModel createRandomInstance() {
4042 }
4143
4244 public static TestModel createRandomInstance (TaskType taskType ) {
45+ return createRandomInstance (taskType , null );
46+ }
47+
48+ public static TestModel createRandomInstance (TaskType taskType , List <SimilarityMeasure > excludedSimilarities ) {
4349 var elementType = taskType == TaskType .TEXT_EMBEDDING ? randomFrom (DenseVectorFieldMapper .ElementType .values ()) : null ;
4450 var dimensions = taskType == TaskType .TEXT_EMBEDDING
4551 ? DenseVectorFieldMapperTestUtils .randomCompatibleDimensions (elementType , 64 )
4652 : null ;
47- var similarity = taskType == TaskType .TEXT_EMBEDDING
48- ? randomFrom (DenseVectorFieldMapperTestUtils .getSupportedSimilarities (elementType ))
49- : null ;
53+
54+ SimilarityMeasure similarity = null ;
55+ if (taskType == TaskType .TEXT_EMBEDDING ) {
56+ List <SimilarityMeasure > supportedSimilarities = new ArrayList <>(
57+ DenseVectorFieldMapperTestUtils .getSupportedSimilarities (elementType )
58+ );
59+ if (excludedSimilarities != null ) {
60+ supportedSimilarities .removeAll (excludedSimilarities );
61+ }
62+
63+ if (supportedSimilarities .isEmpty ()) {
64+ throw new IllegalArgumentException (
65+ "No supported similarities for combination of element type ["
66+ + elementType
67+ + "] and excluded similarities "
68+ + (excludedSimilarities == null ? List .of () : excludedSimilarities )
69+ );
70+ }
71+
72+ similarity = randomFrom (supportedSimilarities );
73+ }
5074
5175 return new TestModel (
5276 randomAlphaOfLength (4 ),
0 commit comments