# Trying to update documents using NEST in C#

**URL:** <https://discuss.elastic.co/t/trying-to-update-documents-using-nest-in-c/250626>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [October 1, 2020, 9:01am UTC](https://discuss.elastic.co/t/trying-to-update-documents-using-nest-in-c/250626 "2020-10-01T09:01:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adiksl](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@adiksl](https://discuss.elastic.co/u/adiksl)\
**Post date:** [October 1, 2020, 9:01am UTC](https://discuss.elastic.co/t/trying-to-update-documents-using-nest-in-c/250626/1 "2020-10-01T09:01:50Z")

</div>

From within a C# winform application I am writing to elasticsearch which is working successfully. This is fine for my audit process but there is a requirement to write notes data to elasticsearch to enable text searching. Previously this data was stored in a MySQL database and updating notes record was straight forward by using the SQL query below but I struggling to find an elasticsearch equivalent as just indexing a document creates multiple entries for the same id.

```
UPDATE table SET details = 'updated details' WHERE id = '1';

```

All data is written using the following structure:

```auto
    public class TestNotesRecord
    {
        public int TestNotes_Id { get; set; }
        public DateTime TestNotes_DateTime { get; set; }
        public string TestNotes_UserId { get; set; }
        public string TestNotes_Details { get; set; }
    }

```

Then this is written to elasticsearch using this function:

```auto
        public bool SaveTestNotes(TestNotesRecord thisRecord, ref string message)
        {
            bool success = true;
            // Connect to Elastic Search
            string elasticStatus = "";
            var uri = new Uri(ElasticHost);
            var settings = new ConnectionSettings(uri);
            try
            {
                settings.ThrowExceptions(alwaysThrow: true);
                settings.PrettyJson();
                Client = new ElasticClient(settings);
                var response = Client.Cluster.Health();
                elasticStatus = response.Status.ToString();

                settings.DefaultIndex("testnotes");

                // Add Record to Elastic Search
                var indexResponse = Client.IndexDocument(thisRecord);
            }
            catch (Exception excp)
            {
                message = "Exception: " + excp.Message;
                success = false;
            }

            return success;
        }

```

From within Kibana I've noticed each record has a unique \_id but I can find how to retrieve that value to only update that specific document.

---

<div class="post-metadata">

**Author:** ![adiksl](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@adiksl](https://discuss.elastic.co/u/adiksl)\
**Post date:** [October 12, 2020, 3:08pm UTC](https://discuss.elastic.co/t/trying-to-update-documents-using-nest-in-c/250626/2 "2020-10-12T15:08:52Z")

</div>

By adding above the class definition:

```auto
[ElasticsearchType(RelationName = "TestNotes", IdProperty = "TestNotes_Id")]

```

This seems to allow updates to occur. The same code is used to write the document to elasticsearch as it appears that if the "\_id" already exists then the record is updated or if it doesn't exist then the document is added.

When querying in Kibana, both the "\_id" and "TestNotes\_Id" fields hold the same value, where as previously the "\_id" for have had a random unique key assigned to it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 9, 2020, 3:09pm UTC](https://discuss.elastic.co/t/trying-to-update-documents-using-nest-in-c/250626/3 "2020-11-09T15:09:17Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
