2

Hey I trying to update existing document of ElasticSearch, I found a cURL code from Elasticsearch site Note: Sam type with 2 document is already exists I just want to update a existing field

POST /EmployeeIndex/Sam/2/_update
{
   "doc" : {
      "Nested" : true,
      "views": 0
   }
}

Its working perfectly how I need but please help me to convert it to NEST, as i working on .NET, I managed to write a code

 var responseUpdate = client.Update<clsEmployeeElasticSearch, object>(u => u
               .Index("EmployeeIndex")
                .Type("Sam")
                    .Id(2)
                    .Doc(new { Nested= true })
                    .RetryOnConflict(3)
                    .Refresh());

But it always creating a new field in my document instead of updating existing one. Please see attached screenshot with a codeenter image description here Please help guys.

Dipesh
  • 371
  • 1
  • 4
  • 20

2 Answers2

3

What you need is a PartialUpdate. Applied on your example, the following code should do what you expect.

    var responseUpdate = client.Update<clsEmployeeElasticSearch, object>(u => u
         .Index("EmployeeIndex")
         .Type("Sam")
         .Id(2)
         .Doc(new {IsActive ="true", Views="0"})
         .DocAsUpsert()
     );
Val
  • 207,596
  • 13
  • 358
  • 360
  • Hey @val thanx for your quick reply, I tried your code and it added `IsActive` and `views` in my `Sam` document – Dipesh Feb 26 '16 at 13:59
  • Which is what you expected, right? i.e. it added the fields in your Sam document with ID 2. On the next execution, it will simply update those fields. – Val Feb 26 '16 at 14:00
  • `Sam` Type with `2` document is already exists I just want to update it @Val – Dipesh Feb 26 '16 at 14:02
  • I'm not sure I understand what has been updated/added then. If the document with ID 2 already exists, then it should have been simply updated. Can you show the document with ID 2, before and after runnign the code? – Val Feb 26 '16 at 14:03
  • 2
    Oh I see "Nested" vs "nested", maybe try to write this instead `.Doc(new {"nested" = "true", "views"="0"})` or simply [disable camel-casing](http://stackoverflow.com/questions/27282865/how-to-disable-camel-casing-elasticsearch-field-names-in-nest) – Val Feb 26 '16 at 14:26
  • 1
    Thanks, I'm glad you figured it out. – Val Feb 26 '16 at 14:39
3

Is it possible that you are already there but be just facing a casing missmatch issue? see from Nest reference:

Property Name Inference In many places NEST allows you to pass property names and JSON paths as C# expressions, i.e:

.Query(q=>q .Term(p=>p.Followers.First().FirstName, "martijn")) NEST by default will camelCase properties. So the FirstName property above will be translated to "followers.firstName".

This can be configured by setting

settings.SetDefaultPropertyNameInferrer(p=>p); This will leave property names untouched.

Properties marked with [ElasticAttibute(Name="")] or [JsonProperty(Name="")] will pass the configured name verbatim.

... Note that you are creating a dynamic object for the update so, i belive attributes might not a be a solution if you keep it that way

macebalp
  • 186
  • 9