Elasticsearch NEST提升现有查询

时间:2018-11-27 18:19:12

标签: c# elasticsearch nest

我们有一个复杂的系统来生成弹性查询。我们有QueryBuilder之类的对象,可以帮助我们根据需要动态构建弹性查询。因此,我们的过滤器是使用类似AllFilters &= Query<TModel>.Must(requirement);

的结构构建的

然后,当我们构建查询时,我们将执行此操作,其中sdSearchDescriptor<TModel>

        var fullQuery = Query<TModel>.Bool(b => b
            .Must(m => m
                .Bool(b2 => b2
                    .Must(Filter, OrFilter)
            )));

        sd = sd.Query(_ => fullQuery);

我的问题是,如何为该查询正确添加增强功能?

我有一个类似List<QueryContainer> AllBoosts的构造,我列举了这样的构造:

    protected IEnumerable<QueryContainer> GetBoosts()
    {
        if (Boosts.OrEmptyIfNull().Any())
        {
            foreach(var boost in Boosts)
            {
                yield return Query<TModel>.Boosting(b => b.Boost(2).Positive(p => boost));
            }
        }
    }

然后尝试将其&=放入SearchDescriptor,但这不会返回任何结果:

        var fullQuery = Query<TModel>.Bool(b => b
            .Must(m => m
                .Bool(b2 => b2
                    .Must(Filter, OrFilter) //these are filters we've built up with conditions in the code
            )));

        //here i enumerate boosts defined in the class
        foreach (var boost in GetBoosts())
            fullQuery &= boost; //should this be &=, |=, or something else?

        sd = sd.Query(_ => fullQuery);

输出查询:

{
  "from": 0,
  "size": 50,
  "sort": [
    {
      "name": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          //various must clauses from class properties / settings here
        },
        {
          "boosting": {
            "boost": 2.0,
            "positive": {
              "nested": {
                "query": {
                  "terms": {
                    "companies.id": [
                      "500086674"
                    ]
                  }
                },
                "path": "companies"
              }
            }
          }
        }
      ]
    }
  }
}

在不更改过滤器原始构造的情况下,将提升合并到查询中的正确方法是什么?弹性版本5.4

0 个答案:

没有答案