Hi Nick
I modified that example to fit the problem that I'm trying to solve.
Unfortunately, I couldn't figure out how to search for a value across
all of the nested fields without referencing each nested field
individually. Is this possible?
As an example, I'm going to use this doc:
{ text: "foo",
attrib: [
{ color: "red", active: true },
{ color: "blue", active: false }
]
}
If 'attrib' is field-type 'object', then internally, this doc would look
something like this:
{ text: ["foo"],
attrib.color: ["red","blue"],
attrib.active: [true, false]
}
If 'attrib' is field-type 'nested', then internally, this doc would be
stored as 3 separate docs, something like this:
{ text: ["foo"] }
{ color: ["red"], active: true},
{ color: ["blue"], active: false},
...plus something to tie docs 2 & 3 to the main doc.
Now, consider this query:
color == 'red' and active == false
In the first (object type) example, the values for the 'attrib' are
flattened out, so the above clause will be true.
In the second (nested type) example, each doc is considered separately,
so the above clause will be false.
So you should use the 'nested' field-type only when you need to run
queries/filters that depend on the each sub-object being considered
separately.
Further configuration:
By default, the nested properties are not visible in the parent objects.
By which I mean: a query for 'attrib.color' will only work as a nested
query/filter.
However, if you set 'include_in_parent' (ie direct parent object) or
'include_in_root' (topmost object) then the nested values will be copied
into the parent or root object, in the same way as demonstrated in the
first example.
Only the root object has an _all field. By default, values in the
nested objects ARE included in the _all field.
So: two choices
- query the topmost _all field
- use a nested query with a bool or dismax query to query each
field in your nested doc.
For instance, using your example docs:
QUERY THE _ALL FIELD:
curl -XGET 'http://127.0.0.1:9200/test/tweet/_search?pretty=1' -d '
{
"query" : {
"text" : {
"_all" : "jack stuff"
}
}
}
'
NESTED QUERY OF ALL FIELDS:
curl -XGET 'http://127.0.0.1:9200/test/tweet/_search?pretty=1' -d '
{
"query" : {
"nested" : {
"query" : {
"bool" : {
"should" : [
{
"text" : {
"comments.username" : "jack stuff"
}
},
{
"text" : {
"comments.content" : "jack stuff"
}
}
]
}
},
"path" : "comments"
}
}
}
'
ALSO NOTE: In your example, you are querying 'this', which is a
stopword, and would thus never return any results.
clint