使用 Nest DSL 语法过滤多个术语的 ElasticSearch 嵌套查询无法按预期工作
ElasticSearch nested query filtered with multiple terms using Nest DSL syntax not working as expected
我正在尝试构建一个嵌套查询,该查询需要在嵌套对象上按多个术语进行过滤。我正在使用 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"
}
}
}
我正在尝试构建一个嵌套查询,该查询需要在嵌套对象上按多个术语进行过滤。我正在使用 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"
}
}
}