嵌套弹性 - 构建动态嵌套查询 [英] Nest Elastic - Building Dynamic Nested Query

查看:199
本文介绍了嵌套弹性 - 构建动态嵌套查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Nest查询嵌套对象,但是查询是以动态方式构建的。以下是以静态方式使用嵌套书籍查询的代码

I have to query a nested object using Nest, however the query is built in dynamic way. Below is code that demonstrate using query on nested "books" in a static way

QueryContainer qry;
         qry = new QueryStringQuery()
         {
             DefaultField = "name",
             DefaultOperator = Operator.And,
             Query = "salman"
         };

         QueryContainer qry1 = null;

         qry1 = new RangeQuery() // used to search for range ( from , to)
         {
             Field = "modified",
             GreaterThanOrEqualTo = Convert.ToDateTime("21/12/2015").ToString("dd/MM/yyyy"),
         };

         QueryContainer all = qry && qry1;

            var results = elastic.Search<Document>(s => s
               .Query(q => q
                    .Bool(qb => qb
                        .Must(all)))
                .Filter(f =>
                        f.Nested(n => n
                             .Path("books")
                                .Filter(f3 => f3.And(
                                            f1 => f1.Term("book.isbn", "122"),
                                            f2 => f2.Term("book.author", "X"))

                                        )
                                )
                        )   

                );

问题是我需要结合多个查询(使用AND,OR运算符)为书以动感的方式。例如,获得满足这些条件的书籍:

The problem is that i need to combine multiple queries (using And,OR operators) for "books" in dynamic fashion. For example, get the books that satisfy these set of conditions:


  1. 条件1:具有作者X和isbn1的图书条件2:具有作者X和isbn2的图书

  2. 条件3:具有作者Z和isbn的图书 3

  3. 其他条款:.....

现在,嵌套查询应该在以下情况下检索书籍:

条件1 AND 条件2 条件3

Now, the filter in the nested Query should retrieve books if:
Condition 1 AND Condition 2 Or Condition 3

假设我有类名称FilterOptions包含以下属性:

Suppose that i have class name FilterOptions that contains the following attributes:


  1. FieldName


  2. 运算符(将结合下一个过滤器)

我将循环给定FilterOptions数组来构建查询。

I am going to loop on the given FilterOptions array to build the query.


问题:

Question:

我应该用什么来构建嵌套查询?是否是FilterDesciptor,如何组合它们将嵌套查询添加到搜索方法?

What should i use to build the nested query? Is it a FilterDesciptor and how to combine them add the nested query to the Search Method?

请推荐任何有价值的链接或示例?

Please, recommend any valuable link or example?

推荐答案

我同意paweloque,似乎你的前两个条件是矛盾的,如果AND-ed在一起,就不行。忽略,这是我的解决方案。我已经实现了这样一种方式,允许超过三种具体条件。我也觉得在 bool 语句中更适合。

I agree with paweloque, it seems your first two conditions are contradictory and wouldn't work if AND-ed together. Ignoring that, here's my solution. I've implemented this in such a way that allows for more than the three specific conditions you have. I too feel it would fit better in a bool statement.

QueryContainer andQuery = null;
QueryContainer orQuery = null;
foreach(var authorFilter in FilterOptions.Where(f=>f.Operator==Operator.And))
{
    andQuery &= new TermQuery
    {
        Field = authorFilter.FieldName,
        Value = authorFilter.Value
    };
}
foreach(var authorFilter in FilterOptions.Where(f=>f.Operator==Operator.Or))
{
    orQuery |= new TermQuery
    {
        Field = authorFilter.FieldName,
        Value = authorFilter.Value
    };
}

之后,在 .Nested call我会把:

After that, in the .Nested call I would put:

.Path("books")
    .Query(q=>q
        .Bool(bq=>bq
            .Must(m=>m.MatchAll() && andQuery)
            .Should(orQuery)
    ))

这篇关于嵌套弹性 - 构建动态嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