将多站点查询写入对象初始值设定项语法

时间:2016-05-06 14:19:04

标签: elasticsearch nest

我有一个multiSearch查询,如下所示。基本上我查询产品和类别类型。我想使这个查询可选,而无需再次编写相同的代码。 基本上在某些情况下我只想查询产品类型,这意味着它不会多搜索,而是搜索查询。如何将此查询拆分为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&amp;&amp; SearchCategoryQuery)

有可能吗?

1 个答案:

答案 0 :(得分:2)

这种流畅的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的另一个例子。我会考虑将此添加到the documentation