Skip to content

Instantly share code, notes, and snippets.

@russcam
Created January 12, 2021 00:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russcam/5c40e5c6fb95b9afd9242df675fce428 to your computer and use it in GitHub Desktop.
Save russcam/5c40e5c6fb95b9afd9242df675fce428 to your computer and use it in GitHub Desktop.
Using NetTopologySuite Geometries with NEST 7.x
<Query Kind="Program">
<NuGetReference>Elasticsearch.Net</NuGetReference>
<NuGetReference>NEST</NuGetReference>
<NuGetReference>NEST.JsonNetSerializer</NuGetReference>
<NuGetReference>NetTopologySuite.IO.GeoJSON</NuGetReference>
<Namespace>Elasticsearch.Net</Namespace>
<Namespace>Elasticsearch.Net.Specification.CatApi</Namespace>
<Namespace>Nest</Namespace>
<Namespace>Nest.JsonNetSerializer</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Security.Cryptography.X509Certificates</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>NetTopologySuite.IO.Converters</Namespace>
<Namespace>Newtonsoft.Json.Converters</Namespace>
<Namespace>NetTopologySuite.Geometries</Namespace>
</Query>
private static void Main()
{
var defaultIndex = "my_shapes";
var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200"));
var settings = new ConnectionSettings(pool, (c, s) =>
new JsonNetSerializer(c, s, contractJsonConverters: new JsonConverter[] {
new GeometryConverter(),
new CoordinateConverter(),
new StringEnumConverter()
}))
.DefaultIndex(defaultIndex)
.DisableDirectStreaming()
.PrettyJson()
.OnRequestCompleted(callDetails =>
{
if (callDetails.RequestBodyInBytes != null)
{
var json = JObject.Parse(Encoding.UTF8.GetString(callDetails.RequestBodyInBytes));
Console.WriteLine(
$"{callDetails.HttpMethod} {callDetails.Uri} \n" +
$"{json.ToString(Newtonsoft.Json.Formatting.Indented)}");
}
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(settings);
var createIndexResponse = client.Indices.Create(defaultIndex, c => c
.Map<MyDocument>(m => m
.Properties(p => p
.GeoShape(g => g
.Name(n => n.Geometry)
)
)
)
);
if (!createIndexResponse.IsValid)
{
throw new Exception($"Error creating index: {createIndexResponse.DebugInformation}");
}
var polygon = new Polygon(new LinearRing(new [] {
new Coordinate(0, 0),
new Coordinate(0, 4),
new Coordinate(4, 4),
new Coordinate(4, 0),
new Coordinate(0, 0)
}));
var document = new MyDocument(1, polygon);
var indexResponse = client.IndexDocument(document);
if (!indexResponse.IsValid)
{
throw new Exception($"Error indexinf document: {indexResponse.DebugInformation}");
}
}
public class MyDocument
{
public MyDocument(int id, Geometry geometry)
{
Id = id;
Geometry = geometry;
}
public int Id { get; set; }
public Geometry Geometry { get; set; }
}
@florinvirdol
Copy link

florinvirdol commented Jan 13, 2021

Your example worked as expected in the end. I was creating multiple ES clients, and used the old one when indexing.
However, i encountered some other issues when indexing from GeoJson files or string representation (possible some deserialization issues).
And also, some issues when trying to see in Kibana Maps the indexed GeometryCollection documents.
I created an GIST and this StackOverflow question, if / when you'll have some time, i'll really appreciate.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment