为什么 'exists' 对正常查询失败而不对过滤查询失败?
Why does 'exists' fail for normal queries and not for filtered queries?
考虑以下两个查询:
{
"_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 [version < 2.0] 其中 exists
是一个过滤器。有 no exists
查询,这就是您收到错误消息的原因:No query registered for [exists]
.
您可以使用:
{
"_source": false,
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "comment"
}
}
]
}
}
}
或者简单地说:
{
"_source": false,
"filter": {
"exists": {
"field": "comment"
}
}
}
从 Elasticsearch 2.0 版本开始,query 和 filter 对象已经合并在一起,只存在 query 能够执行这两种功能的对象。因此,从 2.0 版开始,您可以使用 exists
查询。
您可以在以下位置阅读有关合并的更多信息:Better query execution coming to Elasticsearch 2.0
考虑以下两个查询:
{
"_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 [version < 2.0] 其中 exists
是一个过滤器。有 no exists
查询,这就是您收到错误消息的原因:No query registered for [exists]
.
您可以使用:
{
"_source": false,
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "comment"
}
}
]
}
}
}
或者简单地说:
{
"_source": false,
"filter": {
"exists": {
"field": "comment"
}
}
}
从 Elasticsearch 2.0 版本开始,query 和 filter 对象已经合并在一起,只存在 query 能够执行这两种功能的对象。因此,从 2.0 版开始,您可以使用 exists
查询。
您可以在以下位置阅读有关合并的更多信息:Better query execution coming to Elasticsearch 2.0