NEST 2.x条款查询不能接受2个参数

时间:2016-11-18 12:34:25

标签: elasticsearch nest

如何将NEST 1.x表达式重写为NEST 2.x或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)));

接受List(),其中包含有关弹性搜索查询应匹配的内容的元素

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

更新:

elasticsearch documentation for 1.X上的最后一个示例包含字段和 qff.Terms(p =&gt; p.Country,userInput.Countries)我想在NEST 5.x或2.x中实现

1 个答案:

答案 0 :(得分:0)

Take a look at the Terms query documentationterms查询需要一个字段,其中包含要匹配的字词以及要匹配的字词集合。

可以使用.Field()which can take anything from which a Field can be inferred指定要匹配的字段,包括字符串或Lambda表达式。

可以使用.Terms()指定要匹配的值,这是一组术语。

鉴于以下POCO

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

对于标记字段的terms查询将是

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