This page helps you to understand how to perform various basic {es} CRUD (create, read, update, delete) operations using the .NET client. It demonstrates how to create a document by indexing an object into {es}, 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.
link:{doc-tests-src}/Usage/CrudExamplesTests.cs[role=include]
-
The default constructor, assumes an unsecured {es} 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 {es}.
link:{doc-tests-src}/Usage/CrudExamplesTests.cs[role=include]
-
By default, the .NET client will try to find a property called
Id
on the class. When such a property is present it will index the document into {es} using the ID specified by the value of this property.
Documents 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'.
link:{doc-tests-src}/Usage/CrudExamplesTests.cs[role=include]
-
Create an instance of the
Tweet
class with relevant properties set. -
Prefer the async APIs, which require awaiting the response.
-
Check the
IsValid
property on the response to confirm that the request and operation succeeded. -
Access the
IndexResponse
properties, such as the ID, if necessary.
link:{doc-tests-src}/Usage/CrudExamplesTests.cs[role=include]
-
The
GetResponse
is mapped 1-to-1 with the Elasticsearch JSON response. -
The original document is deserialized as an instance of the Tweet class, accessible on the response via the
Source
property.
The client exposes a fluent interface and a powerful query DSL for searching.
link:{doc-tests-src}/Usage/CrudExamplesTests.cs[role=include]
-
The generic type argument specifies the
Tweet
class, which is used when deserialising the hits from the response. -
The index can be omitted if a
DefaultIndex
has been configured onElasticsearchClientSettings`
, or a specific index was configured when mapping this type. -
Execute a term query against the
user
field, searching for tweets authored by the user 'stevejgordon'. -
Documents matched by the query are accessible via the
Documents
collection property on theSearchResponse
.
You may prefer using the object initializer syntax for requests if lambdas aren’t your thing.
link:{doc-tests-src}/Usage/CrudExamplesTests.cs[role=include]
-
Create an instance of
SearchRequest
, setting properties to control the search operation. -
Pass the request to the
SearchAsync
method on the client.
Documents can be updated in several ways, including by providing a complete replacement for an existing document ID.
link:{doc-tests-src}/Usage/CrudExamplesTests.cs[role=include]
-
Update a property on the existing tweet instance.
-
Send the updated tweet object in the update request.
Documents can be deleted by providing the ID of the document to remove.
link:{doc-tests-src}/Usage/CrudExamplesTests.cs[role=include]