代码之家  ›  专栏  ›  技术社区  ›  DLeh

Elasticsearch NEST提升现有查询

  •  0
  • DLeh  · 技术社区  · 6 年前

    QueryBuilder 这有助于我们根据需要动态构建弹性查询。因此,我们的过滤是使用如下结构构建的: AllFilters &= Query<TModel>.Must(requirement);

    然后,当我们构建查询时,在哪里 sd 是一个 SearchDescriptor<TModel> :

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

    我的问题是,如何正确地将boosting添加到这个查询中?

    我有一个像 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"
                  }
                }
              }
            }
          ]
        }
      }
    }
    

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

    0 回复  |  直到 6 年前