我从.NET中的NEST开始,用于实现ElasticSearch查询,一开始我就遇到了一个问题,为什么不能使用以下语句创建对象。

var searchResponse = client.Search<TestDto>(sd => sd
            .Index("qa-report")
            .Size(1)
            .Query(q => q
                .Match(m => m.Field("TestId").Query("CX001")
                )));

执行时,对象searchResponse.Documents包含为空的TestDto
我不确定TestDto是否正确。如何从ElasticSeach映射轻松创建它?还是在ES中显示数据时是否必须根据JSON响应创建类?

这是我的课
public class TestDto
{
    public class Platform
    {
        public string BrowserName { get; set; }
        public string OS { get; set; }
        public string OsVersion { get; set; }
    }

    public class MachineInfo
    {
        public string IP { get; set; }
        public string Name { get; set; }
        public string User { get; set; }
    }

    public class MetaData
    {
        public string Environment { get; set; }
        public Platform Platform { get; set; }
        public double DurationMs { get; set; }
        public string TestName { get; set; }
        public string SuiteName { get; set; }
        public IList<string> Categories { get; set; }
        public string ExceptionsStr { get; set; }
        public object LastException { get; set; }
        public object Priority { get; set; }
        public object TestSuiteId { get; set; }
        public string TestStatusStr { get; set; }
        public string Logs { get; set; }
        public string LastMessage { get; set; }
        public string JobName { get; set; }
        public string BuildNumber { get; set; }
        public string TestRunId { get; set; }
        public MachineInfo MachineInfo { get; set; }
    }

    public class Example
    {
        public string TestId { get; set; }
        public string PlatformStr { get; set; }
        public string Duration { get; set; }
        public string ResultMessage { get; set; }
        public int TestStatus { get; set; }
        public string ResultId { get; set; }
        public DateTime Created { get; set; }
        public IList<object> Issues { get; set; }
        public MetaData MetaData { get; set; }
    }
}

这是我在ES中得到的回应:
{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 90,
    "successful": 90,
    "failed": 0
  },
  "hits": {
    "total": 1643,
    "max_score": 10.439584,
    "hits": [
      {
        "_index": "qa-report",
        "_type": "qa-report",
        "_id": "445.0",
        "_score": 10.439584,
        "_source": {
          "TestId": "CX001",
          "PlatformStr": "windows_10_chrome_+",
          "Duration": "00:00:05.2530435",
          "ResultMessage": """
None
""",
          "TestStatus": 4,
          "ResultId": "e338ca8b-dd95-4f57-9bed-20d0e65efc4c",
          "Created": "2019-11-29T06:01:48.617172Z",
          "Issues": [],
          "MetaData": {
            "Environment": "local",
            "Platform": {
              "BrowserName": "chrome",
              "OS": "windows",
              "OsVersion": "10"
            },
            "DurationMs": 5253.043500000001,
            "TestName": "TestName",
            "SuiteName": "SuiteName",
            "Categories": [
              "Cat1",
              "Cat2"
            ],
            "ExceptionsStr": """
None
""",
            "LastException": {
              "Type": "System.InvalidOperationException",
              "Message": "Sequence contains no matching element",
              "StackTrace": """
None
"""
            },
            "Priority": null,
            "TestSuiteId": null,
            "TestStatusStr": "Failed",
            "Logs": """
None
""",
            "LastMessage": """
None
""",
            "JobName": "Job1",
            "BuildNumber": "10",
            "TestRunId": "LTestRunId1",
            "MachineInfo": {
              "IP": "10",
              "Name": "PI",
              "User": "PO"
            }
          }
        }
      }
    ]
  }
}

最佳答案

默认情况下为NEST maps POCO property names which are Pascal-cased, to camel-cased JSON object property names。例如

public class Platform
{
    public string BrowserName { get; set; }
    public string OS { get; set; }
    public string OsVersion { get; set; }
}

将映射到以下JSON
{
    "browserName": "",
    "oS": "",
    "osVersion": ""
}

NEST 7.x对此 shell 严格。由于Elasticsearch的结果包含Pascal大小写的JSON属性名称,因此我们需要告诉NEST期望Pascal大小写的属性。可以使用 DefaultFieldNameInferrer 全局完成此操作

var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
    .DefaultFieldNameInferrer(p => p); // <-- add this

var client = new ElasticClient(settings);

关于.net - .NET NEST中的Elasticsearch查询未初始化对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61506312/

10-15 22:42