Last active
June 14, 2017 06:45
-
-
Save russcam/a427dadb7f5429768450fc8d2118103f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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