使用Elasticsearch将数组字段映射到C#字符串列表
如图所示,我有一个带有字符串值数组的groups字段。 但是,在尝试将其映射到使用Elasticsearch将数组字段映射到C#字符串列表,c#,elasticsearch,nest,C#,elasticsearch,Nest,如图所示,我有一个带有字符串值数组的groups字段。 但是,在尝试将其映射到列表属性时,我遇到了一个异常 类似于将值\“[134706634134706635]\”转换为类型System.Collections.Generic.IList[System.String]”时出现的错误。 我尝试使用elasticsearch提供的一些不同属性,但都不起作用。json转换器属性工作正常,但需要编写大量代码才能按我所希望的方式工作 有没有一种更干净、更自然的方法来处理NEST C#代码: 该问题与您
列表
属性时,我遇到了一个异常
类似于将值\“[134706634134706635]\”转换为类型System.Collections.Generic.IList[System.String]”时出现的错误。
我尝试使用elasticsearch提供的一些不同属性,但都不起作用。json转换器属性工作正常,但需要编写大量代码才能按我所希望的方式工作
有没有一种更干净、更自然的方法来处理NEST
C#代码:
该问题与您的模板有关;在
\uuuu默认\uuuu
映射中,由于已将组
指定为字符串
类型,因此传入属性将被字符串化并作为字符串保留在Elasticsearch中。Logstash编解码器将正确地从源json以数字数组的形式发送groups
属性,但由于您具有默认映射,因此将作为字符串保留
若要更正,请将\uuuu默认值\uuuu
映射更改为
"mappings": {
"_default_": {
"properties": {
"type": { "type": "string", "index": "not_analyzed" },
"type_value": { "type": "string", "index": "not_analyzed" },
"groups": { "type": "integer" }
}
}
}
如果索引中只有一个类型,则可能需要为该类型显式定义映射
完成此操作后,将C#POCO更改为
公共类关联组
{
[关键词]
公共字符串类型{get;set;}
[关键字(Name=“type_value”)]
公共字符串类型值{get;set;}
公共列表组{get;set;}
}
如果您使用的是Elasticsearch 5.x,您应该使用映射到较旧的
未分析字符串类型的关键字类型。此外,您只需指定TypeValue
的名称,因为它使用snake外壳(NEST client理解Elasticsearch中的camel case命名,并将映射到pascal case POCO属性名称)。最后,我将Groups
属性更改为int
列表。看起来您的数组实际上是一个字符串。请参阅数组周围的转义双引号“[134706634134706635]\”
?您的组
数组很可能在某一点上被字符串化并存储为字符串。如果您能看到如何为索引创建映射以及如何为数据编制索引,那将是非常好的。@Val我在模板中指定了类型字符串(这是我从文档中了解到的),我是否应该使用其他内容来代替它?不过,这是正确的,这个问题似乎存在于为文档编制索引的代码的上游。很可能您的客户端代码正在以某种方式字符串化该数组,并且既不发送列表也不发送数组。@Rob请参阅编辑模板我正在使用的模板有关详细答案,请参阅Anks。我尝试了你的建议,但现在logstash抱怨它无法将字符串转换为int。你需要删除该索引并重新创建它(我猜现在你可以这样做)。Logstash应该通过正确的json发送;您还可以输出到标准输出以进行检查吗?好的,通过查看标准输出,它似乎可以工作。我必须做的一件事是将json文件设为单行。但是,我在GUI(head插件)中没有看到组列。知道为什么吗?好的,代码也可以。我想这个head
插件也欺骗了我,它不起作用。它没有显示“我的组”列,但它在那里:)为帮助干杯。有一段时间没看HEAD插件了。我建议使用curl或Kibana的Sense来检查:
{
"template": "correlation-groups",
"settings" : {
"number_of_shards" : 2,
"number_of_replicas" : 0,
"index" : {
"store" : { "compress" : { "stored" : true, "tv": true } }
}
},
"dynamic_templates": [
{
"string_template" : {
"match" : "*",
"mapping": { "type": "string", "index": "not_analyzed" },
"match_mapping_type" : "string"
}
}
],
"mappings": {
"_default_": {
"properties": {
"type": { "type": "string", "index": "not_analyzed" },
"type_value": { "type": "string", "index": "not_analyzed" },
"groups": { "type": "string"}
}
}
}
}
"mappings": {
"_default_": {
"properties": {
"type": { "type": "string", "index": "not_analyzed" },
"type_value": { "type": "string", "index": "not_analyzed" },
"groups": { "type": "integer" }
}
}
}
public class CorrelationGroup
{
[Keyword]
public string Type { get; set; }
[Keyword(Name = "type_value")]
public string TypeValue { get; set; }
public List<int> Groups { get; set; }
}