如何索引嵌套数组

时间:2018-01-11 13:51:51

标签: json indexing couchbase n1ql

如何在文档上方索引(Couchbase中的N1QL查询)以加快嵌套数组中的SerialNumber字段搜索(doc => groups => items => item.SerialNumber)?

样品:

{
  "Id": "0012ed6e-41af-4e45-b53f-bac3b2eb0b82",
  "Machine": "Machine2",
  "Groups": [        
    {
      "Id": "0fed9b14-fa38-e511-893a-001125665867",
      "Name": "Name",
      "Items": [
        {
          "Id": "64e69b14-fa38-e511-893a-001125665867",
          "SerialNumber": "1504H365",
          "Position": 73
        },
        {
          "Id": "7be69b14-fa38-e511-893a-001125665867",
          "SerialNumber": "1504H364",
          "Position": 72
        }
      ]
    },
    {
      "Id": "0fed9b14-fa38-e511-893a-001125665867",
      "Name": "Name",
      "Items": [
        {
          "Id": "64e69b14-fa38-e511-893a-001125665867",
          "SerialNumber": "1504H365",
          "Position": 73
        },
        {
          "Id": "7be69b14-fa38-e511-893a-001125665867",
          "SerialNumber": "1504H364",
          "Position": 72
        }
      ]
    }
  ]
}

我的疑问:

CREATE INDEX idx_serial ON `aplikomp-bucket` 
(ALL ARRAY(ALL ARRAY i.SerialNumber FOR i IN g.Items END ) FOR g In Groups END);

1 个答案:

答案 0 :(得分:3)

CREATE INDEX idx_serial ON `aplikomp-bucket` (DISTINCT ARRAY(DISTINCT ARRAY i.SerialNumber FOR i IN g.Items END ) FOR g In Groups END);

SELECT META().id FROM `aplikomp-bucket` AS a
WHERE ANY g IN a.Groups SATISFIES (ANY i IN g.Items SATISFIES i.SerialNumber > 123 END) END;