【问题标题】:Indexing geojson as object into geoshape worked with Nest 5.0.1, but does not with Nest 6.4.2?将geojson作为对象索引到geoshape中适用于Nest 5.0.1,但不适用于Nest 6.4.2?
【发布时间】:2019-01-22 15:30:51
【问题描述】:

我们正在从 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。

【问题讨论】:

  • 您能否提供一个完整、最小且可验证的示例来复制您所看到的内容(请参阅常见问题解答:stackoverflow.com/help/mcve)?我怀疑在这种情况下object 是Json.NET JObject 类型,客户端不知道如何专门处理它,而没有连接JsonNetSerializer。如果你能提供一个完整的例子,我将能够提供进一步的帮助

标签: c# elasticsearch nest


【解决方案1】:

它确实是一个 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; }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-23
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多