xaberue
xaberue

Reputation: 336

Perform a query over a nested collection in ElasticSearch with NEST in .NET Core

I am trying to perform a search over an index for the following object:

public class IndexedElement
{

    public Guid Id { get; set; }
    public long RowId { get; set; }
    public IndexedElementType Type { get; set; }
    public string Summary { get; set; }
    public string Description { get; set; }

    public IList<string> Tags { get; set; }

}

The purpose is to search by the Summary property, or by matching any of the strings inside the Tags collections

What I currently have is this:

    public IEnumerable<IndexedElement> Search(string description)
    {
        var query = GetClient().Search<IndexedElement>(s => s.From(0).Size(5)
            .Query(
                q => q.Term(p => p.Summary, description)
                ||
                q.Nested(n => n.Path(p => p.Tags).Query(q2 => q2.Terms(t => t.Field(f => f.Tags).Terms(description))))                    
            ));

        return query.Documents.ToList();
    }

But the Nested part is not working, I don't know if I am using it in the proper way or maybe I have to find another solution for that.

Any ideas?

Thank you all in advance

Upvotes: 1

Views: 682

Answers (1)

Russ Cam
Russ Cam

Reputation: 125528

You don't need to perform a nested query to query the Tags field, as each tag is only a primitive JSON value i.e. a string. Just the terms query will suffice.

Where a nested query would be needed is where Tags is a POCO with multiple properties and is mapped as a nested datatype.

Upvotes: 1

Related Questions