【问题标题】:How to upload/index a GeoJson file in Elasticsearch using NEST(C#)如何使用 NEST(C#) 在 Elasticsearch 中上传/索引 GeoJson 文件
【发布时间】:2019-12-30 23:37:19
【问题描述】:

我有 GeoJson 文件,我想通过 NEST 在 Elastic Search 上编制索引 但由于缺乏专业知识,我在索引文档时遇到了麻烦 我创建了一个代表 ElasticSearch 上的映射的类:

public class GeoDocument
    {
        [Nest.Keyword(Name = "DocId")]
        public string DocId { get; set; }

        [Nest.GeoShape(Name = "GeoField")]
        public object GeoField { get; set; }
    }

但是当我使用这个映射来索引一个文档时


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

var geoDocument = new GeoJson
{
   DocId = "1",
   GeoField = JsonConvert.DeserializeObject<object>(polygon)
};
var IndexResponse = client.IndexDocument(geoDocument);

我得到类似这样的结果

"_source": {
                    "DocId": "1",
                    "GeoField": [
                        [
                            []
                        ],
                        [
                            [
                                [
                                    [
                                        [],
                                        []
                                    ],
                                    [
                                        [],
                                        []
                                    ],
                                    [
                                        [],
                                        []
                                    ],
                                    [
                                        [],
                                        []
                                    ]
                                ]
                            ]
                        ]
                    ]
                }
            }

【问题讨论】:

标签: c# elasticsearch nest


【解决方案1】:

为了使 JObject 正确保存,您必须告诉 ElasticClient 使用 NewtonSoft .Net 序列化程序。

  1. 安装NEST.JsonNetSerializer
  2. 在 ConnectionSettings 中引用 JsonNetSerializer
  3. 如果更改设置后得到 400,则可能需要创建一个新索引。

示例代码

using Nest;
using Elasticsearch.Net;
using Nest.JsonNetSerializer;
    SingleNodeConnectionPool node = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    ConnectionSettings settings = new ConnectionSettings(
        node,
        JsonNetSerializer.Default
    );
    settings.DefaultMappingFor<GeoDocument>(m => m.IndexName("project2"));
    ElasticClient client = new ElasticClient(settings);

    // This is Supposed to be GeoDocument as per your question.
    GeoDocument geoDocument = new GeoDocument 
    {
        DocId = "1",
        GeoField = JObject.Parse(polygon)
        // GeoField = JsonConvert.DeserializeObject<object>(polygon) // <-- Works too.
    };

    IndexResponse IndexResponse = client.IndexDocument(geoDocument);

回应

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "project2",
                "_type": "_doc",
                "_id": "COQRXW8BNG2RJmIOyoO0",
                "_score": 1.0,
                "_source": {
                    "DocId": "1",
                    "GeoField": {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [
                                    5.856956,
                                    51.002753
                                ],
                                [
                                    5.856928,
                                    51.002771
                                ],
                                [
                                    5.856687,
                                    51.002853
                                ],
                                [
                                    5.856956,
                                    51.002753
                                ]
                            ]
                        ]
                    }
                }
            }
        ]
    }
}

【讨论】:

  • 嗨,@Jawad,你能解释一下这条线吗? ` settings.DefaultMappingFor(m => m.IndexName("project2"));` 映射如何存储在设置文件中?谢谢!
  • @FlorinVirdol 设置不是来自文件。它是 ConnectionSettings 的一个属性,它是 ElasticSearch 的一部分
  • 哦,好的,我现在明白了。谢谢你,@Jawad!
  • 嗨,@Jawad!我设法使用您的示例进行摄取,但 ElasticSearch 中的GeoField 属性不是GeoShape 类型,而是具有两个属性(typecoordinates)的对象。如何将GeoField 添加为GeoShape?谢谢!
  • 如何将GeoField 提取为GeoShape?我试过: 1. 你在 ElasticSearch 中的示例 JsonNetSerializer.Default - GeoField ingested 属性不是 GeoShape 类型,而是一个具有两个属性(typecoordinates)的对象 2. 添加了一些 CustomSerializer其中添加了 NTS GeometryConverter),尝试摄取 IFeature.Geometry 但出现错误:Elasticsearch.Net.UnexpectedElasticsearchClientException: Self referencing loop detected for property 'coordinateValue' with type 'NetTopologySuite.Geometries.Coordinate'. Path 'GeoField.coordinate' 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 2020-09-11
  • 2018-05-29
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
相关资源
最近更新 更多