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 can I use Elasticsearch and ZFS together?
- How do I set up an Elasticsearch Yum repository?
- How can I use Elasticsearch and Zookeeper together to manage distributed applications?
- How can I use Elasticsearch and Zabbix together for software development?
- How do I use an elasticsearch query builder?
- How can I check the status of a yellow index in Elasticsearch?
- How can I use Elasticsearch with Zammad?
- How can I use Elasticsearch to diagnose "yellow" issues?
- How can I set up and use Elasticsearch on the Yandex Cloud platform?
See more codes...