Mukil Deepthi
Mukil Deepthi

Reputation: 6452

C# NEST elasticsearch source filtering returning null for most of the fields

I am new to Elasticsearch with .NET using NEST. I am trying to execute a simple search to match all and interested with only few properties. I am not able to get the values for almost all the fields in the source. all shows as null value

The index already exists in elasticsearch.

I have a class representing the type.

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; }
}

and my mapping is:

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

The following query returns Hits count correctly but the values are null except for the Kind property. Not sure what am i doing wrong here. Is this because the property names are different in c# class or something else?

    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
                                        )
                                    )
                                );

    }

Upvotes: 0

Views: 3148

Answers (1)

LeBigCat
LeBigCat

Reputation: 1770

Property need to have the same name in order to nest to map it correctly with your es index.

You can use attribute in your c# class file to change the mappings if you want to have different name on c# side. You can use fluent mapping too.

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

Hope it's help

++

Upvotes: 1

Related Questions