elasticsearchHow can I use Elasticsearch with C#?
Elasticsearch can be used with C# via the official low-level client library NEST.
NEST provides a strongly-typed query DSL that maps 1:1 with the Elasticsearch query DSL, and takes advantage of specific C# features such as covariant results and auto mapping of POCO types.
Example code to connect to an Elasticsearch cluster, index a document, and search for it:
var defaultIndex = "my-index";
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
// Index a document
var person = new Person { Id = 1, Name = "Martijn Laarman" };
var indexResponse = client.IndexDocument(person);
// Search for the document
var searchResponse = client.Search<Person>(s => s
.Query(q => q
.Term(f => f.Name, person.Name)
)
);
// Output the total number of documents found
Console.WriteLine($"{searchResponse.Total} documents found");
Output example
1 documents found
The code above does the following:
- Creates a
ConnectionSettings
object, which specifies the connection parameters to the Elasticsearch cluster. - Creates an
ElasticClient
object, using theConnectionSettings
. - Indexes a
Person
object, which is a custom POCO type. - Searches for the document using a
Term
query on theName
field. - Outputs the total number of documents found.
Helpful links
More of Elasticsearch
- How can I use elasticsearch zone awareness to improve my software development?
- How do I use Elasticsearch with ZGC?
- How can I use Elasticsearch with Zammad?
- How do I set up an Elasticsearch Yum repository?
- How do I use ElasticSearch to zip files?
- How can I set up and use Elasticsearch on the Yandex Cloud platform?
- How do I configure elasticsearch xpack.security.transport.ssl?
- How can I use Elasticsearch to diagnose "yellow" issues?
- How can I use Yandex Mirror to access Elasticsearch data?
- How can I use YouTube to learn about Elasticsearch?
See more codes...