C#NEST elasticsearch源过滤对于大多数字段返回null

时间:2018-11-22 09:42:02

标签: c# elasticsearch nest

我是使用NEST使用.NET进行Elasticsearch的新手。 我正在尝试执行一个简单的搜索,以匹配所有感兴趣的属性,并且只包含几个属性。我无法获取源中几乎所有字段的值。全部显示为空值

该索引已经存在于elasticsearch中。

我有一个代表类型的类。

public class DocType
{
    public long CommunicationsDate { get; set; }
    public string ControlNumber { get; set; }
    public string Kind { get; set; }
    public string PrimaryCommuncationsName { get; set; }
    public float RiskScore { get; set; }
}

我的映射是:

PUT relativity
{
  "mappings": {
    "doctype": {
      "properties": {
        "comms_date": {
          "type": "date"
        },
        "control_number": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "kind": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "primary_comms_name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "risk_score": {
          "type": "float"
        }
      }
    }
  }
}

以下查询返回的命中计数正确,但除Kind属性外,其值为空。不知道我在这里做错了什么。这是因为属性名称在c#类中还是其他名称上不同?

    return await _connection.Get<ElasticClient>().SearchAsync<DocType>(s =>
    {       
        var searchDescriptor = s.Index("relativity")
                                .Type("DocType")
                                .Size(100)
                                .Source(sf => sf
                                    .Includes(i => i
                                        .Fields(
                                            f => f.ControlNumber,
                                            f => f.PrimaryCommuncationsName,
                                            f => f.RiskScore,
                                            f => f.Kind,
                                            f => f.CommunicationsDate
                                        )
                                    )
                                );

    }

1 个答案:

答案 0 :(得分:1)

属性需要具有相同的名称,以便嵌套以将其正确映射到您的es索引。

如果要在c#端使用不同的名称,则可以在c#类文件中使用attribute来更改映射。 您也可以使用流畅的映射。

https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/attribute-mapping.html

希望有帮助

++