Monthly Archives: February 2015

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.

addperson.ps1:

[Reflection.Assembly]::LoadFile("C:\estest\Nest.dll")
[Reflection.Assembly]::LoadFile("C:\estest\Elasticsearch.Net.dll")
[Reflection.Assembly]::LoadFile("C:\estest\Newtonsoft.Json.dll")
 
$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);

Usage:

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.