gpt4 book ai didi

c# - 使用Nest DSL语法用多个术语过滤的ElasticSearch嵌套查询无法按预期工作

转载 作者:行者123 更新时间:2023-12-02 23:58:45 25 4
gpt4 key购买 nike

我正在尝试建立一个嵌套查询,该查询需要在嵌套对象上按多个术语进行过滤。我正在使用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语法,它对重构更友好。

最佳答案

尝试这样的事情:

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"
}
}
}

关于c# - 使用Nest DSL语法用多个术语过滤的ElasticSearch嵌套查询无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50968844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com