如何使用 NEST(C#) 在 Elasticsearch 中 upload/index 一个 GeoJson 文件

How to upload/index a GeoJson file in Elasticsearch using NEST(C#)

我有 GeoJson 文件,我想通过 NEST 在 Elastic Search 上建立索引 但是由于缺乏专业知识,我在索引文档时遇到了麻烦 我创建了一个 class 代表 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": [
                        [
                            []
                        ],
                        [
                            [
                                [
                                    [
                                        [],
                                        []
                                    ],
                                    [
                                        [],
                                        []
                                    ],
                                    [
                                        [],
                                        []
                                    ],
                                    [
                                        [],
                                        []
                                    ]
                                ]
                            ]
                        ]
                    ]
                }
            }

为了正确保存 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
                                ]
                            ]
                        ]
                    }
                }
            }
        ]
    }
}