0

我在 C# 中使用 NEST(版本 7.7.1)创建动态索引。数据包含需要在弹性搜索(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"
        }
      }
    }
  }
}
4

1 回答 1

0

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

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()
                                   ));
于 2020-06-18T10:23:02.473 回答