假设我们在以下结构中存储了类型为vehicle的对象,这些对象具有对类型所有者的引用。然后运行以下请求:
`POST:本地主机:9200/15 / vehicles / _search'
具有以下主体:
{ "query": { "wildcard": {"make":"*toy*"} }}
返回相关对象:
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "15.index",
"_type": "vehicle",
"_id": "352",
"_score": 1,
"_source": {
"id": "352",
"name": "toyota",
"owner_id": "12",
"owner": {
"id": "12",
"name": "John Smith",
"login_id": 1,
"active": true,
}
}
}
]
}
我现在尝试按嵌套对象查询(例如,属于用户John Smith的所有车辆)
{"query": {"wildcard": {"owner.name": "*John*"}}}
不返回任何结果,而:
{"query": {"wildcard": {"owner": {"name": "*John*"}}}}
错误出在:
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {....
[4]: SearchParseException[....
Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[....
[wildcard] query does not support [name]]; }{....
[3]: SearchParseException[....
: Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[....
[wildcard] query does not support [name]]; }{....
[2]: SearchParseException[....
]: Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[....
[wildcard] query does not support [name]]; }{....
: SearchParseException[[....
: Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[....
[wildcard] query does not support [name]]; }{....
[0]: SearchParseException[....: Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[.... [wildcard] query does not support [name]]; }]",
"status": 400
针对嵌套对象的查询(通配符或其他方式)的正确格式是什么?通配符查询与该模板有何具体区别(如果有的话)?
您需要使用嵌套查询来查询nested
类型内的字段。
{
"query":{
"nested":{
"path":"owner",
"query":{
"wildcard":{
"owner.name":"John*"
}
}
}
}
}
另外,您不应该以通配符开头,因为它会导致查询非常慢。
如果您有多个级别的nested
对象,则path
值应该是最深级别的nested
对象,而查询中的属性应该是完整路径。
{
"query":{
"nested":{
"path":"owner.pets",
"query":{
"wildcard":{
"owner.pets.name":"{someValue}"
}
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句