Skip to content

Testing if single field exists on GET result #1317

@samjudson

Description

@samjudson

Hi

I'm getting a document (using Get() method) and only want to return a single field. However I couldn't find an easy way to then retrieve that field, or rather to test if the field exists (as it doesn't on a lot of documents).

So far I have the following (this works), but I think it should be easier than this:

var result = client.Get<ElasticCreative>(get => get.Index(SitePrefix).Id(recordNumber).Fields(c => c.KeywordPath));

var list = new List<string>();

if (result.Found && result.Fields != null)
{
    IFieldSelection<ElasticCreative> fields = result.Fields;
    if (fields.FieldValuesDictionary != null)
    {
        list.AddRange(fields.FieldValues<string[]>("keywordPath"));
    }
}

Having to cast to a IFieldSelection to check if the FieldValueDictionary is null before then retrieving the field value seems wrong. However if I try to retrieve the FieldValue without that check I get a null exception thrown by the call to FieldValue (as it uses the FieldValueDictionary under the hood).

Would this be considered a bug? Would it not be better to be able to do something simpler like below? (This throw exception if the "keywordPath field doesn't exist).

var result = client.Get<ElasticCreative>(get => get.Index(SitePrefix).Id(recordNumber).Fields(c => c.KeywordPath));

var list = new List<string>();

if (result.Found && result.Fields != null)
{
    var path = fields.FieldValues<string[]>("keywordPath");
    if( path != null ) list.AddRange(path);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions