【问题标题】:C# NEST elasticsearch source filtering returning null for most of the fieldsC# NEST elasticsearch 源过滤为大多数字段返回 null
【发布时间】:2019-04-24 23:01:01
【问题描述】:

我是使用 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"
        }
      }
    }
  }
}

以下查询正确返回 Hits 计数,但除了 Kind 属性之外的值为 null。不知道我在这里做错了什么。这是因为 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
                                        )
                                    )
                                );

    }

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

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

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

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

    希望对你有帮助

    ++

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 2016-04-03
      相关资源
      最近更新 更多