我正在使用NEST 2.3.2查询ElasticSearch,并且需要基于可选布尔值为FollowUpDateTime字段创建一个条件过滤器。如果该布尔
- 为true-希望查看所有日期为非空的记录
- 是虚假的-没有日期的记录
- 为null-显示所有记录
以下是我在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:/搜索(_S)?漂亮=真正的
此API调用的审核跟踪:
- BadResponse:节点:http://**********.es.amazonaws.com/时间:00:00:00.1381301
OriginalException:System.Net.WebException:远程服务器返回错误:(400)错误请求。在
System.Net.HttpWebRequest.GetResponse()位于Elasticsearch.Net.HttpConnection.Request[TReturn](RequestDatarequestData)
# 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "28_10855"rn }rn }rn }rn ],rn "filter": [rn {rn "bool": {rn "must": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn ]rn }rn }rn}]]]; 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "28_10855"rn }rn }rn }rn ],rn "filter": [rn {rn "bool": {rn "must": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn ]rn }rn }rn}]]]; 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "28_10855"rn }rn }rn }rn ],rn "filter": [rn {rn "bool": {rn "must": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn ]rn }rn }rn}]]]; 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "28_10855"rn }rn }rn }rn ],rn "filter": [rn {rn "bool": {rn "must": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn ]rn }rn }rn}]]]; 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "28_10855"rn }rn }rn }rn ],rn "filter": [rn {rn "bool": {rn "must": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn ]rn }rn }rn}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }]",
"status" : 400
}
正如您所看到的错误报告"嵌套:QueryParsingException[[customernote]bool query不支持[filter]];"你知道我在这个例子中做错了什么吗?这是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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "2810855"rn }rn }rn }rn ],rn "filter": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn}]]]; 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "2810855"rn }rn }rn }rn ],rn "filter": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn}]]]; 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "2810855"rn }rn }rn }rn ],rn "filter": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn}]]]; 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "2810855"rn }rn }rn }rn ],rn "filter": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn}]]]; 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 [{rn "from": 0,rn "size": 25,rn "sort": [rn {rn "NoteDate": {rn "order": "desc"rn }rn }rn ],rn "query": {rn "bool": {rn "must": [rn {rn "match": {rn "CustomerId": {rn "query": "2810855"rn }rn }rn }rn ],rn "filter": [rn {rn "exists": {rn "field": "FollowUpDateTime"rn }rn }rn ]rn }rn }rn}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }]",
"status" : 400
}
基本相同的错误
嵌套:QueryParsingException[[customernote]bool查询不支持[filter]];
显然,我们在.NET 4.0项目中有以下对的引用
- Elasticsearch.Net-官方低级别弹性搜索客户端packages\Elasticsearch.Net.2.2\lib\net45\Elasticearch.Net.dll
- NEST-官方高级弹性搜索客户端packages\NEST.2.3.2\lib\net45\NEST.dll
有趣的事实是,它们仍然使用不推荐使用的查询。正如弗雷德里克在这里提到的
您正在使用的客户端,但服务器是1.5.2(因为这是aws弹性搜索服务提供的唯一版本,您可以似乎正在使用)
见下文-它有效:
var query = new QueryContainer[8];//Size is based on hte number of search parameters
var descriptor = new QueryContainerDescriptor<EsNoteModel>();
/*........... skipping here some code for other parameters ..........*/
if (hasFollowUpDate.HasValue)
{
if ((bool) hasFollowUpDate)
{
//If true shows only with a non-blank follow-up date
query[7] = descriptor.Filtered(p => p.Filter(f => f.Exists(r => r.Field(u => u.FollowUpDateTime))));
}
else
{
////If false shows only notes with a blank follow-up date
query[7] = descriptor.Filtered(p => p.Filter(f => f.Missing(r => r.Field(u => u.FollowUpDateTime))));
}
}
var result = ElasticSearchClient.Search<EsNoteModel>(body => body
.From(offset - 1)
.Size(rows)
.Query(q => q
.Bool(b => b
.Must(query)
)
)
.Sort(s => s
.Field(f => f
.Field(p => p.NoteDate)
.Order(SortOrder.Descending)))
);
因此,简单地将BOOL一词替换为Filtered,就成功了
您可以使用QueryContainer
或FilterContainer
动态执行此操作。以下是我们的解决方案中非常相似的一个:
var query = new List<QueryContainer>
{
Query<AlertForReporting>.Range(r => r.OnField(f => f.AlertDate)
.Greater(start.ToEPCISFormat())
.Lower(end.ToEPCISFormat())
)
};
var result = ElasticClient.Search<AlertForReporting>(s =>
s.Size(0)
.Query(q =>
q.Bool(b =>
b.Must(query.ToArray())
)
));