在 Array 字段上创建 Couchbase 索引

Create Couchbase Index on Array field

你好如何在数组字段上创建索引我的示例文档是

   { 
"name": [     {
      "family": "Smith",
      "given": [
        "Kam"
      ],
      "prefix": [
        "Mrs."
      ],
      "use": "official"
    },
    {
      "family": "Johns",
      "given": [
        "Kam"
      ],
      "use": "maiden"
    }
  ]
}

我想在家庭和给定字段上编写搜索查询(如)...如何创建索引和建议查询..我是 couchbase 的新手

此查询选择姓氏 "Smith" 和名字 "Kam" 的客户:

select * from customer
where any n in name satisfies n.family = 'Smith' and 
          any fn in n.given satisfies fn = 'Kam' end end

请注意使用嵌套的 ANY 子句,因为在数据中使用了嵌套数组。

然后您可以像这样在姓氏上创建索引:

CREATE INDEX customer_name ON customer
   ( DISTINCT ARRAY n.family FOR n IN name END)

索引在没有任何提示的情况下被使用。您可以通过将 EXPLAIN 添加到查询的开头来看到它正在被使用。这将为您提供 JSON 中包含索引扫描运算符的查询计划。

您可以在此处了解有关数组索引的更多信息: https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/indexing-arrays.html