我们正在从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。
答案 0 :(得分:0)
它确实是一个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; }
}
}
}