【发布时间】:2016-10-11 17:50:32
【问题描述】:
我正在使用 NEST 2.3.2 来查询 ElasticSearch,并且需要根据可选的布尔值为 FollowUpDateTime 字段创建条件过滤器。如果那个布尔值
- 为真 - 希望查看所有具有非空白日期的记录,
- 是假的 - 没有日期的记录,
- 为空 - 显示所有记录。
这是我在 Nest 中的内容:
var containers = new QueryContainer[7];
//Size is based on hte number of search parameters..
var descriptor = new QueryContainerDescriptor<EsNoteModel>();
var filters = new QueryContainer[1];
var filterdescriptor = new QueryContainerDescriptor<EsNoteModel>();
/*...... some code for other parameters here .....*/
if (hasFollowUpDate.HasValue)
{
if ((bool) hasFollowUpDate)
{
//If true shows only with a non-blank follow-up date
filters[0] = filterdescriptor.Exists(ff => ff.Field("FollowUpDateTime"));
}
else
{
//If false shows only notes with a blank follow-up date
filters[0] = filterdescriptor.Missing(ff => ff.Field("FollowUpDateTime"));
}
}
/*...... CALL to ES below .....*/
var result = ElasticSearchClient.Search<EsNoteModel>(body => body
.From(offset - 1)
.Size(rows)
.Query(q => q
.Bool(b => b
.Must(containers)
.Filter(f => f.Bool(t => t.Must(filters)))
)
)
.Sort(s => s
.Field(f => f
.Field(p => p.NoteDate)
.Order(SortOrder.Descending)))
);
问题是上面的查询产生了错误。请看下面:
由不成功的低级别调用构建的无效 NEST 响应 POST:/_search?pretty=true
此 API 调用的审计跟踪:
- BadResponse:节点:http://**********.es.amazonaws.com/ 使用:00:00:00.1381301
OriginalException:System.Net.WebException:远程服务器返回错误:(400)错误请求。在
System.Net.HttpWebRequest.GetResponse() 在 Elasticsearch.Net.HttpConnection.Request[TReturn](RequestData 请求数据)
# Request:
{
"from": 0,
"size": 25,
"sort": [
{
"NoteDate": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"match": {
"CustomerId": {
"query": "2810855"
}
}
}
],
"filter": [
{
"bool": {
"must": [
{
"exists": {
"field": "FollowUpDateTime"
}
}
]
}
}
]
}
}
}
回复如下:
# Response:
{
"error" : "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[-t7SwBo_TjeH_98h_56ANw][customernote][0]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][0]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@2d55231>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"28_10855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[TOOugCq3ROqF9CGnHIay2g][customernote][1]: SearchParseException[[customernote][1]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@562f97b5>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"28_10855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[TOOugCq3ROqF9CGnHIay2g][customernote][2]: SearchParseException[[customernote][2]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@394cf46e>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"28_10855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[-t7SwBo_TjeH_98h_56ANw][customernote][3]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][3]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@46d07ba8>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"28_10855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[TOOugCq3ROqF9CGnHIay2g][customernote][4]: SearchParseException[[customernote][4]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@74fb3710>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"28_10855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }]",
"status" : 400
}
如您所见错误报告“嵌套:QueryParsingException[[customernote] bool 查询不支持 [filter]];”任何想法我在这个例子中做错了什么?这是对过滤子句的正确使用吗?
**** 更新
更改查询
var result = ElasticSearchClient.Search<EsNoteModel>(body => body
.From(offset - 1)
.Size(rows)
.Query(q => q
.Bool(b => b
.Must(containers)
.Filter(filters)
)
)
.Sort(s => s
.Field(f => f
.Field(p => p.NoteDate)
.Order(SortOrder.Descending)))
);
请求
# Request:
{
"from": 0,
"size": 25,
"sort": [
{
"NoteDate": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"match": {
"CustomerId": {
"query": "2810855"
}
}
}
],
"filter": [
{
"exists": {
"field": "FollowUpDateTime"
}
}
]
}
}
}
收到响应错误:
# Response:
{
"error" : "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[TOOugCq3ROqF9CGnHIay2g][customernote][0]: SearchParseException[[customernote][0]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@4839a60c>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"2810855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[-t7SwBo_TjeH_98h_56ANw][customernote][1]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][1]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@21d845ae>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"2810855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[-t7SwBo_TjeH_98h_56ANw][customernote][2]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][2]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@6124d214>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"2810855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[TOOugCq3ROqF9CGnHIay2g][customernote][3]: SearchParseException[[customernote][3]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@f884425>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"2810855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[-t7SwBo_TjeH_98h_56ANw][customernote][4]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][4]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@77228f64>!]: Parse Failure [Failed to parse source [{\r\n \"from\": 0,\r\n \"size\": 25,\r\n \"sort\": [\r\n {\r\n \"NoteDate\": {\r\n \"order\": \"desc\"\r\n }\r\n }\r\n ],\r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n {\r\n \"match\": {\r\n \"CustomerId\": {\r\n \"query\": \"2810855\"\r\n }\r\n }\r\n }\r\n ],\r\n \"filter\": [\r\n {\r\n \"exists\": {\r\n \"field\": \"FollowUpDateTime\"\r\n }\r\n }\r\n ]\r\n }\r\n }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }]",
"status" : 400
}
基本一样的错误
嵌套:QueryParsingException[[customernote] bool 查询不支持[filter]];
【问题讨论】:
-
布尔查询只能有一个从 elasticsearch 2 开始的过滤器。
-
使用 Elasticsearch.Net.2.3.2
-
这是您正在使用的客户端,但服务器是 1.5.2(因为这是您似乎正在使用的 aws elasticsearch 服务提供的唯一版本)
-
弗雷德里克 - 你值得一颗星!谢谢
-
NEST 的主要版本与 Elasticsearch 的主要版本兼容,因此对于 Elasticsearch 1.5.2,您应该使用最新的 NEST 1.x,目前为 1.8.2。查看 github 上的兼容性矩阵 - github.com/elastic/elasticsearch-net
标签: amazon-web-services elasticsearch nest