logo
down
shadow

elasticsearch: update in a nested object (HTTP)


elasticsearch: update in a nested object (HTTP)

By : Shin San
Date : November 19 2020, 01:01 AM
seems to work fine 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
code :
curl -XPOST 'http://localhost:9200/data/data/X_UucUuYTOqdB8eo2H-XWw/_update' -d '{
  "script": "increment",
  "params": {
    "newPage": {
      "name": "A",
      "hit": 40
    }
  }
}'
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
}


Share : facebook icon twitter icon
AngularJS - Update nested object property inside array using $http

AngularJS - Update nested object property inside array using $http


By : aicsoon
Date : March 29 2020, 07:55 AM
Does that help You should probably be taking a function argument. Also, why are you trying to modify it after a post (presumably the post updates the server). Retrieve, modify, then post back to the server. Also you should send the data using an argument with $http.post().
angular $http docs
code :
$scope.updateProperties = function() { // Update what with what???  Use one or more arguments?
  return $http.get('data.json')  // You could take the URL as argument
    .then(function(response) {
      response.data[0].properties.name = 'Lucas'; // Maybe use a function argument instead?
      return $http.post('data.json', response.data);
    })
}
Elasticsearch: How insert nested Json array object into Elasticsearch Index

Elasticsearch: How insert nested Json array object into Elasticsearch Index


By : Guillermo Andrade Hu
Date : March 29 2020, 07:55 AM
may help you . You need to create the mapping upfront and specifically mention that some fields have to be "type":"nested" otherwise ES will just create objects. Here I specifically refer to nested type of fields in ES.
If by nested you meant just the nested nature of the document, then you don't have to do anything.
Elasticsearch: remove/update field inside nested object

Elasticsearch: remove/update field inside nested object


By : shan
Date : March 29 2020, 07:55 AM
I wish this helpful for you , To add a new element to your nested field you can proceed like this:
code :
$ curl -XPOST 'localhost:9200/index/type/1212/_update?pretty' -d '
{
    "script" : "ctx._source.list_data += newElement",
    "params": {
        "newElement": {
           "list_id" : 121,
           "timestamp" : 1469050965
        }
    }
}'
$ curl -XPOST 'localhost:9200/index/type/1212/_update?pretty' -d '
{
    "script" : "ctx._source.list_data.removeAll{it.list_id == remove_id}",
    "params": {
        "remove_id" : 122
    }
}'
Empty nested object with partial update on ElasticSearch

Empty nested object with partial update on ElasticSearch


By : Pambos Gunner Consta
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I've the following document indexed in ElasticSearch , I suggest to do a scripted update like this and it will work:
code :
POST entities/_doc/1/_update
{
   "script" : {
      "source": """
         ctx._source.myObj.elements.clear(); 
         ctx._source.myObj.name = params.name;
      """,
      "params": {
        "name": "newTest"
      }
   }
}
Elasticsearch multilevel nested object filter and update

Elasticsearch multilevel nested object filter and update


By : als
Date : March 29 2020, 07:55 AM
wish helps you Please see below as how, looking at your document in question, I've created the mapping, the update by query script and the response as how it appears.
Mapping:
code :
PUT mybook
{
  "mappings": {
    "mydocs":{
      "properties":{
        "StoreName": {
          "type": "keyword"
        },
        "StoreID":{
          "type": "keyword"
        },
        "BookInfo":{
          "type": "nested",
          "properties": {
            "Comments" :{
              "type": "nested",
              "properties":{
                "date":{
                  "type": "date"
                },
                "Comment": {
                  "type": "text"
                }
              }
            },
            "Name": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
POST mybook/_update_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "StoreName": "test"
          }
        },
        {
          "term": {
            "StoreID": "12435"
          }
        },
        {
          "nested": {
            "path": "BookInfo",
            "query": {
              "term": {
                "BookInfo.Name": "Book1"
              }
            }
          }
        }
      ]
    }
  },
  "script": {
    "lang": "painless",
    "inline": """
      ArrayList list = ctx._source.BookInfo;
      for(int i=0; i<list.size();i++){
        HashMap myHashMap = list.get(i);
        boolean isPresent = false;
        for(int j=0; j<myHashMap.size();j++){
          String mybook = myHashMap.get(params.book_key);
          if(mybook==params.book_value)
          {
            ArrayList mylist = myHashMap.get("Comments");
            for(int k=0; k<mylist.size(); k++){
              HashMap existingMap = mylist.get(k);
              Iterator entries = existingMap.entrySet().iterator();
              while(entries.hasNext()){
                Map.Entry entry = (Map.Entry) entries.next();
                if(entry.getKey()==params.date_key && entry.getValue()==params.date_value){
                  isPresent = true
                }
              }  
            }

            if(!isPresent){
              HashMap myMap = new HashMap();
              myMap.put(params.date_key, params.date_value);
              myMap.put(params.comment_key, params.comment_value);
              mylist.add(myMap);
              isPresent = false;
            }
          }
        }
      }
    """,
    "params": {
      "book_key":   "Name",
      "book_value": "Book1",
      "date_key":   "date",
      "date_value": "2019-01-08",
      "comment_key": "Comment",
      "comment_value": "This is a new comment here"

  }}
}
"params": {
      "book_key":   "Name",
      "book_value": "Book1",
      "date_key":   "date",
      "date_value": "2019-01-08",
      "comment_key": "Comment",
      "comment_value": "This is a new comment here"

}
{
  "_index": "mybook",
  "_type": "mydocs",
  "_id": "1",
  "_version": 44,
  "found": true,
  "_source": {
    "StoreName": "test",
    "StoreID": "12435",
    "BookInfo": [
      {
        "Comments": [
          {
            "date": "2019-01-07",
            "Comment": "this is a nice book"
          },
          {
            "date": "2019-01-06",
            "Comment": "this is a nice book"
          },
          {
            "date": "2019-01-04",
            "Comment": "this is a nice book"
          },
          {
            "date": "2019-01-03",
            "Comment": "this is a nice book"
          },
          {
            "date": "2019-01-08",
            "Comment": "This is a new comment here"
          }
        ],
        "UPC": 2435466,
        "Name": "Book1"
      },
      {
        "Comments": [
          {
            "date": "2019-01-07",
            "Comment": "this is a nice book"
          },
          {
            "date": "2019-01-06",
            "Comment": "this is a nice book"
          },
          {
            "date": "2019-01-04",
            "Comment": "this is a nice book"
          },
          {
            "date": "2019-01-03",
            "Comment": "this is a nice book"
          }
        ],
        "UPC": 576868345657,
        "Name": "Book2"
      }
    ]
  }
}
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org