我正在尝试构建一个嵌套查询,该查询需要在嵌套对象上按多个术语进行过滤。我正在使用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语法,它对重构更友好。
答案 0 :(得分:0)
尝试这样的事情:
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"
}
}
}
答案 1 :(得分:0)
我尝试了基于iKishanSojitra答案的替代方法,但它也不起作用
queryContainer &= Query<PropertyDTO>.Nested(n => n
.Path(p => p.Publications)
.Query(qu => qu
.Bool(b => b
.Must(
new QueryContainer[]
{
Query<PropertyDTO>.Term(t => t
.Field(f => f.Publications.First().Id == publicationId.ToString(CultureInfo.InvariantCulture))
),
Query<PropertyDTO>.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)
)
)
}
)
)
)
);