0

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?

Grim Reaper
  • 561
  • 4
  • 6
  • 22
  • May you show the complete error message? Which is the elasticsearch version that you use? I can't reproduce this with 6.0. By the way, this expression `ctx._source.ProductCityViews["Washington"] != null` checks that the key **exists**, which contradicts the comment that follows: `// If ProductCityViews.Washington doesn't exist`. – Nikolay Vasiliev Apr 06 '18 at 19:07
  • My apologies for the late response. That comment was actually a mistake on my part. It should say: `// If ProductCityViews.Washington DOES exist`. I'll edit my post. I run ElasticSearch 6.2.3. Also, I managed to somewhat fix it, but it's not the most elegant solution. Basically I update the index with `doc: { ProductCityViews: {} }`. This creates the missing field, but I have to do that for each new product. Then I just run a second update with the specified script and it works. I feel like there is a much cleaner solution that I am unaware of, though. – Grim Reaper Apr 06 '18 at 21:19
  • Still I am not able to reproduce. Does the object you are trying to update exist in the index? If it does exist, how does it look like exactly? Thank you. – Nikolay Vasiliev Apr 07 '18 at 18:13
  • I managed to fix it, finally. I will be posting an answer to this question when I find the time. Thank you very much, however, for trying to help! :) – Grim Reaper Apr 07 '18 at 18:15

0 Answers0