Couchbase N1QL array query

前提是你 提交于 2020-01-13 06:58:08

问题


Document sample from my giata_properties bucket: link

Relevant json paste

{
  "propertyCodes": {
    "provider": [
      {
        "code": [
          {
            "value": [
              {
                "value": "304387"
              }
            ]
          }
        ],
        "providerCode": "hotelbeds",
        "providerType": "gds"
      },
      {
        "code": [
          {
            "value": [
              {
                "name": "Country Code",
                "value": "EG"
              },
              {
                "name": "City Code",
                "value": "HRG"
              },
              {
                "name": "Hotel Code",
                "value": "91U"
              }
            ]
          }
        ],
        "providerCode": "gta",
        "providerType": "gds"
      }
    ]
  },
  "name": "Arabia Azur Resort"
}

I want a query (and an index) to retrieve a document based on propertyCodes.provider.code.value.value and propertyCodes.provider.providerCode. I've managed to do each separately but I'm not sure how to merge both of them in a single query.

SELECT meta().id FROM giata_properties AS gp USE INDEX(`#primary`) WHERE ANY v WITHIN gp.propertyCodes.provider[*].code SATISFIES v.`value` = '150613' END;

SELECT meta().id FROM giata_properties AS gp USE INDEX(`#primary`) WHERE ANY v within gp.propertyCodes.provider[*].providerCode SATISFIES v = 'hotelbeds' END;

So for example I want to fetch the document that includes propertyCodes.provider.code.value.value of 304387 and that provider is also hotelbeds, because code value can be duplicated over documents, but code and providerCode combination is unique.


回答1:


Here are the query and the indexes.

The query.

SELECT META().id
FROM giata_properties AS gp
WHERE ANY p IN propertyCodes.provider SATISFIES ( ANY v WITHIN p.code SATISFIES v.`value` = '304387' END ) AND p.providerCode = 'hotelbeds' END;

The indexes.

CREATE INDEX idx_value ON giata_properties
( DISTINCT ARRAY ( DISTINCT ARRAY v.`value` FOR v WITHIN p.code END ) FOR p IN propertyCodes.provider END );

CREATE INDEX idx_providerCode ON giata_properties
( DISTINCT ARRAY p.providerCode FOR p IN propertyCodes.provider END );


来源:https://stackoverflow.com/questions/40071965/couchbase-n1ql-array-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!