Skip to content

Instantly share code, notes, and snippets.

@zmoog
Last active June 14, 2022 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmoog/08788876b5de37fed6f897435d833b58 to your computer and use it in GitHub Desktop.
Save zmoog/08788876b5de37fed6f897435d833b58 to your computer and use it in GitHub Desktop.
//
// Sample C# code to index a document in Elasticsearch v7 and v8 using the v7 client.
//
// Install required libraries:
// $ dotnet add package NEST
//
// Enable v8 compatibility mode:
// $ export ELASTIC_CLIENT_APIVERSIONING=true
//
// Run!
// $ dotnet run
//
using Elasticsearch.Net;
using Nest;
public class Person
{
[PropertyName("id")]
public int Id { get; set; }
[PropertyName("first_name")]
public string FirstName { get; set; }
[PropertyName("last_name")]
public string LastName { get; set; }
public Person()
{
Random rnd = new Random();
Id = rnd.Next();
FirstName = "f";
LastName = "l";
}
static void Main(string[] args)
{
var uri = "https://<whatever>.eastus2.azure.elastic-cloud.com:443";
var apiKey = "<base64 encoded api key>";
var connectionSettings = new ConnectionSettings(new Uri(uri))
.ApiKeyAuthentication(new ApiKeyAuthenticationCredentials(apiKey))
.DefaultIndex("people")
.PrettyJson()
.EnableDebugMode();
var client = new ElasticClient(connectionSettings);
var p = new Person();
var response = client.IndexDocument(p);
Console.Out.WriteLine(response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment