c# - Elasticsearch 地理位置未使用NEST映射到geo_point类型

标签 c# elasticsearch nest elastic-cloud

我正在C#中使用NEST(版本7.7.1)创建动态索引。数据包含地理位置,需要在 Elasticsearch (elastic.co中的版本7.6.1)索引中存储为geo_point。但是映射并没有发生。它始终将其作为对象代替geo_point。

我尝试了多种选择,例如
1)自动映射
2)手动映射
3)自动映射和手动映射。

以下是代码

Uri EsInstance = new Uri(EsUrl);
ConnectionSettings EsConfiguration = new ConnectionSettings(EsInstance);

if (!string.IsNullOrWhiteSpace(EsUser))
    EsConfiguration.BasicAuthentication(EsUser, EsPass);
EsClient = new ElasticClient(EsConfiguration);

var settings = new IndexSettings { NumberOfReplicas = 2, NumberOfShards = 5 };
indexConfig = new IndexState
{
    Settings = settings
};


var res = await EsClient.CreateIndex(indexName,
                            c => c.Index(indexName)
                                  .Map<TripInsightDocument>(
                                        m => m.Properties(ps => ps)
                                            .AutoMap()
                                            .Properties(ps => ps
                                                .Object<TripInsightLeg>(o => o
                                                    .Name(n => n.legs)
                                                    .AutoMap()
                                                    .Properties(lp => lp
                                                        .GeoPoint(gp => gp.Name(fn => fn.fpos).IgnoreMalformed(true))
                                                        .GeoPoint(gp => gp.Name(fn => fn.tpos).IgnoreMalformed(true))
                                                    )
                                                )
                                                .Object<TripInsightStop>(o => o
                                                    .Name(n => n.stops)
                                                    //.AutoMap()
                                                    .Properties(lp => lp
                                                        .GeoPoint(gp => gp.Name(fn => fn.pos).IgnoreMalformed(true))
                                                     )
                                                )
                                                .Object<TripInsightSegmt>(o => o
                                                    .Name(n => n.segmts)
                                                    //.AutoMap()
                                                    .Properties(lp => lp.GeoPoint(gp => gp.Name(fn => fn.fpos).IgnoreMalformed(true))
                                                                        .GeoPoint(gp => gp.Name(fn => fn.tpos).IgnoreMalformed(true))
                                                    )
                                                )
                                                .Object<TripInsightGFence>(o => o
                                                    .Name(n => n.gfence)
                                                    //.AutoMap()
                                                    .Properties(lp => lp.GeoPoint(gp => gp.Name(fn => fn.pos).IgnoreMalformed(true)))
                                                )
                                            )
                                   ).RequestConfiguration(r => r
                                        .DisableDirectStreaming()
                                   )
                                  .InitializeUsing(esh.indexConfig));

