Reputation: 15481
Let's say we are storing objects of type vehicle which have a reference to a type owner in the following structure. Then Running the following request:
`POST: localhost:9200/15/vehicles/_search'
with the following body:
{ "query": { "wildcard": {"make":"*toy*"} }}
returns the relevant objects:
"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,
}
}
}
]
}
I am now attempting to query by nested object (e.g. all vehicles that belong to user John Smith)
{"query": {"wildcard": {"owner.name": "*John*"}}}
returns no results, while:
{"query": {"wildcard": {"owner": {"name": "*John*"}}}}
errors out with:
"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
What is the correct format for a query (wildcard or otherwise) against a nested object? How specifically do wildcard queries differ from that template (if at all)?
Upvotes: 7
Views: 9055
Reputation: 4983
To combine other conditions with nested
object, help this help some one.
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "*",
"fields": [
"field_a",
"field_b",
"field_c"
]
}
},
{
"nested": {
"path": "students",
"query": {
"bool": {
"must": [
{
"wildcard": {
"students.name": {
"value": "*keyword*"
}
}
}
]
}
}
}
}
]
}
}
}
Upvotes: 0
Reputation: 5924
You need to used a Nested Query to query fields within nested
types.
{
"query":{
"nested":{
"path":"owner",
"query":{
"wildcard":{
"owner.name":"John*"
}
}
}
}
}
Also, you shouldn't start your term with a wildcard as it can result in extremely slow queries.
If you have multiple levels of nested
objects the path
value should be the deepest level of nested
objects, and the property in the query should be the full path.
{
"query":{
"nested":{
"path":"owner.pets",
"query":{
"wildcard":{
"owner.pets.name":"{someValue}"
}
}
}
}
}
Upvotes: 10