I have a main document, the structure looks like this:
[
{
"DEV_generic": {
"attributes": {
"fieldCanonical": "abc-xxx-yyyy",
"fieldCanonicalHistory": [
"abc-xxx-yyyy"
],
"someIndicator": "Yes",
"someField1": "xxx",
"lastReceivedDate": "2014-11-11",
"lastRevisionUser": "suser"
},
"createDateUtc": 1485905922,
"crossReferences": [
{
"productId": "productId_1234",
"type": "productType1"
},
{
"productId": "productId_234",
"type": "productType2"
},
{
"productId": "productId_543",
"type": "productType4"
}
],
"id": "xx_1234",
"parentId": "xx_1200",
"productType": "mmy",
"type": "masterProduct",
"updateDateUtc": 1486075105
}
}
]
There is a separate document that is a product group:
[
{
"DEV_generic": {
"attributes": {
"fieldCanonical": "xxx_series",
"fieldCanonicalHistory": [
"xxx_series"
],
"canonical": "bmw",
"fullName": "BMW",
"partId": 20005,
"productGroupFullName": "x-Series",
"lastReceivedDate": "2014-11-11",
"lastRevisionUser": "bmctague"
},
"createDateUtc": 1485906027,
"crossReferences": [
{
"productId": "productId_958",
"type": "productType5"
},
{
"productId": "productId_3845",
"type": "productType6"
}
],
"id": "prefix2_productId_543",
"name": "x-Series",
"parentId": "prefix1_20000005",
"productType": "productType4",
"type": "masterProduct",
"updateDateUtc": 1486075148
}
}
]
The crossReferences array in the first document (let’s call it mainProduct) may or may not have a cross reference to the product type of the second document - let’s call it productGroup.
We need to make a query that basically returns:
mainProduct.someIndicator, mainProduct.someField1, productGroup.fieldCanonical
The KEY to the productGroup document is retrieved from mainProduct.crossReferences and built with a concat like ‘masterProduct::’ || crossReferences.productId, but only where crossReferences.type = productType4.
However not every mainProduct document has an associated productGroup inside its crossReferences array.
In regular SQL we would use a LEFT OUTER JOIN but when we attempt the same with N1ql we keep running into problems with the either ambiguous field names or missing index.
The closest we have come to success is this query:
SELECT
mainProduct.attributes.someIndicator,
mainProduct.attributes.someField1,
mainProduct.id,
cr.productId as mmGroupProductId,
(SELECT mmyGroup.attributes.fullName, mmyGroup.attributes.partId FROM DEV_generic mmyGroup
USE KEYS 'masterProduct::'||cr.productId)
FROM DEV_generic mainProduct
UNNEST mainProduct.crossReferences as cr
WHERE mainProduct.type= 'masterProduct'
AND mainProduct.productType = 'mmy'
AND cr.type = 'productType4'
However there are a few problems with this:
- this is retrieving results only for mainProduct documents that have a cross reference to productType4 - we want to retrieve all mainProduct documents whether they have a productType4 cross reference, just have those cross-referenced productGroup fields be null if they don’t exist
- the productGroup field values come back in a nested array
I’ve tried a bunch of variation from looking over other somewhat similar questions in these forums, but it looks like we don’t have the correct indexes set up for these documents either.
Any help would be really appreciated it - we’ve been trying to sort this out for a few days now.
Thanks,
cchong