Trying to update documents using NEST in C#

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:

    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:

        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.

By adding above the class definition:

[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.

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