1

I'm trying to update a partial nested object with a script. But what I have written is incorrect.

I've this object:

{
  "_index" : "test_7",
  "_type" : "testField",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "view" : {
      "hit" : 3,
      "pages" : [
        {
          "name" : "A",
          "hit" : 1
        },
        {
          "name" : "B",
          "hit" : 1
        },
        {
          "name" : "C",
          "hit" : 1
        }
      ]
    }
  }
}

I would like add 3 hits on a page.

With the existing page named "B":

  {
    "view" : {
      "hit" : 6,
      "pages" : [
        {
          "name" : "A",
          "hit" : 1
        },
        {
          "name" : "B",
          "hit" : 4
        },
        {
          "name" : "C",
          "hit" : 1
        }
      ]
    }
  }

With a new page named "D":

  {
    "view" : {
      "hit" : 6,
      "pages" : [
        {
          "name" : "A",
          "hit" : 1
        },
        {
          "name" : "B",
          "hit" : 1
        },
        {
          "name" : "C",
          "hit" : 1
        },
        {
          "name" : "D",
          "hit" : 3
        }
      ]
    }
  }

Please can you tell me how write the HTTP request? Moreover where can I read more documentation about "ctx._source"?

Thanks.

2
  • "I would like add 3 hits if the P page." - Your question is not very clear. Are you talking about two different scenarios ? Commented Dec 30, 2014 at 11:05
  • 1
    Yes because I don't know how many pages has my object. I may update an existing page, or I may add a new page. I looked stackoverflow.com/questions/18360225/… Commented Dec 30, 2014 at 11:09

1 Answer 1

1

ctx_source is nothing but the source JSON document that you submitted for indexing for that particular document. You need to use scripting support in the _update API

curl -XPOST 'http://localhost:9200/data/data/X_UucUuYTOqdB8eo2H-XWw/_update' -d '{
  "script": "increment",
  "params": {
    "newPage": {
      "name": "A",
      "hit": 40
    }
  }
}'

Increment script

source = ctx._source
Boolean isAdded = false;
for(page in source.view.pages){
    if(page.name == newPage.name){
        page.hit += newPage.hit
        isAdded = true
    }
}
if(!isAdded){
    source.view.pages += newPage
}

Here , specify the count and the pageName in the params section. If the pageName is something that is existing , that value would be incremented or a new one would be appended.

Hope this help.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx! Meanwhile I read mvel documentation (ES < 1.4): mvel.codehaus.org/Language+Guide+for+2.0 And I wrote an ugly script. Your one is better. Just for remaining: { "script" : "if ((name in ctx._source.view.pages).contains(updated_data.name)) { current = ($ in ctx._source.view.pages if $.name == updated_data.name)[0]; current.hit += updated_data.hit; } else { ctx._source.view.pages += updated_data; } ", "params" : { "updated_data" : { "name": "ini1", "hit": 3 } } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.