Add a document to ElasticSearch using NEST and PowerShell

Create the PowerShell script addperson.ps1. Make sure you have the necessary DLLs for NEST, Elasticsearch.NET and Newtonsoft.Json on hand.


$uri = New-Object System.Uri("http://localhost:9200");
$settings = New-Object Nest.ConnectionSettings($uri, "acme");
$client = New-Object Nest.ElasticClient($settings);
Add-Type -Language CSharp @"
public class Person
    public string Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string City {get;set;}
$person = new-object Person;
$person.Id = "1";
$person.Firstname = "Alan";
$person.Lastname = "Smithee";
$person.City = "Arakis";
$indexRequest = new-object "Nest.IndexRequest[Person]"($person);
$index = $client.Index($indexRequest);


1. Open a PowerShell
2. execute

> powershell .\addperson.ps1

Note: Run the script in its own application context otherwise you have to re-open a new PowerShell window after changes to the person contract.

Leave a Reply

Your email address will not be published.


Please type the text above: