我使用以下查询为订单存储区创建了一个复合索引:
CREATE INDEX idx_customer ON `order` (
buyer.contact.firstName,
buyer.contact.lastName,
ALL DISTINCT ARRAY aoc.`value` FOR aoc IN buyer.contact.communicationChannel
WHEN aoc.`communicationChannelCode`= "EMAIL" END)
WHERE _class = "com.lbk.entities.OrderEntity"
当我搜索buyer.contact.firstName时,查询与索引匹配,但buyer.contact.lastName失败。 基本上,它与查询中的第一个字段匹配。
以下是失败字段(buyer.contact.lastName)的解释结果:
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "PrimaryScan3",
"index": "#primary",
"index_projection": {
"primary_key": true
},
"keyspace": "order",
"namespace": "default",
"using": "gsi"
},
{
"#operator": "Fetch",
"keyspace": "order",
"namespace": "default"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "(((((`order`.`buyer`).`contact`).`lastName`) = \"jj\") and ((`order`.`_class`) = \"com.lbk.entities.OrderEntity\"))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "(((`order`.`buyer`).`contact`).`lastName`)"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
"text": "SELECT buyer.contact.lastName FROM `order` where buyer.contact.lastName = 'jj'\nAND (`_class` = \"com.lbk.entities.OrderEntity\")"
}
企业版5.5.1内部版本3511
答案 0 :(得分:2)
要使用索引,查询谓词必须具有前导索引键作为谓词。 Couchbase存储桶可以具有任何类型的文档,并且没有架构。当文档中的前导索引键为MISSING时,Indexer不会索引。如果前导索引键上没有谓词,则查询还必须包含MISSING值,因此索引将不符合该查询条件,因此将不被使用。
在您的情况下,添加以下内容之一以查询谓词是否可以。
1) AND buyer.contact.firstName IS NOT MISSING
2) AND buyer.contact.firstName IS NOT NULL
3) AND buyer.contact.firstName IS VALUED
在https://blog.couchbase.com/n1ql-practical-guide-second-edition/中结帐“在Couchbase N1QL中设计查询查询索引”