ElasticSearch NEST - 所有字段都为NULL

4
我正在尝试使用NEST从Elasticsearch检索数据。一切都很好,但是NEST的所有字段都返回null。然而,在调试模式下,我看到它正确地计算了文档数量,但没有显示字段的值。

enter image description here

enter image description here

我已经完成的部分:

  • 检查过映射,看起来没问题
  • 尝试了字符串查询
  • 尝试获取源代码并读取数据
  • 尝试 NEST返回 null 而不是字段 ,但这些解决方案都没有帮助
  • 将Product.cs字段名称更改为camelCase也没有帮助

这是我现在的代码:

public class ElasticSearch
{
    private ElasticClient _client;

    public ElasticSearch()
    {
        var node = new Uri("http://localhost:9200");
        var settings = new ConnectionSettings(node);
        settings.DefaultIndex("logsystem.logs");
        _client = new ElasticClient(settings);
    }

    public void searchResults()
    {
        var searchResults = _client.Search<Product>(s => s.AllIndices());


    }
}

Product.cs

    [BsonIgnoreExtraElements]
    public class Product
    {
        [BsonId]
        [BsonIgnore]
        public ObjectId Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string ProductLicenseKey { get; set; }
        [Required]
        public string Action { get; set; }
        [Required]
        public string ActionName { get; set; }
        [Required]
        public string MachineId { get; set; }
    }

ElasticSearch中的映射:

{
"logsystem.logs": {
    "mappings": {
        "properties": {
            "Action": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "ActionName": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "MachineId": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "Name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "ProductLicenseKey": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
}
}

也许我的映射不正确?任何回答都会有所帮助。谢谢。
通过Postman获取ElasticSearch文档的编辑。
{
"took": 11,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
        "value": 6,
        "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
        {
            "_index": "logsystem.logs",
            "_type": "_doc",
            "_id": "5e5c1ca2aaa6f1245cc38895",
            "_score": 1.0,
            "_source": {
                "Action": "Button",
                "ActionName": "Fixed Single Generation",
                "MachineId": "987-654-321",
                "Name": "System",
                "ProductLicenseKey": "123-456-789"
            }
        },
        {
            "_index": "logsystem.logs",
            "_type": "_doc",
            "_id": "5e5c1cb0aaa6f1245cc38896",
            "_score": 1.0,
            "_source": {
                "Action": "Button",
                "ActionName": "Fixed Multiple Generation",
                "MachineId": "987-654-321",
                "Name": "System",
                "ProductLicenseKey": "123-456-789"
            }
        },
        {
            "_index": "logsystem.logs",
            "_type": "_doc",
            "_id": "5e5c1cbdaaa6f1245cc38897",
            "_score": 1.0,
            "_source": {
                "Action": "Button",
                "ActionName": "Trackers Single Generation",
                "MachineId": "987-654-321",
                "Name": "System",
                "ProductLicenseKey": "123-456-789"
            }
        },
        {
            "_index": "logsystem.logs",
            "_type": "_doc",
            "_id": "5e5c1ccbaaa6f1245cc38898",
            "_score": 1.0,
            "_source": {
                "Action": "Button",
                "ActionName": "Trackers Multiple Generation",
                "MachineId": "987-654-321",
                "Name": "System",
                "ProductLicenseKey": "123-456-789"
            }
        },
        {
            "_index": "logsystem.logs",
            "_type": "_doc",
            "_id": "5e5c1cd3aaa6f1245cc38899",
            "_score": 1.0,
            "_source": {
                "Action": "Button",
                "ActionName": "Fixed Multiple Generation",
                "MachineId": "987-654-321",
                "Name": "System",
                "ProductLicenseKey": "123-456-789"
            }
        },
        {
            "_index": "logsystem.logs",
            "_type": "_doc",
            "_id": "5e5c1ce0aaa6f1245cc3889a",
            "_score": 1.0,
            "_source": {
                "Action": "Button",
                "ActionName": "Tree Generation",
                "MachineId": "987-654-321",
                "Name": "System",
                "ProductLicenseKey": "123-456-789"
            }
        }
    ]
 }
}

你能发布一下 ElasticSearch 中记录的样子吗? - Viresh Mathad
@VireshMathad 在帖子中添加了。这就是我在 Postman 中得到的结果。 - Julius
我猜你应该检查“点击数”而不是文档。 - Viresh Mathad
@VireshMathad 哎呀,一样的问题。字段甚至都找不到... - Julius
2个回答

12

问题在于客户端试图将驼峰式的JSON对象键反序列化为POCO属性并强制转换。

解决方法: 创建ES客户端时,在ConnectionSettings上添加DefaultFieldNameInferrer设置属性。

    public ElasticSearch()
    {
        var node = new Uri("http://localhost:9200");
        var settings = new ConnectionSettings(node);
        settings.DefaultIndex("logsystem.logs");
        settings.DefaultFieldNameInferrer(p => p);
        _client = new ElasticClient(settings);
    }

注意: 更改此设置将导致某些字段无法正确反序列化,如果它们是基本数据类型(例如intbooldecimal等),则其在底层数据中可以为null。如果遇到该问题,请在类型后添加可为空的?运算符,它应该可以修复该问题。


1
您在这个解决方案中添加了其他内容吗?我尝试实现它时出现了一个新的错误。 - Benjamin Salerno
@BenjaminSalerno 不是。我刚刚添加了这个,它在7.6.0版本中完美运行。 - Julius
1
遇到了同样的问题。有些属性必须标记为可空。当我的C#类具有任何值类型,如intdecimalfloatbool,其中底层数据可以为null时,public bool IsAwesome { get; set; }变成了public bool? IsAwesome { get; set; })。一旦我这样做了,错误就消失了。我希望更改DefaultFieldNameInferrer设置不会破坏值类型反序列化。 - Josh

0
你应该使用这种技术来命名字段。
例如:
"fields": {
   "keyword": {
      "type": "keyword",
      "ignore_above": 256
    }
  }

public class Entity
{
    [Nested(Name = "fields")]
    public Fields Fields { get; set; }
}

public class Fields
{
    [Nested(Name = "keyword")]
    public Keyword Keyword { get; set; }
}

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

    [Number(Name = "ignore_above")]
    public string Ignore { get; set; }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接