【问题标题】:ElasticSearch nested query filtered with multiple terms using Nest DSL syntax not working as expected使用 Nest DSL 语法过滤多个术语的 ElasticSearch 嵌套查询未按预期工作
【发布时间】:2018-06-22 04:10:22
【问题描述】:

我正在尝试构建一个嵌套查询,该查询需要按嵌套对象上的多个术语进行过滤。我正在使用 Nest nuget 包版本 6.1

查询是使用 Nest DSL 语法构建的,如下所示:

queryContainer &= Query<PropertyDTO>.Nested(n => n
    .Path(p => p.Publications)
    .Query(q =>
        q.Term(t => t
            .Field(ff => ff.Publications.First().Id == publicationId.ToString(CultureInfo.InvariantCulture))
        )
        && q.DateRange(r =>
            r
            .Field(f => f.Publications.First().PublishedOn)
            .GreaterThanOrEquals(
                from.HasValue
                ? DateMath.Anchored(from.Value)
                : DateMath.Anchored(DateTime.MinValue)
            )
            .LessThanOrEquals(
                to.HasValue
                ? DateMath.Anchored(to.Value)
                : DateMath.Anchored(DateTime.Now)
            )
        )
    )
);

预期的结果应该是:

{
  "nested": {
    "query": 
        {
          "bool": {
            "must": [
              {
                "range": {
                  "publications.publishedOn": {
                    "gte": "2018-06-13T00:00:00",
                    "lte": "2018-06-20T23:59:59"
                  }
                }
              },
              {
                "term": {
                  "publications.id": {
                    "value": "1.510136"
                  }
                }
              }
            ]
          }
        },
    "path": "publications"
  }
}

但相反,我得到:

{
  "nested": {
    "query": {
      "term": {
        "publications.id": {
          "value": "1.510136"
        }
      }
    },
    "path": "publications"
  }
},
{
  "nested": {
    "query": {
      "range": {
        "publications.publishedOn": {
          "gte": "2018-06-14T00:00:00",
          "lte": "2018-06-21T23:59:59"
        }
      }
    },
    "path": "publications"
  }
}

我做错了什么?

目前我正在使用基于原始查询版本的解决方法,但我想使用更易于重构的 Nest DSL 语法。

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    试试这样的:

    lESResponse = lESClient.Search<PropertyDTO>(s => s
                        .Query(q => q
                            .Nested(n => n
                                .Path("publications")
                                .Query(qu => qu
                                    .Bool(b => b
                                        .Must(new QueryContainer[] {
                                            new DateRangeQuery() {
                                                Field = "publications.publishedOn",
                                                GreaterThanOrEqualTo = DateMath.Anchored(DateTime.MinValue),
                                                LessThanOrEqualTo = DateMath.Anchored(DateTime.Now)
                                            },
                                            new TermQuery() {
                                                Field = "publications.id",
                                                Value = "1.510136"
                                            }
                                        } ))))));
    

    下面是输出:

    {
      "query": {
        "nested": {
          "query": {
            "bool": {
              "must": [
                {
                  "range": {
                    "publications.publishedOn": {
                      "gte": "",
                      "lte": "2018-06-21T18:35:17.1274477+05:30"
                    }
                  }
                },
                {
                  "term": {
                    "publications.id": {
                      "value": "1.510136"
                    }
                  }
                }
              ]
            }
          },
          "path": "publications"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      相关资源
      最近更新 更多