@@ -452,17 +452,25 @@ private void validateIndexOptions(SemanticTextIndexOptions indexOptions, String
452452 return ;
453453 }
454454
455- if (indexOptions .type () == SemanticTextIndexOptions .SupportedIndexOptions .SPARSE_VECTOR ) {
456- // sparse vector index options are validated in the ctor when created
457- return ;
458- }
459-
460455 if (modelSettings == null ) {
461456 throw new IllegalArgumentException (
462457 "Model settings must be set to validate index options for inference ID [" + inferenceId + "]"
463458 );
464459 }
465460
461+ if (indexOptions .type () == SemanticTextIndexOptions .SupportedIndexOptions .SPARSE_VECTOR ) {
462+ if (modelSettings .taskType () != SPARSE_EMBEDDING ) {
463+ throw new IllegalArgumentException (
464+ "Invalid task type for index options, required ["
465+ + SPARSE_EMBEDDING
466+ + "] but was ["
467+ + modelSettings .taskType ()
468+ + "]"
469+ );
470+ }
471+ return ;
472+ }
473+
466474 if (indexOptions .type () == SemanticTextIndexOptions .SupportedIndexOptions .DENSE_VECTOR ) {
467475 if (modelSettings .taskType () != TEXT_EMBEDDING ) {
468476 throw new IllegalArgumentException (
@@ -1179,18 +1187,7 @@ private static Mapper.Builder createEmbeddingsField(
11791187 false
11801188 ).setStored (useLegacyFormat == false );
11811189
1182- if (indexOptions != null ) {
1183- SparseVectorFieldMapper .SparseVectorIndexOptions sparseVectorIndexOptions =
1184- (SparseVectorFieldMapper .SparseVectorIndexOptions ) indexOptions .indexOptions ();
1185-
1186- sparseVectorMapperBuilder .setIndexOptions (sparseVectorIndexOptions );
1187- } else {
1188- SparseVectorFieldMapper .SparseVectorIndexOptions defaultIndexOptions = SparseVectorFieldMapper .SparseVectorIndexOptions
1189- .getDefaultIndexOptions (indexVersionCreated );
1190- if (defaultIndexOptions != null ) {
1191- sparseVectorMapperBuilder .setIndexOptions (defaultIndexOptions );
1192- }
1193- }
1190+ configureSparseVectorMapperBuilder (indexVersionCreated , sparseVectorMapperBuilder , indexOptions );
11941191
11951192 yield sparseVectorMapperBuilder ;
11961193 }
@@ -1201,15 +1198,34 @@ private static Mapper.Builder createEmbeddingsField(
12011198 false
12021199 );
12031200
1204- setDenseVectorMapperBuilderForEmbeddings (indexVersionCreated , denseVectorMapperBuilder , modelSettings , indexOptions );
1201+ configureDenseVectorMapperBuilder (indexVersionCreated , denseVectorMapperBuilder , modelSettings , indexOptions );
12051202
12061203 yield denseVectorMapperBuilder ;
12071204 }
12081205 default -> throw new IllegalArgumentException ("Invalid task_type in model_settings [" + modelSettings .taskType ().name () + "]" );
12091206 };
12101207 }
12111208
1212- private static void setDenseVectorMapperBuilderForEmbeddings (
1209+ private static void configureSparseVectorMapperBuilder (
1210+ IndexVersion indexVersionCreated ,
1211+ SparseVectorFieldMapper .Builder sparseVectorMapperBuilder ,
1212+ SemanticTextIndexOptions indexOptions
1213+ ) {
1214+ if (indexOptions != null ) {
1215+ SparseVectorFieldMapper .SparseVectorIndexOptions sparseVectorIndexOptions =
1216+ (SparseVectorFieldMapper .SparseVectorIndexOptions ) indexOptions .indexOptions ();
1217+
1218+ sparseVectorMapperBuilder .setIndexOptions (sparseVectorIndexOptions );
1219+ } else {
1220+ SparseVectorFieldMapper .SparseVectorIndexOptions defaultIndexOptions = SparseVectorFieldMapper .SparseVectorIndexOptions
1221+ .getDefaultIndexOptions (indexVersionCreated );
1222+ if (defaultIndexOptions != null ) {
1223+ sparseVectorMapperBuilder .setIndexOptions (defaultIndexOptions );
1224+ }
1225+ }
1226+ }
1227+
1228+ private static void configureDenseVectorMapperBuilder (
12131229 IndexVersion indexVersionCreated ,
12141230 DenseVectorFieldMapper .Builder denseVectorMapperBuilder ,
12151231 MinimalServiceSettings modelSettings ,
@@ -1295,10 +1311,12 @@ static SemanticTextIndexOptions defaultIndexOptions(IndexVersion indexVersionCre
12951311 }
12961312
12971313 if (modelSettings .taskType () == SPARSE_EMBEDDING ) {
1298- return new SemanticTextIndexOptions (
1299- SemanticTextIndexOptions .SupportedIndexOptions .SPARSE_VECTOR ,
1300- SparseVectorFieldMapper .SparseVectorIndexOptions .getDefaultIndexOptions (indexVersionCreated )
1301- );
1314+ SparseVectorFieldMapper .SparseVectorIndexOptions sparseVectorIndexOptions = SparseVectorFieldMapper .SparseVectorIndexOptions
1315+ .getDefaultIndexOptions (indexVersionCreated );
1316+
1317+ return sparseVectorIndexOptions == null
1318+ ? null
1319+ : new SemanticTextIndexOptions (SemanticTextIndexOptions .SupportedIndexOptions .SPARSE_VECTOR , sparseVectorIndexOptions );
13021320 }
13031321
13041322 return null ;
0 commit comments