In your case, you only for two documents which you’re unnesting.
Even in case of an index (array index), because you don’t have any other predicate, I’m not sure how much improvement you’d get. You can try to create the index with meta().id and see.
How big are the documents?
Below is an example of nested array index you can try.
You’d need to use 4.5 to exploit array indexes to create the appropriate array index.
See the example below. For multi-level indexes, you’ll have to create multi-level array index.
All these are available in 4.5.
CREATE INDEX inested ON `travel-sample`
(DISTINCT ARRAY
(DISTINCT ARRAY y.flight
FOR y IN x.special_flights END)
FOR x IN schedule END)
WHERE type = "route" ;
===============
select * from b;
[
{
“b”: {
“session”: “d5f08f4c-37d7-4fba-ab78-045844aa98de”,
“things”: [
{
“thingId”: “1”,
“time”: 1461343192396,
“value”: “something”
},
{
“thingId”: “2”,
“time”: 1461343192396,
“value”: “23”
}
]
}
},
{
“b”: {
“session”: “d5f08f4c-37d7-4fba-ab78-045844aa98de”,
“things”: [
{
“thingId”: “3”,
“time”: 1461343774121,
“value”: “foo”
},
{
“thingId”: “2”,
“time”: 1461343775672,
“value”: “bar”
}
]
}
}
]
CREATE INDEX idxarray
ON b
((all (array (v
.thingId
) for v
in things
end)))
explain select v from b unnest things v where v.thingId = 1;
[
{
“plan”: {
"#operator": “Sequence”,
"~children": [
{
"#operator": “DistinctScan”,
“scan”: {
"#operator": “IndexScan”,
“index”: “idxarray”,
“index_id”: “fae20d0eec22dc50”,
“keyspace”: “b”,
“namespace”: “default”,
“spans”: [
{
“Range”: {
“High”: [
“1”
],
“Inclusion”: 3,
“Low”: [
“1”
]
}
}
],
“using”: “gsi”
}
},
{
"#operator": “Fetch”,
“keyspace”: “b”,
“namespace”: “default”
},
{
"#operator": “Parallel”,
"~child": {
"#operator": “Sequence”,
"~children": [
{
"#operator": “Unnest”,
“as”: “v”,
“expr”: “(b
.things
)”
}
]
}
},
{
"#operator": “Parallel”,
"~child": {
"#operator": “Sequence”,
"~children": [
{
"#operator": “Filter”,
“condition”: “((v
.thingId
) = 1)”
},
{
"#operator": “InitialProject”,
“result_terms”: [
{
“expr”: “v
”
}
]
},
{
"#operator": “FinalProject”
}
]
}
}
]
},
“text”: “select v from b unnest things v where v.thingId = 1;”
}
]