I use go-mysql-elasticsearch to synchronize data between MySQL and ElasticSearch. This way, once an item is created in MySQL, it is automatically created in ElasticSearch.
The mappings in ElasticSearch are created automatically, and that part works without a problem. All of my data is in sync.
The problem appears when I try to add new mappings to the ElasticSearch index and modify them.
My index is called products, and the type is also products. I add new mappings like this:
PUT 127.0.0.1:9200/products/_mapping/products
{
"properties": {
"ProductCityViews" : {
"properties": {
}
}
}
}
This works as expected, and when I visit:
GET http://127.0.0.1:9200/products?pretty=true
I can see that the new mappings are added to the products index:
"ProductCityViews" : {
"type" : "object"
}
However, when I try updating the products to add values to the new fields, I get a illegal_argument_exception, like so:
client.update( {
index: 'products',
type: 'products',
id: id,
body: {
script: `
if ( ctx._source.ProductCityViews["Washington"] == null ) { // If ProductCityViews.Washington doesn't exist
ctx._source.ProductCityViews["Washington"] = 1; // Create it
}
`,
lang: "painless"
}
}, ( error, response, status ) => {
if ( error ) {
console.log( error, response, status ); // Log the error to the console
return; // Return to prevent further actions
}
}) ;
The illegal_argument_exception is thrown for:
ctx._source.ProductCityViews
This makes sense, because the products don't have any values set for this, meaning that it's null.
The questions here is, how do I update that null field?
Google didn't provide me with any solid answers, hence this post.
To clarify, I am trying to achieve this in the end:
ProductCityViews: {
Washington: 1,
Tokyo: 23
}
- How do I update a null object in ElasticSearch?
Is it even possible?
Am I missing something?