From b8559ea841f6344f07b33cc669f6f070cf62cf79 Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Thu, 19 Sep 2024 09:45:26 +0200 Subject: [PATCH] HSEARCH-5245 Always add index.knn for OpenSearch indexes with vector fields --- .../AbstractElasticsearchVectorFieldTypeOptionsStep.java | 4 +++- ...stractOpenSearch2VectorFieldTypeMappingContributor.java | 7 +++++-- .../Elasticsearch812VectorFieldTypeMappingContributor.java | 6 ++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/dsl/impl/AbstractElasticsearchVectorFieldTypeOptionsStep.java b/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/dsl/impl/AbstractElasticsearchVectorFieldTypeOptionsStep.java index 536a4ad7bf5..819ad23c0dc 100644 --- a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/dsl/impl/AbstractElasticsearchVectorFieldTypeOptionsStep.java +++ b/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/dsl/impl/AbstractElasticsearchVectorFieldTypeOptionsStep.java @@ -106,7 +106,6 @@ public ElasticsearchIndexValueFieldType toIndexFieldType() { builder.codec( codec ); if ( resolvedSearchable ) { builder.searchable( true ); - mappingContributor.contribute( builder, this ); builder.queryElementFactory( PredicateTypeKeys.EXISTS, new ElasticsearchExistsPredicate.Factory<>() ); } @@ -117,6 +116,9 @@ public ElasticsearchIndexValueFieldType toIndexFieldType() { builder.multivaluable( false ); + // NOTE: we make additional contribution at the end of the configuration to make sure that + // the context we pass (this) is fully configured: + mappingContributor.contribute( builder, this ); return builder.build(); } diff --git a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/mapping/impl/AbstractOpenSearch2VectorFieldTypeMappingContributor.java b/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/mapping/impl/AbstractOpenSearch2VectorFieldTypeMappingContributor.java index 376e7e94c9b..1182d08e7db 100644 --- a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/mapping/impl/AbstractOpenSearch2VectorFieldTypeMappingContributor.java +++ b/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/mapping/impl/AbstractOpenSearch2VectorFieldTypeMappingContributor.java @@ -64,9 +64,12 @@ public final void contribute(ElasticsearchIndexValueFieldType.Builder bui SearchQueryElementFactory, ElasticsearchSearchIndexValueFieldContext> factory = getKnnPredicateFactory( builder ); - builder.queryElementFactory( PredicateTypeKeys.KNN, factory ); - builder.contributeAdditionalIndexSettings( settings -> settings.addKnn( true ) ); + + // only add a predicate if the field on which this contribution happens is searchable: + if ( context.searchable() ) { + builder.queryElementFactory( PredicateTypeKeys.KNN, factory ); + } } protected abstract SearchQueryElementFactory void contribute(ElasticsearchIndexValueFieldType.Builder builder, Context context) { - builder.queryElementFactory( PredicateTypeKeys.KNN, - new ElasticsearchKnnPredicate.Elasticsearch812Factory<>( builder.codec() ) ); + if (context.searchable() ) { + builder.queryElementFactory( PredicateTypeKeys.KNN, + new ElasticsearchKnnPredicate.Elasticsearch812Factory<>( builder.codec() ) ); + } } private static String resolveDefault(VectorSimilarity vectorSimilarity) {