简体   繁体   English

将geojson作为对象索引到geoshape中,可以在Nest 5.0.1中使用,但不能在Nest 6.4.2中使用吗?

[英]Indexing geojson as object into geoshape worked with Nest 5.0.1, but does not with Nest 6.4.2?

We are upgrading from elasticsearch 5.2 to elasticsearch 6.4.2, and therefore also from Nest 5.0.1 to Nest 6.4.2. 我们正在从Elasticsearch 5.2升级到Elasticsearch 6.4.2,因此也从Nest 5.0.1升级到Nest 6.4.2。 In 5.0.1 we could index geoJSON data as an object, but Nest 6.4.2 generates a request containing geoJSON without data. 在5.0.1中,我们可以将geoJSON数据索引为一个对象,但是Nest 6.4.2会生成一个包含geoJSON的请求,而没有数据。

We index a field with geographic data in geoJSON format to a geoshape field in elasticsearch like this: 我们将带有geoJSON格式的地理数据的字段索引到elasticsearch中的geoshape字段,如下所示:

In class GeoDocument: 在GeoDocument类中:

[Nest.Text(Name = "field1")]
public string Field1 { get; set; }
[Nest.GeoShape(Name = "geometrie")]
public object Geometrie { get; set; }

Data: 数据:

string polygon = "{\"type\":\"Polygon\",\"coordinates\":[[[5.856956,51.002753],[5.856928,51.002771],[5.856687,51.002853],[5.856956,51.002753]]]}";  

Serialize data to object: 将数据序列化为对象:

Geometrie = JsonConvert.DeserializeObject<object>(polygon);

Index document in Nest 5.0.1 (worked fine): Nest 5.0.1中的索引文档(工作正常):

var response = this.ElasticClient.Index<T>(geoDocument);

Index document in Nest 6.4.2: Nest 6.4.2中的索引文档:

var response = this.ElasticClient.IndexDocument<T>(geoDocument);

The request should be like: 该请求应类似于:

{"field1":"correct content","geometrie":{"type":"Polygon","coordinates"::[[[5.856956,51.002753],[5.856928,51.002771],[5.856687,51.002853],[5.856956,51.002753]]]}}

But Nest generates a request like: 但是Nest会生成类似以下的请求:

{"field1":"correct content","geometrie":{"type":[],"coordinates":[[[[],[]],[[],[]],[[],[]],[[],[]]]]}}

Response: 响应:

{"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}

We don't inject a SourceSerializerFactory in the connectionsettings. 我们不在连接设置中注入SourceSerializerFactory。

It is a Json.NET JObject indeed. 它确实是一个Json.NET JObject。 The ConnectionSettings where created without a SourceSerializerFactory. 在没有SourceSerializerFactory的情况下创建的ConnectionSettings。 Setting JsonNetSerializer.Default as SourceSerializerFactory solves the problem, thanks! 将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM