0

I have a question, I am trying to update an object in ES, so, every time I query it I will get all the updated info. I have an object like this:

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 768,
    "successful": 768,
    "failed": 0
  },
  "hits": {
    "total": 456,
    "max_score": 1,
    "hits": [
      {
        "_index": "sometype_1",
        "_type": "sometype",
        "_id": "12312321312312",
        "_score": 1,
        "_source": {
          "readModel": {
            "id": "asdfqwerzcxv",
            "status": "active",
            "hidden": false,
            "message": "hello world",
          },
          "model": {
            "id": "asdfqwerzcxv",
            "content": {
              "objectId": "421421312312",
              "content": {
                 "@type": "text",
                 "text": "hello world"
               }
              ..... //the rest of the object...

And I want to update the message (part of the read model), so I made something like this:

PUT test/readModel.id/ID123
{
    "message" : "hello"
}

But everytime I query looking for ID123 I get same info (and even worse, the more PUTs I make the more objects I get back (with same info)

any idea how to?

jpganz18
  • 5,508
  • 17
  • 66
  • 115
  • Will there be several documents with `readModel.id: asdfqwerzcxv` that you need to update or just a single one? Below you have an answer both both cases. – Val Nov 28 '18 at 14:41

1 Answers1

2

If there's only a single document that you need to update, you can use the Update API like this:

POST sometype_1/sometype/12312321312312/_update
{
  "doc": {
    "model.message": { ... your JSON object here... }
  }
}

If several documents can have readModel.id: asdfqwerzcxv and you want to update all of them with the same message, then you need to use the Update by query API like this_

POST sometype_1/_update_by_query
{
  "script": {
    "source": "ctx._source.message = params.message",
    "lang": "painless",
    "params": {
      "message": "hello"
    }
  },
  "query": {
    "match": {
      "readModel.id": "asdfqwerzcxv"
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • what if I want to update the model (not the readModel) with a json value? like "message" : { "value" : "hello", "type" : "text"}, is it fine this way? – jpganz18 Nov 28 '18 at 14:52
  • Does `model` already have a field `message` of type `text`? or is it a new field you're creating? – Val Nov 28 '18 at 14:58
  • no, it doesnt have it, or could be a new field like message-description for example if is easier... – jpganz18 Nov 28 '18 at 15:00
  • Back to my initial question: Will there be several documents with `readModel.id: asdfqwerzcxv` that you need to update or just a single one? – Val Nov 28 '18 at 15:01
  • thanks! last thing... if instead update message I want a new field like messageDescription for example, could I just change that on the "model.message" ? – jpganz18 Nov 28 '18 at 15:04
  • Yes, you can add whatever you want in the `doc:{}` portion provided the values you update are compatible with your existing mapping, otherwise you'll get an error. – Val Nov 28 '18 at 15:07
  • yes, I get an error due the mapping is strict, anyway to update this mapping? – jpganz18 Nov 28 '18 at 15:13
  • Please check this other answer regarding mapping updates: https://stackoverflow.com/questions/40354202/is-it-possible-to-update-an-existing-field-in-an-index-through-mapping-in-elasti/40354251#40354251 (once a field is created, you don't have many options to change/alter its definition) – Val Nov 28 '18 at 15:16