考虑以下两个查询:
{
"_source" : false,
"query": {
"bool": {
"must": [
{
"range": {
"date": {
"gte": 1460789348000,
"lte": 1466059748000
}
}
}
]
}
}
}
现在考虑添加了
filtered
和filter
字段的以下查询:{
"_source": false,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"range": {
"date": {
"gte": 1460789348000,
"lte": 1466059748000
}
}
}
]
}
}
}
}
}
两者都给出相同的结果,并且没有
filtered
和filter
字段的第一个查询不会导致错误,这与以下查询不同:{
"_source": false,
"query": {
"bool": {
"must": [
{
"exists": {
"field": "comment"
}
}
]
}
}
}
错误:
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[bi8BrruxR9i_H8-n4Qo9Mg][acct_prop_5][0]: RemoteTransportException[[Battletide][inet[/172.16.0.11:9302]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[acct_prop_5][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[acct_prop_5] No query registered for [exists]]; }{[JorHtifCSxGrbb3ovGJMWQ][advocacy_insights_p5_v1_00000000_0000][0]: RemoteTransportException[[Caiera][inet[/172.16.0.11:9304]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[advocacy_insights_p5_v1_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[advocacy_insights_p5_v1_00000000_0000] No query registered for [exists]]; }{[Tlt3tXF_S9-rEmK9iwSvoA][audience_act_p5_v2_00000000_0000][0]: RemoteTransportException[[Monet St. Croix][inet[/172.16.0.11:9303]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[audience_act_p5_v2_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[audience_act_p5_v2_00000000_0000] No query registered for [exists]]; }{[NTpZwKeYRI6l6zHf2BlIFg][audience_act_p5_v3_00000000_0000][0]: SearchParseException[[audience_act_p5_v3_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[audience_act_p5_v3_00000000_0000] No query registered for [exists]]; }{[NTpZwKeYRI6l6zHf2BlIFg][audience_act_p5_v3_20150601_0000][0]: SearchParseException[[audience_act_p5_v3_20150601_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[audience_act_p5_v3_20150601_0000] No query registered for [exists]]; }{[Tlt3tXF_S9-rEmK9iwSvoA][auth2][0]: RemoteTransportException[[Monet St. Croix][inet[/172.16.0.11:9303]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[auth2][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[auth2] No query registered for [exists]]; }{[JorHtifCSxGrbb3ovGJMWQ][authors][0]: RemoteTransportException[[Caiera][inet[/172.16.0.11:9304]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[authors][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[authors] No query registered for [exists]]; }{[bi8BrruxR9i_H8-n4Qo9Mg][bm_content_lifetime_p5_v1_00000000_0000][0]: RemoteTransportException[[Battletide][inet[/172.16.0.11:9302]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[bm_content_lifetime_p5_v1_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[bm_content_lifetime_p5_v1_00000000_0000] No query registered for [exists]]; }{[NTpZwKeYRI6l6zHf2BlIFg][bm_content_lifetime_p5_v2_00000000_0000][0]: SearchParseException[[bm_content_lifetime_p5_v2_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: QueryParsingException[[bm_content_lifetime_p5_v2_00000000_0000] No query registered for [exists]]; }{[bi8BrruxR9i_H8-n4Qo9Mg][bm_content_lifetime_p5_v93_00000000_0000][0]: RemoteTransportException[[Battletide][inet[/172.16.0.11:9302]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[bm_content_lifetime_p5_v93_00000000_0000][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"_source":false,"query":{"bool":{"must":[{"exists":{"field":"comment"}}]}}}]]]; nested: .........
而对于这两个字段,它将变为正确的查询:
{
"_source": false,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "comment"
}
}
]
}
}
}
}
}
range
和exists
查询有什么区别,后者仅允许一个查询有效,而没有filtered
和filter
字段?
最佳答案
您可能必须使用旧版本的Elasticsearch [版本<2.0 ],其中exists
是过滤器。没有没有 exists
查询,这就是为什么您收到错误消息:No query registered for [exists]
的原因。
相反,您可以使用:
{
"_source": false,
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "comment"
}
}
]
}
}
}
或者简单地:
{
"_source": false,
"filter": {
"exists": {
"field": "comment"
}
}
}
从Elasticsearch 2.0版开始,查询和过滤器对象已合并在一起,并且仅存在能够同时实现这两种功能的查询对象。因此,您可以从2.0版开始使用
exists
查询。您可以在Better query execution coming to Elasticsearch 2.0上了解有关合并的更多信息
关于elasticsearch - 为什么 'exists'对于普通查询而不是过滤查询失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37860227/