@@ -41,6 +41,7 @@ class ElasticSearch(
4141 lazy val url : String = config.url
4242 lazy val shards : Int = config.shards
4343 lazy val replicas : Int = config.replicas
44+ lazy val includeDenseVectorMappings : Boolean = config.includeDenseVectorMappings
4445
4546
4647 def migrationAwareUpdater [REQUEST , RESPONSE ](
@@ -78,9 +79,13 @@ class ElasticSearch(
7879 executeAndLog(request, s " Setting migration info on image id: ${imageId}" )
7980 }
8081
82+ private def prepareImageJson (json : JsObject ): JsObject =
83+ if (includeDenseVectorMappings) json
84+ else json - " embedding"
85+
8186 def directInsert (image : Image , indexName : String )(implicit ex : ExecutionContext , logMarker : LogMarker ): Future [ElasticSearchInsertResponse ] =
8287 executeAndLog(
83- indexInto(indexName).id(image.id).source(Json .stringify(Json .toJson(image))),
88+ indexInto(indexName).id(image.id).source(Json .stringify(prepareImageJson( Json .toJson(image).as[ JsObject ] ))),
8489 s " ES6 indexing image ${image.id} into index ' $indexName' "
8590 ).map(indexResponse =>
8691 ElasticSearchInsertResponse (indexResponse.result.index)
@@ -92,14 +97,14 @@ class ElasticSearch(
9297
9398 // On insert, we know we will not have a lastModified to consider, so we always take the one we get
9499 val insertImage = image.copy(lastModified = Some (lastModified))
95- val insertImageAsJson = Json .toJson(insertImage)
100+ val insertImageAsJson = prepareImageJson( Json .toJson(insertImage).as[ JsObject ] )
96101
97102 def runUpsertIntoIndex (indexAlias : String , maybeEsInfo : Option [JsObject ]) = {
98103 val esInfo = maybeEsInfo.getOrElse(JsObject .empty)
99104
100105 // On update, we do not want to take the one we have been given unless it is newer - see updateLastModifiedScript script
101106 val updateImage = image.copy(lastModified = None )
102- val upsertImageAsJson = Json .toJson(updateImage)
107+ val upsertImageAsJson = prepareImageJson( Json .toJson(updateImage).as[ JsObject ] )
103108
104109 val painlessSource =
105110 // If there are old identifiers, then merge any new identifiers into old and use the merged results as the new identifiers
@@ -214,18 +219,24 @@ class ElasticSearch(
214219 def updateEmbedding (id : String , embedding : Embedding , lastModified : DateTime )
215220 (implicit ex : ExecutionContext , logMarker : LogMarker ): List [Future [ElasticSearchUpdateResponse ]] = {
216221
217- val replaceEmbeddingScript = " ctx._source.embedding = params.embedding;"
222+ if (! includeDenseVectorMappings) {
223+ logger.info(s " Skipping embedding update for image $id: dense vector mappings are disabled " )
224+ List (Future .successful(ElasticSearchUpdateResponse ()))
225+ } else {
218226
219- val scriptSource = loadPainless(replaceEmbeddingScript)
227+ val replaceEmbeddingScript = " ctx._source.embedding = params.embedding; "
220228
221- val embeddingParameter = asNestedMap( Json .toJson(embedding) )
229+ val scriptSource = loadPainless(replaceEmbeddingScript )
222230
223- val eventualUpdateResponse = migrationAwareUpdater(
224- requestFromIndexName = indexName => prepareUpdateRequest(indexName, id, scriptSource, lastModified, (" embedding" , embeddingParameter)),
225- logMessageFromIndexName = indexName => s " ES6 updating embedding on image $id in index $indexName"
226- ).incrementOnFailure(metrics.map(_.failedEmbeddingUpdates)) { case _ => true }
231+ val embeddingParameter = asNestedMap(Json .toJson(embedding))
227232
228- List (eventualUpdateResponse.map(_ => ElasticSearchUpdateResponse ()))
233+ val eventualUpdateResponse = migrationAwareUpdater(
234+ requestFromIndexName = indexName => prepareUpdateRequest(indexName, id, scriptSource, lastModified, (" embedding" , embeddingParameter)),
235+ logMessageFromIndexName = indexName => s " ES6 updating embedding on image $id in index $indexName"
236+ ).incrementOnFailure(metrics.map(_.failedEmbeddingUpdates)) { case _ => true }
237+
238+ List (eventualUpdateResponse.map(_ => ElasticSearchUpdateResponse ()))
239+ }
229240 }
230241
231242 def applyImageMetadataOverride (id : String , metadata : Edits , lastModified : DateTime )
0 commit comments