[英]Indexing geojson as object into geoshape worked with Nest 5.0.1, but does not with Nest 6.4.2?
我們正在從Elasticsearch 5.2升級到Elasticsearch 6.4.2,因此也從Nest 5.0.1升級到Nest 6.4.2。 在5.0.1中,我們可以將geoJSON數據索引為一個對象,但是Nest 6.4.2會生成一個包含geoJSON的請求,而沒有數據。
我們將帶有geoJSON格式的地理數據的字段索引到elasticsearch中的geoshape字段,如下所示:
在GeoDocument類中:
[Nest.Text(Name = "field1")]
public string Field1 { get; set; }
[Nest.GeoShape(Name = "geometrie")]
public object Geometrie { get; set; }
數據:
string polygon = "{\"type\":\"Polygon\",\"coordinates\":[[[5.856956,51.002753],[5.856928,51.002771],[5.856687,51.002853],[5.856956,51.002753]]]}";
將數據序列化為對象:
Geometrie = JsonConvert.DeserializeObject<object>(polygon);
Nest 5.0.1中的索引文檔(工作正常):
var response = this.ElasticClient.Index<T>(geoDocument);
Nest 6.4.2中的索引文檔:
var response = this.ElasticClient.IndexDocument<T>(geoDocument);
該請求應類似於:
{"field1":"correct content","geometrie":{"type":"Polygon","coordinates"::[[[5.856956,51.002753],[5.856928,51.002771],[5.856687,51.002853],[5.856956,51.002753]]]}}
但是Nest會生成類似以下的請求:
{"field1":"correct content","geometrie":{"type":[],"coordinates":[[[[],[]],[[],[]],[[],[]],[[],[]]]]}}
響應:
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse field [geometrie] of type [geo_shape]"}],"type":"mapper_parsing_exception","reason":"failed to parse field [geometrie] of type [geo_shape]","caused_by":{"type":"illegal_state_exception","reason":"Can't get text on a START_ARRAY at 1:673"}},"status":400}
我們不在連接設置中注入SourceSerializerFactory。
它確實是一個Json.NET JObject。 在沒有SourceSerializerFactory的情況下創建的ConnectionSettings。 將JsonNetSerializer.Default設置為SourceSerializerFactory可以解決此問題,謝謝!
using System;
using Nest;
using Nest.JsonNetSerializer;
using Newtonsoft.Json;
namespace TestIndexing
{
class Program
{
static void Main(string[] args)
{
var indexName = "geodocument";
var connectionPool = new Elasticsearch.Net.SniffingConnectionPool(new Uri[] { new Uri("http://localhost:9200") });
var connectionSettings = new Nest.ConnectionSettings(connectionPool);
connectionSettings.DefaultIndex(indexName);
connectionSettings.DisableDirectStreaming();
var elasticClient = new ElasticClient(connectionSettings);
Func<TypeMappingDescriptor<GeoDocument>, ITypeMapping> typeMapping = m => m
.Dynamic(false)
.Properties(ps => ps
.Keyword(k => k
.Name(n => n.DocId))
.GeoShape(g => g
.PointsOnly(false)
.Name(o => o.GeoField)));
elasticClient.CreateIndex(new CreateIndexDescriptor(indexName).Mappings(ms => ms.Map(typeMapping)));
var polygon = "{\"type\":\"Polygon\",\"coordinates\":[[[5.856956,51.002753],[5.856928,51.002771],[5.856687,51.002853],[5.856956,51.002753]]]}";
var document = new GeoDocument()
{
DocId = "1",
GeoField = JsonConvert.DeserializeObject<object>(polygon),
};
var indexResponse = elasticClient.IndexDocument(document);
Console.WriteLine(indexResponse.DebugInformation);
elasticClient.DeleteIndex(new DeleteIndexRequest(indexName));
Console.ReadKey();
}
[Nest.ElasticsearchType(Name = "geoDocument", IdProperty = "DocId")]
public class GeoDocument
{
[Nest.Keyword(Name = "DocId")]
public string DocId { get; set; }
[Nest.GeoShape(Name = "GeoField")]
public object GeoField { get; set; }
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.