Last active
June 14, 2022 08:31
-
-
Save zmoog/08788876b5de37fed6f897435d833b58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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