Skip to content

Instantly share code, notes, and snippets.

@russcam
Last active June 14, 2017 06:45
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 russcam/a427dadb7f5429768450fc8d2118103f to your computer and use it in GitHub Desktop.
Save russcam/a427dadb7f5429768450fc8d2118103f to your computer and use it in GitHub Desktop.
void Main()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var defaultIndex = "default-index";
var connectionSettings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex)
.PrettyJson()
.DisableDirectStreaming()
.OnRequestCompleted(response =>
{
if (response.RequestBodyInBytes != null)
{
Console.WriteLine(
$"{response.HttpMethod} {response.Uri} \n" +
$"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
}
else
{
Console.WriteLine($"{response.HttpMethod} {response.Uri}");
}
Console.WriteLine();
if (response.ResponseBodyInBytes != null)
{
Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
$"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
$"{new string('-', 30)}\n");
}
else
{
Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
$"{new string('-', 30)}\n");
}
});
var client = new ElasticClient(connectionSettings);
if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);
client.CreateIndex(defaultIndex, c => c
.Mappings(m => m
.Map<Message>(mm => mm
.AutoMap()
)
)
);
client.IndexMany(new[] {
new Message { Content = "This is a test message" },
new Message { Content = "Another one" },
new Message { Content = "Yet another one" }
});
client.Refresh(defaultIndex);
var searchResponse = client.Search<Message>(s => s
.FilterPath("hits.hits._source")
.Index(defaultIndex)
.AllTypes()
.Query(q => q
.MatchAll()
)
);
}
public class Message
{
public string Content { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment