I have a Dataset for which I don't know or have the type in advance, nor the number of properties or their type.
On execution, I obtain for that Dataset a DatasetSchema that contain the names, types and some flags for the properties.
For geometry properties I have their GeoJson representation stored as string and I have some flags (isGeoShape, isGeoPoint) that tell the ES property type.
I'm also using NetTopologySuite if it's needed to parse those GeoJsons to actual Geometry objects, but i rather not do this extra parsing and use the GeoJson strings instead.
class DatasetSchema {
List<DatasetField> Fields;
}
class DatasetField {
string Name;
Type DataType;
bool isGeoShape;
bool isGeoPoint;
}
Questions:
How can I create such an ES index with unknown / dynamic mappings schema with NEST high level client with those geometry properties?
How can I Bulk index those documents with NEST high level client with Bulk or BulkAll APIs with those geometry properties?
I saw here and here that the bulk indexing might be done with BulkDescriptor:
dynamic obj = new System.Dynamic.ExpandoObject();
// ….
var descriptor = new BulkDescriptor();
foreach (var doc in values)
{
descriptor.Index<object>(i => i
.Index("abc")
.Id((Id)doc.Id)
.Document((object)doc));
}
client.Bulk(descriptor);
Still, I’m curious how geometry types should be treated?
Thank you very much! Any thoughts or suggestions are welcomed!
Dynamic templates will be a good fit for your use case, this feature gives you a great way to control how elasticsearch maps your dynamic data schema.
You can leverage match parameter and control field type based on field name. If the instance of
DatasetFieldhasIsGeoPointset totruewe can prefix elasticsearch field name with GeoPoint and configure dynamic template to creategoe_pointfield for names prefixed with GeoPointHere is a sample C# app showing it in action
This will produce following elasticsearch mapping
When it comes to bulk indexing documents, the easiest way is to use
IndexManyAsyncextension methodPlease also have a look at this blog post describing indexing multiple documents in detail. Check "Multiple documents" section.
UPDATE: add new dynamic template to a mapping with existing dynamic templates