Skip to content

Commit

Permalink
merge index mapping in place
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Kao <[email protected]>
  • Loading branch information
seankao-az committed Dec 12, 2024
1 parent 4efb097 commit 7833a6c
Showing 1 changed file with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class FlintOpenSearchMetadataCacheWriter(options: FlintOptions)
var client: IRestHighLevelClient = null
try {
client = OpenSearchClientUtils.createClient(options)
val existingMapping = getIndexMapping(client, osIndexName)
val indexMapping = getIndexMapping(client, osIndexName)
val metadataCacheProperties = FlintMetadataCache(metadata).toMap.asJava
val mergedMapping = mergeMapping(existingMapping, metadataCacheProperties)
mergeMetadataCacheProperties(indexMapping, metadataCacheProperties)
val serialized = buildJson(builder => {
builder.field("_meta", mergedMapping.get("_meta"))
builder.field("properties", mergedMapping.get("properties"))
builder.field("_meta", indexMapping.get("_meta"))
builder.field("properties", indexMapping.get("properties"))
})
updateIndexMapping(client, osIndexName, serialized)
} catch {
Expand All @@ -61,23 +61,18 @@ class FlintOpenSearchMetadataCacheWriter(options: FlintOptions)
}

/**
* Merge existing mapping with metadata cache properties. Metadata cache is written into
* Merge metadata cache properties into index mapping in place. Metadata cache is written into
* _meta.properties field of index mapping.
*/
private def mergeMapping(
existingMapping: JMap[String, AnyRef],
metadataCacheProperties: JMap[String, AnyRef]): JMap[String, AnyRef] = {
val meta =
existingMapping.getOrDefault("_meta", Map.empty.asJava).asInstanceOf[JMap[String, AnyRef]]
val properties =
meta.getOrDefault("properties", Map.empty.asJava).asInstanceOf[JMap[String, AnyRef]]
val updatedProperties = new HashMap[String, AnyRef](properties)
updatedProperties.putAll(metadataCacheProperties)
val updatedMeta = new HashMap[String, AnyRef](meta)
updatedMeta.put("properties", updatedProperties)
val result = new HashMap[String, AnyRef](existingMapping)
result.put("_meta", updatedMeta)
result
private def mergeMetadataCacheProperties(
indexMapping: JMap[String, AnyRef],
metadataCacheProperties: JMap[String, AnyRef]): Unit = {
indexMapping
.computeIfAbsent("_meta", _ => new HashMap[String, AnyRef]())
.asInstanceOf[JMap[String, AnyRef]]
.computeIfAbsent("properties", _ => new HashMap[String, AnyRef]())
.asInstanceOf[JMap[String, AnyRef]]
.putAll(metadataCacheProperties)
}

private[metadatacache] def updateIndexMapping(
Expand Down

0 comments on commit 7833a6c

Please sign in to comment.