在 CouchBase(4.1.1) 中,N1QL 可以使用整个元素创建索引,以这两个示例为例。
假设我们有这个文档结构:
{
"name": "blah",
"credentials": [
{
"level": 5,
"sector": "XP"
}
],
}
现在说我们想根据名称和整个凭据元素创建 index1 可能吗?
就像是
create index indexName on `bucketName` (name, credentials) USING GSI;
或 index2 基于名称和嵌套字段之一,如级别;怎么可能做到这一点?就像是
create index indexName on `bucketName`(name, credential.levels) USING GSI;
运行解释时,我的二级索引没有被使用,couchbase 默认为这个存储桶的主索引。
这是我正在使用的选择。
select s.name, s.credentials
from `security` unnest s.credentials
where credentials is not missing and name = 'tom';
这是制作的解释:
{
"requestID": "f8d46eeb-3898-4ace-a24f-1582e0504eb7",
"signature": "json",
"results": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "PrimaryScan",
"index": "#primary",
"keyspace": "read",
"namespace": "default",
"using": "gsi"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"as": "s",
"keyspace": "bucketName",
"namespace": "default"
},
{
"#operator": "Unnest",
"as": "beacons",
"expr": "(`s`.`credentials`)"
},
{
"#operator": "Filter",
"condition": "((`s`.`name`) = \"tom\")"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "(`s`.`id`)"
},
{
"expr": "`credentials`"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "2.82063ms",
"executionTime": "2.765439ms",
"resultCount": 1,
"resultSize": 1917
}
}