嵌套多搜索查询编写为对象初始值设定项语法

Nest multisearch query writing as object initializer Syntax

我有一个如下所示的多搜索查询。基本上我查询产品和类别类型。我想让这个查询成为可选的,而不用再次编写相同的代码。 基本上在某些情况下我只想查询产品类型,这意味着它不会进行多重搜索而是搜索查询。如何将此查询拆分为 2 个搜索查询。我想像下面这样。

    return Client.MultiSearch(ms => ms
        .Search<Product>("products", s => s
        .Index(IndexName)
        .Explain(explain)
          .Query(q => q
             .Bool(b => b
                 .Should(
                     sh => sh.MultiMatch(qs => qs
                         .Fields(d => d                         
                             .Field(Name + ".raw", NameBoost + 0.5)
                             .Field(Name, NameBoost)                            
                            .Type(TextQueryType.BestFields)
                            .Query(key))
                            ))).From(startfrom).Size(size))
.Search<Category>("categories", s => s
.Index(IndexName)
.Explain(explain)
.Query(q => q.
Bool(b => b.
Should(sh => sh.
MultiMatch(m => m
.Fields(d => d
.Field(f => f.Name, NameBoost)
.Field(p => p.Name.Suffix("raw"), NameBoost + 0.5)).Type(TextQueryType.BestFields)
.Query(key)
)
))).From(startfrom).Size(size))
);

像下面这样的东西。根据 this 文章

,我猜它被称为对象初始化器语法

Client.MultiSearch(SearchProductQuery && SearchCategoryQuery)

可能吗?

本流利API多搜索

client.MultiSearch(ms => ms
    .Search<Product>("products", s => s
        .Index(IndexName)
        .Explain(explain)
        .Query(q => q
           .Bool(b => b
                .Should(sh => sh
                    .MultiMatch(qs => qs
                        .Fields(d => d
                            .Field(Name + ".raw", NameBoost + 0.5)
                            .Field(Name, NameBoost)
                        )
                        .Type(TextQueryType.BestFields)
                        .Query(key)
                    )
                )
            )
        )
        .From(startfrom)
        .Size(size)
    )
    .Search<Category>("categories", s => s
        .Index(IndexName)
        .Explain(explain)
        .Query(q => q
            .Bool(b => b
                .Should(sh => sh
                    .MultiMatch(m => m
                        .Fields(d => d
                            .Field(f => f.Name, NameBoost)
                            .Field(p => p.Name.Suffix("raw"), NameBoost + 0.5)
                        )
                        .Type(TextQueryType.BestFields)
                        .Query(key)
                    )
                )
            )
        )
        .From(startfrom)
        .Size(size)
    )
);

会是这个OIS API多搜索

var multiSearch = new MultiSearchRequest
{
    Operations = new Dictionary<string, ISearchRequest>
    {
        { "products", new SearchRequest<Product>(IndexName)
            {
                Explain = true,
                Query = new BoolQuery
                {
                    Should = new QueryContainer[] {
                        new MultiMatchQuery
                        {
                            Fields = 
                                ((Fields)Field.Create(Name + ".raw", NameBoost + 0.5))
                                .And(Name, NameBoost),
                            Type = TextQueryType.BestFields,
                            Query = key
                        }
                    }
                },
                From = startfrom,
                Size = size
            }
        },
        { "categories", new SearchRequest<Category>(IndexName)
            {
                Explain = true,
                Query = new BoolQuery
                {
                    Should = new QueryContainer[] {
                        new MultiMatchQuery
                        {
                            Fields =    
                                ((Fields)Infer.Field<Category>(f => f.Name, NameBoost))
                                .And<Category>(f => f.Name.Suffix("raw"), NameBoost + 0.5),
                            Type = TextQueryType.BestFields,
                            Query = key
                        }
                    }
                },
                From = startfrom,
                Size = size
            }
        },
    }
};

client.MultiSearch(multiSearch);

看看multi search integration tests for another example. I'll look at getting this added to the documentation