c# - 使用 Elasticsearch 将数组字段映射到 C# 字符串列表

标签 c# elasticsearch nest

如图所示,我有一个包含字符串值数组的组字段。 但是,当我尝试将其映射到 List<string> 时遇到异常。属性。

类似于 Error converting value \"[134706634,134706635]\" to type System.Collections.Generic.IList[System.String]'

我尝试使用elasticsearch提供的一些不同属性,但没有任何效果。 json 转换器属性工作正常,但需要编写大量代码才能使其按照我想要的方式工作。

有没有一种更干净、更原生的方式来使用 NEST 来做到这一点?

C# 代码:

 var groupQuery = await
     elastic.SearchAsync<CorrelationGroup>(s => s
             .Query(q => q
             .Bool(b => b
             .Must(
                  m => m.ConstantScore(c => c
                       .Filter(f => f
                       .Term(x => x.Type, counterType))
                   ),
                  m => m.ConstantScore(c => c.
                        Filter(f => f
                       .Term(x => x.TypeValue, counterTypeValue))))))
             .Index("correlation-groups").AllTypes());

 public class CorrelationGroup
 {
     [Text(Name = "type")]
     public string Type { get; set; }

     [Text(Name = "type_value")]
     public string TypeValue { get; set; }

     public List<string> Groups { get; set; }
 }

源 json 文件:

[ { "type": "APN", "type_value": "internet", "groups": [150994936,150994940] }, { "type": "APN", "type_value": "internet", "groups": [150984921,150984922] }, { "type": "APN", "type_value": "internet", "groups": [150984917,150984918,150984921,150984922] } ]

我的模板是:

{
    "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"}
            }
         }
     }
 }

LOGSTASH CONF

INDEX

最佳答案

该问题与您的模板有关;在 __default__ 映射中,由于您已将 groups 指定为 string 类型,因此传入属性将被字符串化并以字符串形式保存在 Elasticsearch 中。 Logstash 编解码器将正确地将源 json 中的 groups 属性作为数字数组发送,但由于您具有默认映射,因此将保留为字符串。

要进行纠正,请将 __default__ 映射更改为

"mappings": {
    "_default_": {
        "properties": {
            "type": { "type": "string", "index": "not_analyzed" },
            "type_value": { "type": "string", "index": "not_analyzed" },
            "groups": { "type": "integer" }
        }
     }
 }

如果索引中只有一种类型,您可能需要显式定义该类型的映射。

完成此操作后,将 C# POCO 更改为

public class CorrelationGroup
{
    [Keyword]
    public string Type { get; set; }

    [Keyword(Name = "type_value")]
    public string TypeValue { get; set; }

    public List<int> Groups { get; set; }
}

如果您使用的是 Elasticsearch 5.x,则应使用映射到旧版 not_analyzed string 类型的 keyword 类型。此外,您只需指定 TypeValue 的名称,因为它使用蛇形大小写(NEST 客户端理解来自 Elasticsearch 的驼峰式大小写命名,并将映射到帕斯卡大小写 POCO 属性名称)。最后,我将 Groups 属性更改为 int 列表。

关于c# - 使用 Elasticsearch 将数组字段映射到 C# 字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876073/

相关文章:

c# - Silverlight ChildWindow 在回调时未正确关闭

elasticsearch - 如何在 Nest for Elasticsearch 中添加完整的短语分词器?

c# - 当每个索引只能有一个映射时,将渗透器存储在单独的索引中吗?

elasticsearch - 使用Nest Client将多语言ElasticSearch文档映射到单语对象

c# - 检查数据库中是否已存在电子邮件地址

c# - 通过 SDK 从插件内部使用 ReSharper "Call Tracking"

c# - System.IO.Abstraction.TestingHelpers - 在不同平台上进行测试

java - Elasticsearch-术语聚合嵌套字段

c# - ElasticSearch:使用NEST从动态条件列表中创建查询

jakarta-ee - 如何存储,版本化和部署 Elasticsearch 河插件设置?