Last active
August 10, 2018 07:38
-
-
Save russcam/bd2e587e33324263aaeaa561085d4e56 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
// Using NEST 6.2.0 with NEST.JsonNetSerializer 6.2.0 | |
private static void Main() | |
{ | |
var defaultIndex = "documents"; | |
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); | |
var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection(), (builtin, settings) => | |
new JsonNetSerializer(builtin, settings, contractJsonConverters: new JsonConverter[] { new StringEnumConverter() })) | |
.DefaultIndex(defaultIndex) | |
.DisableDirectStreaming() | |
.PrettyJson() | |
.OnRequestCompleted(callDetails => | |
{ | |
if (callDetails.RequestBodyInBytes != null) | |
{ | |
Console.WriteLine( | |
$"{callDetails.HttpMethod} {callDetails.Uri} \n" + | |
$"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}"); | |
} | |
else | |
{ | |
Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}"); | |
} | |
Console.WriteLine(); | |
if (callDetails.ResponseBodyInBytes != null) | |
{ | |
Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" + | |
$"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" + | |
$"{new string('-', 30)}\n"); | |
} | |
else | |
{ | |
Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" + | |
$"{new string('-', 30)}\n"); | |
} | |
}); | |
var client = new ElasticClient(connectionSettings); | |
var doc = new MyDocument(1) | |
{ | |
Polygon = new PolygonGeoShape(new[] { | |
new[] | |
{ | |
new GeoCoordinate(1, 2), | |
new GeoCoordinate(3, 4) | |
} | |
}) | |
}; | |
client.IndexDocument(doc); | |
} | |
public class MyDocument | |
{ | |
public MyDocument(int id) => Id = id; | |
public int Id { get; set; } | |
public PolygonGeoShape Polygon { get; set; } | |
} | |
/* | |
outputs | |
PUT http://localhost:9200/documents/mydocument/1?pretty=true | |
{ | |
"id": 1, | |
"polygon": { | |
"coordinates": [ | |
[ | |
[ | |
2.0, | |
1.0 | |
], | |
[ | |
4.0, | |
3.0 | |
] | |
] | |
], | |
"type": "polygon" | |
} | |
} | |
Status: 200 | |
------------------------------ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment