将Wkt(多面)添加到ElasticSearch时解析异常

时间:2019-06-14 08:30:45

标签: elasticsearch .net-core nest wkt

我正在尝试将MySql数据库中的Geolocation数据导入.NETCore中的ElasticSearch(v7.1.1)。

我遇到以下错误:

{"error":{"root_cause":[{"type":"parse_exception","reason":"expected number but found: ','"}],"type":"mapper_parsing_exception","reason":"failed to parse field [shape] of type [geo_shape]","caused_by":"type":"parse_exception","reason":"expected number but found: ','"}},"status":400}

我要导入的WKT是:

MULTIPOLYGON (((4.7579668999999996 46.2765857002975,4.758639999999998 46.277100600297395,4.7580551999999985 46.280358200296845,4.7599586999999985 46.28062440029681,4.760272399999998 46.28449790029614,4.7599307999999985 46.287958500295545,4.760144999999998 46.288988000295376,4.768144999999999 46.287875000295564,4.7791999999999994 46.28661400029579,4.779245999999998 46.28499000029606,4.780515999999999 46.28457600029613,4.784049999999998 46.286391000295815,4.7855029999999985 46.28613300029587,4.790346999999999 46.287064000295715,4.7930329999999985 46.288710000295424,4.7944049999999985 46.29102700029502,4.797415999999999 46.29047700029512,4.798778999999998 46.29134200029497,4.801573999999999 46.291726000294894,4.803376999999999 46.295254000294314,4.803198999999999 46.29630700029412,4.806661999999998 46.30017200029347,4.809911999999998 46.303189000292946,4.810800999999999 46.30498100029263,4.813812999999999 46.30424100029276,4.8173629999999985 46.304849000292656,4.817989999999998 46.307564000292196,4.820322999999998 46.310179000291754,4.820378999999998 46.31125800029156,4.811309999999998 46.31352800029114,4.812275999999999 46.31594300029076,4.807483999999998 46.31819400029035,4.805152999999999 46.321629000289775,4.804822999999998 46.32296000028955,4.801529999999999 46.322797000289576,4.8021715999999985 46.325112200289176,4.806246399999999 46.32550560028909,4.809514599999998 46.32759570028874,4.808897499999998 46.32852310028857,4.808985999999998 46.337877300286955,4.810925599999999 46.33843760028686,4.817702099999998 46.33346470028771,4.8197126999999975 46.33291060028782,4.820696399999998 46.33184060028801,4.829585999999999 46.33015270028829,4.841794399999999 46.32865140028856,4.851587699999999 46.326253800288974,4.844155299999998 46.31659080029065,4.841140899999998 46.312041200291425,4.839071399999999 46.30872580029199,4.837259299999999 46.30501310029262,4.833032899999998 46.30031740029344,4.831669099999999 46.29636310029412,4.8307376999999985 46.29065400029508,4.827784899999999 46.28152960029665,4.827083699999999 46.277739100297296,4.8263492999999995 46.27620600029758,4.823364199999999 46.276666900297485,4.821481199999998 46.2776875002973,4.8202029999999985 46.279495800296985,4.815944299999999 46.279719400296955,4.816172999999998 46.28109010029673,4.813862699999998 46.28104950029673,4.813230299999998 46.278694000297136,4.812089499999999 46.27765110029731,4.8096404999999995 46.277088600297404,4.802514699999998 46.27920020029703,4.801723599999999 46.27802760029725,4.797655999999998 46.276863400297444,4.793134399999998 46.27807170029723,4.790360699999997 46.27778950029728,4.788977799999999 46.27672700029746,4.786570499999998 46.273582200298,4.786097499999999 46.273974500297946,4.777379599999998 46.276076000297586,4.773336299999999 46.276329300297526,4.768414599999998 46.2753243002977,4.763615199999999 46.27526840029772,4.7579668999999996 46.2765857002975)),((4.811962599999998 46.36091350028296,4.815018199999998 46.36176320028281,4.817915499999999 46.36470840028228,4.818443199999999 46.367786800281735,4.819791799999998 46.372192700280976,4.820597999999999 46.37239760028094,4.821574199999999 46.37626310028026,4.831746199999999 46.37724710028009,4.829639499999997 46.379540500279695,4.835752299999999 46.37953070027969,4.835998799999999 46.37886340027982,4.839689299999999 46.378551100279864,4.841294099999998 46.376953800280134,4.8399877999999985 46.37472340028053,4.8415586999999976 46.37357900028072,4.840440699999999 46.37113030028116,4.844790699999998 46.369457500281435,4.845292999999999 46.37050320028126,4.8496498999999975 46.37001720028136,4.850486599999999 46.37130240028113,4.856000099999998 46.3697255002814,4.855948699999998 46.368887400281544,4.8586298999999995 46.36808090028168,4.856130499999999 46.364444700282334,4.853673499999998 46.359393000283205,4.851530099999999 46.356564400283695,4.851010299999999 46.35231710028445,4.846286899999997 46.3531745002843,4.843693699999998 46.35273630028438,4.8407938999999995 46.353536300284226,4.8381178999999985 46.35282770028436,4.836072899999998 46.34999510028486,4.835249299999998 46.34672080028542,4.8337344999999985 46.34738920028531,4.8276113999999986 46.34858320028509,4.822604799999997 46.34887670028505,4.821413699999999 46.34727450028532,4.821292799999999 46.343918400285915,4.817387599999999 46.34563560028562,4.8125165999999995 46.34738810028533,4.8103460999999985 46.348564500285114,4.8050014999999995 46.35464130028404,4.8055528999999995 46.357257900283585,4.808215099999998 46.3594711002832,4.811962599999998 46.36091350028296)))

在这种情况下,它是一个多面体。我们可以通过http://arthur-e.github.io/Wicket/sandbox-gmaps3.html

看到WKT是正确的

我的映射是:

{
  "mapping": {
    "properties": {
      "shape": {
        "type": "geo_shape"
      }
    }
  }
}

我正在使用GeoWKTReader.Read(wkt)函数将WKT转换为GeoShape对象。

以下是复制错误的示例代码:

class Program
    {
        static void Main(string[] args)
        {
            var defaultIndex = "document";

            var shape = GeoWKTReader.Read(
                "MULTIPOLYGON (((4.7579668999999996 46.2765857002975,4.758639999999998 46.277100600297395,4.7580551999999985 46.280358200296845,4.7599586999999985 46.28062440029681,4.760272399999998 46.28449790029614,4.7599307999999985 46.287958500295545,4.760144999999998 46.288988000295376,4.768144999999999 46.287875000295564,4.7791999999999994 46.28661400029579,4.779245999999998 46.28499000029606,4.780515999999999 46.28457600029613,4.784049999999998 46.286391000295815,4.7855029999999985 46.28613300029587,4.790346999999999 46.287064000295715,4.7930329999999985 46.288710000295424,4.7944049999999985 46.29102700029502,4.797415999999999 46.29047700029512,4.798778999999998 46.29134200029497,4.801573999999999 46.291726000294894,4.803376999999999 46.295254000294314,4.803198999999999 46.29630700029412,4.806661999999998 46.30017200029347,4.809911999999998 46.303189000292946,4.810800999999999 46.30498100029263,4.813812999999999 46.30424100029276,4.8173629999999985 46.304849000292656,4.817989999999998 46.307564000292196,4.820322999999998 46.310179000291754,4.820378999999998 46.31125800029156,4.811309999999998 46.31352800029114,4.812275999999999 46.31594300029076,4.807483999999998 46.31819400029035,4.805152999999999 46.321629000289775,4.804822999999998 46.32296000028955,4.801529999999999 46.322797000289576,4.8021715999999985 46.325112200289176,4.806246399999999 46.32550560028909,4.809514599999998 46.32759570028874,4.808897499999998 46.32852310028857,4.808985999999998 46.337877300286955,4.810925599999999 46.33843760028686,4.817702099999998 46.33346470028771,4.8197126999999975 46.33291060028782,4.820696399999998 46.33184060028801,4.829585999999999 46.33015270028829,4.841794399999999 46.32865140028856,4.851587699999999 46.326253800288974,4.844155299999998 46.31659080029065,4.841140899999998 46.312041200291425,4.839071399999999 46.30872580029199,4.837259299999999 46.30501310029262,4.833032899999998 46.30031740029344,4.831669099999999 46.29636310029412,4.8307376999999985 46.29065400029508,4.827784899999999 46.28152960029665,4.827083699999999 46.277739100297296,4.8263492999999995 46.27620600029758,4.823364199999999 46.276666900297485,4.821481199999998 46.2776875002973,4.8202029999999985 46.279495800296985,4.815944299999999 46.279719400296955,4.816172999999998 46.28109010029673,4.813862699999998 46.28104950029673,4.813230299999998 46.278694000297136,4.812089499999999 46.27765110029731,4.8096404999999995 46.277088600297404,4.802514699999998 46.27920020029703,4.801723599999999 46.27802760029725,4.797655999999998 46.276863400297444,4.793134399999998 46.27807170029723,4.790360699999997 46.27778950029728,4.788977799999999 46.27672700029746,4.786570499999998 46.273582200298,4.786097499999999 46.273974500297946,4.777379599999998 46.276076000297586,4.773336299999999 46.276329300297526,4.768414599999998 46.2753243002977,4.763615199999999 46.27526840029772,4.7579668999999996 46.2765857002975)),((4.811962599999998 46.36091350028296,4.815018199999998 46.36176320028281,4.817915499999999 46.36470840028228,4.818443199999999 46.367786800281735,4.819791799999998 46.372192700280976,4.820597999999999 46.37239760028094,4.821574199999999 46.37626310028026,4.831746199999999 46.37724710028009,4.829639499999997 46.379540500279695,4.835752299999999 46.37953070027969,4.835998799999999 46.37886340027982,4.839689299999999 46.378551100279864,4.841294099999998 46.376953800280134,4.8399877999999985 46.37472340028053,4.8415586999999976 46.37357900028072,4.840440699999999 46.37113030028116,4.844790699999998 46.369457500281435,4.845292999999999 46.37050320028126,4.8496498999999975 46.37001720028136,4.850486599999999 46.37130240028113,4.856000099999998 46.3697255002814,4.855948699999998 46.368887400281544,4.8586298999999995 46.36808090028168,4.856130499999999 46.364444700282334,4.853673499999998 46.359393000283205,4.851530099999999 46.356564400283695,4.851010299999999 46.35231710028445,4.846286899999997 46.3531745002843,4.843693699999998 46.35273630028438,4.8407938999999995 46.353536300284226,4.8381178999999985 46.35282770028436,4.836072899999998 46.34999510028486,4.835249299999998 46.34672080028542,4.8337344999999985 46.34738920028531,4.8276113999999986 46.34858320028509,4.822604799999997 46.34887670028505,4.821413699999999 46.34727450028532,4.821292799999999 46.343918400285915,4.817387599999999 46.34563560028562,4.8125165999999995 46.34738810028533,4.8103460999999985 46.348564500285114,4.8050014999999995 46.35464130028404,4.8055528999999995 46.357257900283585,4.808215099999998 46.3594711002832,4.811962599999998 46.36091350028296)))");

            var document = new Document
            {
                Shape = shape
            };

            var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
                .DefaultIndex(defaultIndex)
                .DisableDirectStreaming()
                .PrettyJson();

            var client = new ElasticClient(settings);

            if (client.Indices.Exists(defaultIndex).Exists)
                client.Indices.Delete(defaultIndex);

            client.Indices.Create(defaultIndex, c => c
                .Map<Document>(m => m.AutoMap())
            );

            var response = client.Index(document, i => i.Refresh(Refresh.WaitFor));
        }
    }

这是Russ在笔记本电脑上提供的示例结果(带有法语的Windows)

HEAD http://localhost:9200/shapes_example?pretty=true

Status: 404

------------------------------

PUT http://localhost:9200/shapes_example?pretty=true
{"mappings":{"properties":{"shape":{"type":"geo_shape"}}}}

Status: 200
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "shapes_example"
}

------------------------------

POST http://localhost:9200/shapes_example/_doc?pretty=true&refresh=wait_for
{"shape":"MULTIPOLYGON (((4,7579669 46,2765857002975, 4,75864 46,2771006002974, 4,7580552 46,2803582002968, 4,7599587 46,2806244002968, 4,7602724 46,2844979002961, 4,7599308 46,2879585002955, 4,760145 46,2889880002954, 4,768145 46,2878750002956, 4,
7792 46,2866140002958, 4,779246 46,2849900002961, 4,780516 46,2845760002961, 4,78405 46,2863910002958, 4,785503 46,2861330002959, 4,790347 46,2870640002957, 4,793033 46,2887100002954, 4,794405 46,291027000295, 4,797416 46,2904770002951, 4,798779 46
,291342000295, 4,801574 46,2917260002949, 4,803377 46,2952540002943, 4,803199 46,2963070002941, 4,806662 46,3001720002935, 4,809912 46,3031890002929, 4,810801 46,3049810002926, 4,813813 46,3042410002928, 4,817363 46,3048490002927, 4,81799 46,307564
0002922, 4,820323 46,3101790002918, 4,820379 46,3112580002916, 4,81131 46,3135280002911, 4,812276 46,3159430002908, 4,807484 46,3181940002903, 4,805153 46,3216290002898, 4,804823 46,3229600002895, 4,80153 46,3227970002896, 4,8021716 46,325112200289
2, 4,8062464 46,3255056002891, 4,8095146 46,3275957002887, 4,8088975 46,3285231002886, 4,808986 46,337877300287, 4,8109256 46,3384376002869, 4,8177021 46,3334647002877, 4,8197127 46,3329106002878, 4,8206964 46,331840600288, 4,829586 46,330152700288
3, 4,8417944 46,3286514002886, 4,8515877 46,326253800289, 4,8441553 46,3165908002906, 4,8411409 46,3120412002914, 4,8390714 46,308725800292, 4,8372593 46,3050131002926, 4,8330329 46,3003174002934, 4,8316691 46,2963631002941, 4,8307377 46,2906540002
951, 4,8277849 46,2815296002967, 4,8270837 46,2777391002973, 4,8263493 46,2762060002976, 4,8233642 46,2766669002975, 4,8214812 46,2776875002973, 4,820203 46,279495800297, 4,8159443 46,279719400297, 4,816173 46,2810901002967, 4,8138627 46,2810495002
967, 4,8132303 46,2786940002971, 4,8120895 46,2776511002973, 4,8096405 46,2770886002974, 4,8025147 46,279200200297, 4,8017236 46,2780276002972, 4,797656 46,2768634002974, 4,7931344 46,2780717002972, 4,7903607 46,2777895002973, 4,7889778 46,27672700
02975, 4,7865705 46,273582200298, 4,7860975 46,2739745002979, 4,7773796 46,2760760002976, 4,7733363 46,2763293002975, 4,7684146 46,2753243002977, 4,7636152 46,2752684002977, 4,7579669 46,2765857002975)), ((4,8119626 46,360913500283, 4,8150182 46,36
17632002828, 4,8179155 46,3647084002823, 4,8184432 46,3677868002817, 4,8197918 46,372192700281, 4,820598 46,3723976002809, 4,8215742 46,3762631002803, 4,8317462 46,3772471002801, 4,8296395 46,3795405002797, 4,8357523 46,3795307002797, 4,8359988 46,
3788634002798, 4,8396893 46,3785511002799, 4,8412941 46,3769538002801, 4,8399878 46,3747234002805, 4,8415587 46,3735790002807, 4,8404407 46,3711303002812, 4,8447907 46,3694575002814, 4,845293 46,3705032002813, 4,8496499 46,3700172002814, 4,8504866
46,3713024002811, 4,8560001 46,3697255002814, 4,8559487 46,3688874002815, 4,8586299 46,3680809002817, 4,8561305 46,3644447002823, 4,8536735 46,3593930002832, 4,8515301 46,3565644002837, 4,8510103 46,3523171002845, 4,8462869 46,3531745002843, 4,8436
937 46,3527363002844, 4,8407939 46,3535363002842, 4,8381179 46,3528277002844, 4,8360729 46,3499951002849, 4,8352493 46,3467208002854, 4,8337345 46,3473892002853, 4,8276114 46,3485832002851, 4,8226048 46,348876700285, 4,8214137 46,3472745002853, 4,8
212928 46,3439184002859, 4,8173876 46,3456356002856, 4,8125166 46,3473881002853, 4,8103461 46,3485645002851, 4,8050015 46,354641300284, 4,8055529 46,3572579002836, 4,8082151 46,3594711002832, 4,8119626 46,360913500283)))"}

