在 C# 中使用 NEST 库调用 elasticsearch 时,如何将条件逻辑添加到 linq 语句?

How to add conditional logic to a linq statement when calling elasticsearch using the NEST library in C#?

我正在尝试使用 C# 中的 Fluent LINQ 来使用 .NET NEST library.

查询 Elasticsearch 服务器

我想根据传入的搜索请求构建 LINQ 语句。如果搜索请求有价格范围,我想在 Elasticsearch 请求中添加一个范围子句。这是我的代码

var products = await client.SearchAsync<Product>(x =>
{
      x = x.Query(q =>
      {
          if (request.IsAvailable.HasValue)
          {
               // this gets called, but it is never added to the final elasticsearch call.
              q = q.Match(b => b.Field(bm => bm.IsAvailable).Query(request.IsAvailable.Value ? "true" : "false")) as QueryContainerDescriptor<Product>;
          }

          if (request.MinPrice.HasValue || request.MaxPrice.HasValue)
          {
              // this gets called, but it is never added to the final elasticsearch call.
              q = q.Range(r =>
              {
                  if (request.MinPrice.HasValue)
                  {
                      r = r.Field(x => x.Price).GreaterThanOrEquals(request.MinPrice.Value);
                  }

                  if (request.MaxPrice.HasValue)
                  {
                      r = r.Field(x => x.Price).LessThanOrEquals(request.MaxPrice.Value);
                  }
                  return r;
              }) as QueryContainerDescriptor<Product>;
          }

          if (request.Types != null && request.Types.Length > 0)
          {
              // this gets called, but it is never added to the final elasticsearch call.
              q = q.Terms(t => t.Field(f => f.Type).Terms(request.Types)) as QueryContainerDescriptor<Product>;
          }

          return q;
      });

      x = x.Source(s => s.Excludes(se =>
      {
          // This works! There fields are being excluded as expected
          if (request.ExcludeSummary)
          {
              se = se.Field(sef => sef.Summary);
          }

          if (request.ExcludeTimestamp)
          {
              se = se.Field(sef => sef.Timestamp);
          }

          if (request.ExcludeLabels)
          {
              se = se.Field(sef => sef.Labels);
          }

          if (request.ExcludeTags)
          {
              se = se.Field(sef => sef.Tags);
          }

          return se;
      }));

      return x;
});

我在 Query() 中的所有条件都没有添加到 elasticsearch 请求中。这意味着生成的 json 请求没有价格或 IsAvaliable 的子句。我怀疑转换是罪魁祸首,但不确定如何解决它。

Source() 正在按预期工作。它将正确的字段添加到 excludes 列表。

如何将查询子句正确添加到 Query() 部分?

我想通了这个问题。似乎NEST采用了最后一个子句并忽略了前面的所有子句。

这是我为修复它所做的工作

var products = await client.SearchAsync<Product>(x =>
{
      x = x.Query(q =>
      {
          QueryContainer qc = q;

          if (request.IsAvailable.HasValue)
          {
              qc = qc && +q.Match(b => b.Field(bm => bm.IsAvailable).Query(request.IsAvailable.Value ? "true" : "false"));
          }

          if (request.MinPrice.HasValue || request.MaxPrice.HasValue)
          {
              qc = qc && +q.Range(r =>
              {
                  if (request.MinPrice.HasValue)
                  {
                      r = r.Field(x => x.Price).GreaterThanOrEquals(request.MinPrice.Value);
                  }

                  if (request.MaxPrice.HasValue)
                  {
                      r = r.Field(x => x.Price).LessThanOrEquals(request.MaxPrice.Value);
                  }
                  return r;
              });
          }

          if (request.Types != null && request.Types.Length > 0)
          {
              qc = qc && +q.Terms(t => t.Field(f => f.Type).Terms(request.Types));
          }

          return qc;
      });

      x = x.Source(s => s.Excludes(se =>
      {
          if (request.ExcludeSummary)
          {
              se = se.Field(sef => sef.Summary);
          }

          if (request.ExcludeTimestamp)
          {
              se = se.Field(sef => sef.Timestamp);
          }

          if (request.ExcludeLabels)
          {
              se = se.Field(sef => sef.Labels);
          }

          if (request.ExcludeTags)
          {
              se = se.Field(sef => sef.Tags);
          }

          return se;
      }));

      return x;
});