0
votes

How NEST 1.x expression below could be rewritten to NEST 2.x or 5.x

var searchResult = _elasticClient.Search<SearchResult>(
                request => request
                    .MinScore(0.7)
                    .Query(q =>
                    {
                        QueryContainer query = null;
                        query &= q.Terms<int>(t => t.Categories
                        .SelectMany(s => s.ChildCategories.Select(c => c.Id))
                        .ToArray(),
                        categories.Select(c => Convert.ToInt32(c)));

to accept List() which contains elements on what ids elastic search query should match

query &= q.Terms(c => c.Field(t => t.Categories.SelectMany(s => s.ChildCategories.Select(d => d.Id))));

This line will below complain about Terms has 1 parameter, but invoked with 2
query &= q.Terms(c => c.Field(t => t.Categories.SelectMany(s => s.ChildCategories.Select(d => d.Id))), new List<int> {1});

UPDATE:

The last example on elasticsearch documentation for 1.X contains field and qff.Terms(p => p.Country, userInput.Countries) which I want to achieve in NEST 5.x or 2.x

1

1 Answers

0
votes

Take a look at the Terms query documentation. A terms query needs a field that contains the term(s) to match and the collection of terms to match against.

field to match can be specified using .Field(), which can take anything from which a Field can be inferred, including a string or a Lambda expression.

values to match against can be specified using .Terms(), which is a collection of terms.

Given the following POCO

public class Project
{
    public IEnumerable<string> Tags { get; set; }
}

A terms query on the tags field would be

var searchResponse = client.Search<Project>(s => s
    .Query(q => q
        .Terms(t => t
            .Field(f => f.Tags)
            .Terms("tag1", "tag2", "tag3")
        )
    )
);