以下是映射的结果
{
  "mapping": {
    "_doc": {
      "properties": {
        "avgspeed": {
          "type": "float"
        },
        "cap": {
          "type": "float"
        },
        "cewbno": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "cid": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "dest": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "dfrq": {
          "type": "float"
        },
        "dfrqug": {
          "type": "float"
        },
        "did": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "dkts": {
          "type": "long"
        },
        "drv2mob": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "drv2nm": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "drvmob": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "drvnm": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "etm": {
          "type": "date"
        },
        "gfence": {
          "properties": {
            "gfname": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "indt": {
              "type": "date"
            },
            "outdt": {
              "type": "date"
            },
            "pos": {
              "properties": {
                "lat": {
                  "type": "float"
                },
                "lon": {
                  "type": "float"
                }
              }
            },
            "stime": {
              "type": "float"
            }
          }
        },
        "legs": {
          "properties": {
            "aat": {
              "type": "date"
            },
            "adt": {
              "type": "date"
            },
            "arvrmk": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "avgspeed": {
              "type": "float"
            },
            "deprmk": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "floc": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "fpos": {
              "properties": {
                "lat": {
                  "type": "float"
                },
                "lon": {
                  "type": "float"
                }
              }
            },
            "gat": {
              "type": "date"
            },
            "gdt": {
              "type": "date"
            },
            "lname": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "lno": {
              "type": "long"
            },
            "sta": {
              "type": "date"
            },
            "std": {
              "type": "date"
            },
            "stime": {
              "type": "float"
            },
            "tloc": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "todist": {
              "type": "float"
            },
            "totime": {
              "type": "float"
            },
            "tpos": {
              "properties": {
                "lat": {
                  "type": "float"
                },
                "lon": {
                  "type": "float"
                }
              }
            }
          }
        },
        "load": {
          "type": "float"
        },
        "mfs": {
          "type": "long"
        },
        "mrktveh": {
          "type": "boolean"
        },
        "org": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "pkgs": {
          "type": "float"
        },
        "portdiv": {
          "type": "boolean"
        },
        "prepby": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "prov": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "remarks": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "rutcd": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "rutnm": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "segmts": {
          "properties": {
            "avgspeed": {
              "type": "float"
            },
            "fpos": {
              "properties": {
                "lat": {
                  "type": "float"
                },
                "lon": {
                  "type": "float"
                }
              }
            },
            "ftime": {
              "type": "date"
            },
            "segid": {
              "type": "long"
            },
            "todist": {
              "type": "float"
            },
            "totime": {
              "type": "float"
            },
            "tpos": {
              "properties": {
                "lat": {
                  "type": "float"
                },
                "lon": {
                  "type": "float"
                }
              }
            },
            "trdist": {
              "type": "float"
            },
            "trtime": {
              "type": "float"
            },
            "ttime": {
              "type": "date"
            }
          }
        },
        "stm": {
          "type": "date"
        },
        "stops": {
          "properties": {
            "ftime": {
              "type": "date"
            },
            "pos": {
              "properties": {
                "lat": {
                  "type": "float"
                },
                "lon": {
                  "type": "float"
                }
              }
            },
            "seqid": {
              "type": "long"
            },
            "stime": {
              "type": "float"
            },
            "trdist": {
              "type": "float"
            },
            "ttime": {
              "type": "date"
            }
          }
        },
        "stpnts": {
          "type": "long"
        },
        "tgpnts": {
          "type": "long"
        },
        "todist": {
          "type": "float"
        },
        "totime": {
          "type": "float"
        },
        "tripdt": {
          "type": "date"
        },
        "tripid": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "tugpnts": {
          "type": "long"
        },
        "vid": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "vndnm": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "vol": {
          "type": "float"
        },
        "wt": {
          "type": "float"
        }
      }
    }
  }
}

最佳答案

问题是,我在初始化索引之前进行了映射。
刚刚交换了执行顺序,现在它可以正常工作

var res = await EsClient.CreateIndex(indexName,
                            c => c.Index(indexName)
                                  //Initialized index before updating mapping
                                  .InitializeUsing(esh.indexConfig) 
                                  .Map<TripInsightDocument>(
                                        m => m.Properties(ps => ps)
                                            .AutoMap()
                                            .Properties(ps => ps
                                                .Object<TripInsightLeg>(o => o
                                                    .Name(n => n.legs)
                                                    .AutoMap()
                                                    .Properties(lp => lp
                                                        .GeoPoint(gp => gp.Name(fn => fn.fpos).IgnoreMalformed(true))
                                                        .GeoPoint(gp => gp.Name(fn => fn.tpos).IgnoreMalformed(true))
                                                    )
                                                )
                                                .Object<TripInsightStop>(o => o
                                                    .Name(n => n.stops)
                                                    //.AutoMap()
                                                    .Properties(lp => lp
                                                        .GeoPoint(gp => gp.Name(fn => fn.pos).IgnoreMalformed(true))
                                                     )
                                                )
                                                .Object<TripInsightSegmt>(o => o
                                                    .Name(n => n.segmts)
                                                    //.AutoMap()
                                                    .Properties(lp => lp.GeoPoint(gp => gp.Name(fn => fn.fpos).IgnoreMalformed(true))
                                                                        .GeoPoint(gp => gp.Name(fn => fn.tpos).IgnoreMalformed(true))
                                                    )
                                                )
                                                .Object<TripInsightGFence>(o => o
                                                    .Name(n => n.gfence)
                                                    //.AutoMap()
                                                    .Properties(lp => lp.GeoPoint(gp => gp.Name(fn => fn.pos).IgnoreMalformed(true)))
                                                )
                                            )
                                   ).RequestConfiguration(r => r
                                        .DisableDirectStreaming()
                                   ));

关于c# - Elasticsearch 地理位置未使用NEST映射到geo_point类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62430378/

相关文章:

c# - 自定义组合框样式

elasticsearch - 如何使用 Elasticsearch Mapping API 更改现有索引的字段类型

java - elasticsearch自定义相似度插件如何实现?

elasticsearch - Puppet - 依赖项不起作用

elasticsearch - 如何使用NEST搜索文本字段 “as is”?

c# - 将 c# `Index` 和 `Range` 与锯齿状数组一起使用

c# - 为什么 Mono 源代码大部分是 C#?

c# - 为什么 `GetFormat` 被调用两次?

elasticsearch - 在 NEST/Elasticsearch.net 中使用 azure 事件目录

elasticsearch - 使用NEST更新ElasticSearch中的子对象