简体   繁体   English

C#Nest - ElasticSearch

[英]C# Nest - ElasticSearch

I'm new to ElasticSearch and I'm using NEST to run my queries. 我是ElasticSearch的新手,我正在使用NEST来运行我的查询。 I need to be able to add X amount of filtering terms to my query. 我需要能够在查询中添加X量的过滤术语。

For now my query looks like this: 现在我的查询看起来像这样:

var page = new Page
{
    Id = 1,
    Name = "JR-11 Hyper black"
};

var tags = new Dictionary<string, string[]>
{
    { "Size", new[] { "16", "17", "18" }},
    { "Color", new[] { "Bronze", "Hyper Black", "Flat Black" }}
};

page.Tags = tags;

ElasticClient.Index(page, idx => idx.Index("pages"));

var result = ElasticClient.Search<Page>(
    body => body.Query(query => query.ConstantScore(
        csq => csq.Filter(filter => filter.Term("tags.Size", "17" ))))
    .Take(1000));

var pages = result.Documents.ToList();

The problem I have is with the csq.Filter(filer => filter.Term("tags.Storlek") csq.Filter(filer => filter.Term("tags.Storlek")的问题是csq.Filter(filer => filter.Term("tags.Storlek")

I need to be able to add a dynamic amount of such filters. 我需要能够添加动态数量的此类过滤器。 Can't really find anything in the documentation for the 2.3 version that I'm using. 无法在我正在使用的2.3版本的文档中找到任何内容。

Fluent API should allow for something like this: Fluent API应该允许这样的事情:

string[] filterTerms = { ... };

var result = ElasticClient.Search<Page>(
    body => body.Query(query => query.ConstantScore(
    csq =>
    {
        var combinedFilters = csq.Filter(filter => filter.Term("tags.Size", "17" ));

        // add an additional, dynamic amount of filters
        foreach (string filterTerm in filterTerms)
            combinedFilters = combinedFilters.Filter(filter => filter.Term(filterTerm, ...));

        return combinedFilters;
    }))
    .Take(1000));

I ended up with this, seems to work as needed :) 我最终得到了这个,似乎按需要工作:)

 var result = ElasticClient.Search<Page>(
            body => body.Query(query => query.ConstantScore(csq => csq.Filter(f =>
            {
                var ff = f.Term("tags.Size", "17");

                // This will be replaced with a loop containing filter terms
                ff &= f.Term("tags.Size", "16");
                ff &= f.Term("tags.Size", "19");

                return ff;
            }))).Take(1000));

Thanks for your answer that got me in the right direction Thomas :) 谢谢你的回答让我走向了正确的方向托马斯:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM