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 do I set up an Elasticsearch Yum repository?
- How can I use Elasticsearch and ZFS together?
- How can I use YouTube to learn about Elasticsearch?
- How do I configure the Xms and Xmx settings for Elasticsearch?
- How do I configure Elasticsearch shards?
- How can I use Elasticsearch and Zabbix together for software development?
- How can I use Elasticsearch and MongoDB together to optimize my software development process?
- What is Elasticsearch and how is it used?
- How do I use an Elasticsearch keystore?
- How can I use regular expressions with Elasticsearch?
See more codes...