Status: 400
{
  "error" : {
    "root_cause" : [
      {
        "type" : "parse_exception",
        "reason" : "expected number but found: ','"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse field [shape] of type [geo_shape]",
    "caused_by" : {
      "type" : "parse_exception",
      "reason" : "expected number but found: ','"
    }
  },
  "status" : 400
}

------------------------------

POST http://localhost:9200/shapes_example/_search?pretty=true&typed_keys=true
{}

Status: 200
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

------------------------------

0

1 个答案:

答案 0 :(得分:0)

我无法通过以下方式复制您看到的错误,但这可能在用法上有所不同

private static void Main()
{
    var defaultIndex = "shapes_example";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex)
        .DisableDirectStreaming()
        .PrettyJson()
        .OnRequestCompleted(callDetails =>
        {
            if (callDetails.RequestBodyInBytes != null)
            {
                Console.WriteLine(
                    $"{callDetails.HttpMethod} {callDetails.Uri} \n" +
                    $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
            }
            else
            {
                Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
            }

            Console.WriteLine();

            if (callDetails.ResponseBodyInBytes != null)
            {
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
                         $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
                         $"{new string('-', 30)}\n");
            }
            else
            {
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
                         $"{new string('-', 30)}\n");
            }
        });

    var client = new ElasticClient(settings);

    if (client.Indices.Exists(defaultIndex).Exists)
        client.Indices.Delete(defaultIndex);

    client.Indices.Create(defaultIndex, c => c
        .Map<Document>(m => m
            .AutoMap()
        )   
    );

    var shape = GeoWKTReader.Read("MULTIPOLYGON (((4.7579668999999996 46.2765857002975,4.758639999999998 46.277100600297395,4.7580551999999985 46.280358200296845,4.7599586999999985 46.28062440029681,4.760272399999998 46.28449790029614,4.7599307999999985 46.287958500295545,4.760144999999998 46.288988000295376,4.768144999999999 46.287875000295564,4.7791999999999994 46.28661400029579,4.779245999999998 46.28499000029606,4.780515999999999 46.28457600029613,4.784049999999998 46.286391000295815,4.7855029999999985 46.28613300029587,4.790346999999999 46.287064000295715,4.7930329999999985 46.288710000295424,4.7944049999999985 46.29102700029502,4.797415999999999 46.29047700029512,4.798778999999998 46.29134200029497,4.801573999999999 46.291726000294894,4.803376999999999 46.295254000294314,4.803198999999999 46.29630700029412,4.806661999999998 46.30017200029347,4.809911999999998 46.303189000292946,4.810800999999999 46.30498100029263,4.813812999999999 46.30424100029276,4.8173629999999985 46.304849000292656,4.817989999999998 46.307564000292196,4.820322999999998 46.310179000291754,4.820378999999998 46.31125800029156,4.811309999999998 46.31352800029114,4.812275999999999 46.31594300029076,4.807483999999998 46.31819400029035,4.805152999999999 46.321629000289775,4.804822999999998 46.32296000028955,4.801529999999999 46.322797000289576,4.8021715999999985 46.325112200289176,4.806246399999999 46.32550560028909,4.809514599999998 46.32759570028874,4.808897499999998 46.32852310028857,4.808985999999998 46.337877300286955,4.810925599999999 46.33843760028686,4.817702099999998 46.33346470028771,4.8197126999999975 46.33291060028782,4.820696399999998 46.33184060028801,4.829585999999999 46.33015270028829,4.841794399999999 46.32865140028856,4.851587699999999 46.326253800288974,4.844155299999998 46.31659080029065,4.841140899999998 46.312041200291425,4.839071399999999 46.30872580029199,4.837259299999999 46.30501310029262,4.833032899999998 46.30031740029344,4.831669099999999 46.29636310029412,4.8307376999999985 46.29065400029508,4.827784899999999 46.28152960029665,4.827083699999999 46.277739100297296,4.8263492999999995 46.27620600029758,4.823364199999999 46.276666900297485,4.821481199999998 46.2776875002973,4.8202029999999985 46.279495800296985,4.815944299999999 46.279719400296955,4.816172999999998 46.28109010029673,4.813862699999998 46.28104950029673,4.813230299999998 46.278694000297136,4.812089499999999 46.27765110029731,4.8096404999999995 46.277088600297404,4.802514699999998 46.27920020029703,4.801723599999999 46.27802760029725,4.797655999999998 46.276863400297444,4.793134399999998 46.27807170029723,4.790360699999997 46.27778950029728,4.788977799999999 46.27672700029746,4.786570499999998 46.273582200298,4.786097499999999 46.273974500297946,4.777379599999998 46.276076000297586,4.773336299999999 46.276329300297526,4.768414599999998 46.2753243002977,4.763615199999999 46.27526840029772,4.7579668999999996 46.2765857002975)),((4.811962599999998 46.36091350028296,4.815018199999998 46.36176320028281,4.817915499999999 46.36470840028228,4.818443199999999 46.367786800281735,4.819791799999998 46.372192700280976,4.820597999999999 46.37239760028094,4.821574199999999 46.37626310028026,4.831746199999999 46.37724710028009,4.829639499999997 46.379540500279695,4.835752299999999 46.37953070027969,4.835998799999999 46.37886340027982,4.839689299999999 46.378551100279864,4.841294099999998 46.376953800280134,4.8399877999999985 46.37472340028053,4.8415586999999976 46.37357900028072,4.840440699999999 46.37113030028116,4.844790699999998 46.369457500281435,4.845292999999999 46.37050320028126,4.8496498999999975 46.37001720028136,4.850486599999999 46.37130240028113,4.856000099999998 46.3697255002814,4.855948699999998 46.368887400281544,4.8586298999999995 46.36808090028168,4.856130499999999 46.364444700282334,4.853673499999998 46.359393000283205,4.851530099999999 46.356564400283695,4.851010299999999 46.35231710028445,4.846286899999997 46.3531745002843,4.843693699999998 46.35273630028438,4.8407938999999995 46.353536300284226,4.8381178999999985 46.35282770028436,4.836072899999998 46.34999510028486,4.835249299999998 46.34672080028542,4.8337344999999985 46.34738920028531,4.8276113999999986 46.34858320028509,4.822604799999997 46.34887670028505,4.821413699999999 46.34727450028532,4.821292799999999 46.343918400285915,4.817387599999999 46.34563560028562,4.8125165999999995 46.34738810028533,4.8103460999999985 46.348564500285114,4.8050014999999995 46.35464130028404,4.8055528999999995 46.357257900283585,4.808215099999998 46.3594711002832,4.811962599999998 46.36091350028296)))"); 
    client.Index(new Document { Shape = shape }, i => i.Refresh(Refresh.WaitFor));

    var searchResponse = client.Search<Document>();

    Console.WriteLine(searchResponse.Documents.Count);
}

public class Document
{
    public IGeoShape Shape { get; set; }
}

单个Document实例已正确反序列化。再次序列化后,它将序列化为WKT,而不是GeoJSON。