Search code examples
scalaelasticsearchelastic4s

Update Mapping Defination is ELasticSearch using Scala


I want to know how to update the mapping of existing INDEX. SO I have an INDEX:

{
    "state": "xxx",
    "settings": {
        "index": {
            "creation_date": "xxx",
            "number_of_shards": "xxx",
            "number_of_replicas": "xxx",
            "uuid": "xxx",
            "version": {
                "created": "xxx"
            }
        }
    },
    "mappings": {
        "es_content_entry": {
            "properties": {
                "input": {
                    "properties": {
                        "zipCode": {
                            "type": "string"
                        },
                        "address": {
                            "type": "string"
                        },
                        "cityName": {
                            "type": "string"
                        },
                    }
                }
            }
        }
    }
}

I am updating the mapping using scala for this INDEX, like this: CheckFieldMappingResult is a object with

indexName: String,
entryType: String,
path: Seq[String],
localMapping: Option[TypedFieldDefinition],
remoteJsonMapping : Option[JsObject],
isConsistent: Boolean,
status: String

val remoteMissingObject: Seq[CheckFieldMappingResult] = mappingConsistensyResult.filter(p => p.status == "REMOTE_MISSING")

    val entryType: Seq[String] = remoteMissingObject.map(_.entryType)
    val path: Seq[Seq[String]] = remoteMissingObject.map(_.path)
    val foo: Seq[TypedFieldDefinition] = remoteMissingObject.map(_.localMapping.get)
    print(foo)

for (remote <- remoteMissingObject)yield {
    esClient.execute{put
    putMapping(INDEX_NAME/remote.entryType).fields {
        val too: String = path.flatten.head
        println(too)
        val foo: TypedFieldDefinition = remote.localMapping.get
        foo
    }

    }
}

It gives me this output:

{
    "state": "xxx",
    "settings": {
        "index": {
            "creation_date": "xxx",
            "number_of_shards": "xxx",
            "number_of_replicas": "xxx",
            "uuid": "xxx",
            "version": {
                "created": "xxx"
            }
        }
    },
    "mappings": {
        "es_content_entry": {
            "properties": {
                "additionnalInfos": {
                    "type": "string"
                },
                "input": {
                    "properties": {
                        "zipCode": {
                            "type": "string"
                        },
                        "address": {
                            "type": "string"
                        },
                        "cityName": {
                            "type": "string"
                        },
                    }
                }
            }
        }
    }
}

It add additional Infos at the start of the index, but i want it to add this path INDEX_NAME\es_content_entry\input\addtionalinfos How can i do that?


Solution

  • I created a object of TypedFieldDefination for all the path values.

    for(
    
      pathToken <- path.reverse.drop(1)
    
    ) {
    
      fullTypedFieldDefinition = FieldDefinition.objectField(Seq(fullTypedFieldDefinition))(pathToken)
    
    }