CRUD usage examples
editCRUD usage examples
editThis page helps you to understand how to perform various basic Elasticsearch CRUD (create, read, update, delete) operations using the .NET client. It demonstrates how to create a document by indexing an object into Elasticsearch, read a document back, retrieving it by ID or performing a search, update one of the fields in a document and delete a specific document.
These examples assume you have an instance of the ElasticsearchClient
accessible via a local variable named client
and several using directives in
your C# file.
using System; using Elastic.Clients.Elasticsearch; using Elastic.Clients.Elasticsearch.QueryDsl; var client = new ElasticsearchClient();
The default constructor, assumes an unsecured Elasticsearch server is running and exposed on http://localhost:9200. See connecting for examples of connecting to secured servers and Elastic Cloud deployments. |
The examples operate on data representing tweets. Tweets are modelled in the client application using a C# class named Tweet containing several properties that map to the document structure being stored in Elasticsearch.
public class Tweet { public int Id { get; set; } public string User { get; set; } public DateTime PostDate { get; set; } public string Message { get; set; } }
By default, the .NET client will try to find a property called |
Indexing a document
editDocuments can be indexed by creating an instance representing a tweet and indexing it via the client. In these examples, we will work with an index named my-tweet-index.
var tweet = new Tweet { Id = 1, User = "stevejgordon", PostDate = new DateTime(2009, 11, 15), Message = "Trying out the client, so far so good?" }; var response = await client.IndexAsync(tweet, "my-tweet-index"); if (response.IsValidResponse) { Console.WriteLine($"Index document with ID {response.Id} succeeded."); }
Create an instance of the |
|
Prefer the async APIs, which require awaiting the response. |
|
Check the |
|
Access the |
Getting a document
editvar response = await client.GetAsync<Tweet>(1, idx => idx.Index("my-tweet-index")); if (response.IsValidResponse) { var tweet = response.Source; }
The |
|
The original document is deserialized as an instance of the Tweet class,
accessible on the response via the |
Searching for documents
editThe client exposes a fluent interface and a powerful query DSL for searching.
var response = await client.SearchAsync<Tweet>(s => s .Index("my-tweet-index") .From(0) .Size(10) .Query(q => q .Term(t => t.User, "stevejgordon") ) ); if (response.IsValidResponse) { var tweet = response.Documents.FirstOrDefault(); }
The generic type argument specifies the |
|
The index can be omitted if a |
|
Execute a term query against the |
|
Documents matched by the query are accessible via the |
You may prefer using the object initializer syntax for requests if lambdas aren’t your thing.
var request = new SearchRequest("my-tweet-index") { From = 0, Size = 10, Query = new TermQuery("user") { Value = "stevejgordon" } }; var response = await client.SearchAsync<Tweet>(request); if (response.IsValidResponse) { var tweet = response.Documents.FirstOrDefault(); }
Create an instance of |
|
Pass the request to the |
Updating documents
editDocuments can be updated in several ways, including by providing a complete replacement for an existing document ID.
tweet.Message = "This is a new message"; var response = await client.UpdateAsync<Tweet, Tweet>("my-tweet-index", 1, u => u .Doc(tweet)); if (response.IsValidResponse) { Console.WriteLine("Update document succeeded."); }
Update a property on the existing tweet instance. |
|
Send the updated tweet object in the update request. |
Deleting documents
editDocuments can be deleted by providing the ID of the document to remove.
var response = await client.DeleteAsync("my-tweet-index", 1); if (response.IsValidResponse) { Console.WriteLine("Delete document succeeded."); }