MongoDB create product summary collection - node.js

Say I have a product collection like this:
{
"_id": "5a74784a8145fa1368905373",
"name": "This is my first product",
"description": "This is the description of my first product",
"category": "34/73/80",
"condition": "New",
"images": [
{
"length": 1000,
"width": 1000,
"src": "products/images/firstproduct_image1.jpg"
},
...
],
"attributes": [
{
"name": "Material",
"value": "Synthetic"
},
...
],
"variation": {
"attributes": [
{
"name": "Color",
"values": ["Black", "White"]
},
{
"name": "Size",
"values": ["S", "M", "L"]
}
]
}
}
and a variation collection like this:
{
"_id": "5a748766f5eef50e10bc98a8",
"name": "color:black,size:s",
"productID": "5a74784a8145fa1368905373",
"condition": "New",
"price": 1000,
"sale": null,
"image": [
{
"length": 1000,
"width": 1000,
"src": "products/images/firstvariation_image1.jpg"
}
],
"attributes": [
{
"name": "Color",
"value": "Black"
},
{
"name": "Size",
"value": "S"
}
]
}
I want to keep the documents separate and for the purpose of easy browsing, searching and faceted search implementation, I want to fetch all the data in a single query but I don't want to do join in my application code.
I know it's achievable using a third collection called summary that might look like this:
{
"_id": "5a74875fa1368905373",
"name": "This is my first product",
"category": "34/73/80",
"condition": "New",
"price": 1000,
"sale": null,
"description": "This is the description of my first product",
"images": [
{
"length": 1000,
"width": 1000,
"src": "products/images/firstproduct_image1.jpg"
},
...
],
"attributes": [
{
"name": "Material",
"value": "Synthetic"
},
...
],
"variations": [
{
"condition": "New",
"price": 1000,
"sale": null,
"image": [
{
"length": 1000,
"width": 1000,
"src": "products/images/firstvariation_image.jpg"
}
],
"attributes": [
"color=black",
"size=s"
]
},
...
]
}
problem is, I don't know how to keep the summary collection in sync with the product and variation collection. I know it can be done using mongo-connector but i'm not sure how to implement it.
please help me, I'm still a beginner programmer.

you don't actually need to maintain a summary collection, its redundant to store product and variation summary in another collection
instead of you can use an aggregate pipeline $lookup to outer join product and variation using productID
aggregate pipeline
db.products.aggregate(
[
{
$lookup : {
from : "variation",
localField : "_id",
foreignField : "productID",
as : "variations"
}
}
]
).pretty()

Related

Shopware 6 API Create a Product with Variants

I created my products with the API and all is ok. But we have some products with variants. These looking good in the backend, but in the frontend shopware doesn't display the product. Where is my error in the payload?
Thank you for helping me!
Here is my code:
{
"write-product":
{
"entity": "product",
"action": "upsert",
"payload": [
{
"id": "17cf3bac8966cc4de52aa557f59e5fd3",
"active": true,
"deliveryTimeId": "1e04777cb17d4396b424129b2d8e3a79",
"description": "Produktbeschreibung",
"ean": null,
"featureSetId": null,
"manufacturerId": null,
"manufacturerNumber": null,
"minPurchase": 1,
"name": "Produktname",
"productNumber": "Nummer",
"releaseDate": "2022-09-28T14:12:18.03",
"stock": 999999,
"taxId": "2842fd17d5a7424795853e7fe7072448",
"price":[
{
"currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
"net": 13.77,
"gross": 16.39,
"linked": false
}
],
"categories":[
{
"id":"648875017ed54637a41c8c1f972c239f",
"name":"Kategoriename"
}
],
"configuratorSettings": [
{
"productId": "17cf3bac8966cc4de52aa557f59e5fd3",
"optionId": "2ab9df7d2dd94ad38eb53cf7dcf16e56"
},
{
"productId": "17cf3bac8966cc4de52aa557f59e5fd3",
"optionId": "0fcf784970ea448bac1f7db542df9b86"
},
{
"productId": "17cf3bac8966cc4de52aa557f59e5fd3",
"optionId": "0014aa7dc0b644b78cd4b39193f580b4"
}
],
"properties": [
{
"groupId":"ca74efd59b5545f2b4e4f532c217b2ce",
"name": "1,0 m"
},
{
"groupId":"fa7f63d697d44684a5bb74ba5a73d3c7",
"name": "2,0 mm"
},
{
"groupId":"a431bc98fe0149c3bf3ececf629e9740",
"name": "Material"
}
],
"visibilities": [
{
"id": "17cf3bac8966cc4de52aa557f59e5fd3",
"salesChannelId": "840a0403b56240b6a11502af1d46521e",
"visibility": 30
}
],
"coverId": "0f9ab99638bd0ea5e907234bf425819e",
"cover":
{
"mediaId": "0f9ab99638bd0ea5e907234bf425819e"
},
"media": [
{
"media":
{
"id": "0f9ab99638bd0ea5e907234bf425819e",
"position": 0,
"mediaFolderId": "d8f460870e454c0bbca540614ca30029",
"alt": "Bildname",
"description": "Bildbeschreibung",
"title": "Bildtitel"
}
}
],
"children": [
{
"id": "25c8876fb7577cad78df7539737d25ce",
"active": true,
"deliveryTimeId": "1e04777cb17d4396b424129b2d8e3a79",
"description": "Beschreibung Kindelement",
"ean": null,
"featureSetId": null,
"manufacturerId": null,
"manufacturerNumber": null,
"minPurchase": 1,
"name": "Name Kindelement",
"productNumber": "Nummer Kindelement",
"releaseDate": "2022-09-16T14:14:31.41",
"stock": 999999,
"taxId": "2842fd17d5a7424795853e7fe7072448",
"price":[
{
"currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
"net": 82.63,
"gross": 98.33,
"linked": false
}
],
"categories":[
{
"id":"648875017ed54637a41c8c1f972c239f",
"name":"Kategoriename"
}
],
"configuratorSettings": [
{
"productId": "25c8876fb7577cad78df7539737d25ce",
"optionId": "b224173313cb4f8da03578ef83702751"
},
{
"productId": "25c8876fb7577cad78df7539737d25ce",
"optionId": "0fcf784970ea448bac1f7db542df9b86"
},
{
"productId": "25c8876fb7577cad78df7539737d25ce",
"optionId": "0014aa7dc0b644b78cd4b39193f580b4"
}
],
"properties": [
{
"groupId":"ca74efd59b5545f2b4e4f532c217b2ce",
"name": "6,0 m"
},
{
"groupId":"fa7f63d697d44684a5bb74ba5a73d3c7",
"name": "2,0 mm"
},
{
"groupId":"a431bc98fe0149c3bf3ececf629e9740",
"name": "Material"
}
],
"parentId": "17cf3bac8966cc4de52aa557f59e5fd3",
"options": [
{
"id":"b224173313cb4f8da03578ef83702751",
"group": {
"id":"ca74efd59b5545f2b4e4f532c217b2ce"}
},
{
"id":"0fcf784970ea448bac1f7db542df9b86",
"group": {
"id":"fa7f63d697d44684a5bb74ba5a73d3c7"}
},
{
"id":"0014aa7dc0b644b78cd4b39193f580b4",
"group": {
"id":"a431bc98fe0149c3bf3ececf629e9740"
}
}
],
"visibilities": [
{
"id": "25c8876fb7577cad78df7539737d25ce",
"salesChannelId": "840a0403b56240b6a11502af1d46521e",
"visibility": 30
}
],
"media": [
{
"media": {
"id": "fba6e74294488b554aa88f7dcf6f38f2",
"position": 0,
"mediaFolderId": "d8f460870e454c0bbca540614ca30029",
"alt": "Bildname Kindelement",
"description": "Beschreibung Kindelement",
"title": "Bildtitel Kindelement"
}
}
]
}
]
}
]
}
}
I'm searching in different boards and looking in the API Browser, Using swagger for the api and so on. No result. I need the main-product and the variants be visible in the frontend. At the moment all products with variants are invisible in the frontend.
Double check your Visibility & Categories. It could be them. Also, maybe you need to re-index after creation?
You have way too much stuff going on at the moment for us to help you out.
Try to split your calls into smaller chunks (for us). Also start with less data when creating a product. With every step check the frontend. Can you even create a simple product & display it on the frontend? Yes? Then try create the most basic child that inherits most of the visible parent.
For a child you do not need much data, e.g.
{
"write-product": {
"entity": "product",
"action": "upsert",
"payload": [
{
"parentId": "",
"options": [
{
"id": ""
}
],
"price": [
{
"currencyId": "{{gen_salesChannel_currency_id}}",
"gross": 0.0,
"net": 0.0,
"linked": false
}
]
"productNumber": "",
"stock": 999
}
]
}
}
Another tip is to use the network tab in the admin to see how it creates these relationships. Here is an image of how it looks like:

Filter by Product Properties with Store API

For the store API endpoint /store-api/product is it possible to filter on the properties of a product? Not the defaults such as whether it's active or stock levels, but the properties we've defined on the product, for example colour or farbe? For the search endpoint it supports passing in a list of properties ID's which this one does not.
None of the below queries work, and return the various errors below or Call to a member function buildAccessor() on null.
{
"limit": 40,
"filter": [
{
"type": "contains",
"field": "Farbe",
"value": "red"
}
]
}
"Field \"Farbe\" in entity \"product\" was not found."
{
"limit": 40,
"filter": [
{
"type": "contains",
"field": "properties.Farbe",
"value": "red"
}
]
}
"Field \"Farbe\" in entity \"property_group_option\" was not found."
You can combine filters for the name of the property value and their respective group in a multi filter. The following example will only give you products that have the "shoe-color" property with the value "coral".
{
"limit": 1,
"includes": {
"product": ["id", "productNumber", "properties"],
"property_group_option": ["name", "group"],
"property_group": ["name"]
},
"associations": {
"properties": {
"associations": {
"group": []
}
}
},
"filter": [
{
"type": "multi",
"operator": "and",
"queries": [
{
"type": "equals",
"field": "properties.group.name",
"value": "shoe-color"
},
{
"type": "equals",
"field": "properties.name",
"value": "coral"
}
]
}
]
}
Example response:
{
"entity": "product",
"total": 1,
"aggregations": [],
"page": 1,
"limit": 1,
"elements": [
{
"productNumber": "6bbfe1f608504c9b9a7bf92d6a071734",
"properties": [
{
"name": "coral",
"group": {
"name": "shoe-color",
"apiAlias": "property_group"
},
"apiAlias": "property_group_option"
},
{
"name": "cotton",
"group": {
"name": "textile",
"apiAlias": "property_group"
},
"apiAlias": "property_group_option"
}
],
"id": "062ba988aa1840fa84371c9c43b2f838",
"apiAlias": "product"
}
],
"states": [],
"apiAlias": "dal_entity_search_result"
}

How to flatten JSON with nested structure before passing to #csv filter

I am trying to parse some JSON that is the output of an AWS CLI command to display Snapshots. I want to load this data up into a spreadsheet to be able to filter, group, and audit it.
I've been stumped on how to get the nested Tags array flattened into the parent objects such that the intermediate can then be passed to the #csv filter.
Here is the example:
Initial input JSON:
{
"Snapshots": [
{
"SnapshotId": "snap-fff",
"StartTime": "2014-04-01T06:00:13.000Z",
"VolumeId": "vol-fff",
"VolumeSize": 50,
"Description": "desc1",
"Tags": [
{
"Value": "/dev/sdf",
"Key": "device"
},
{
"Value": "a name",
"Key": "Name"
},
{
"Value": "Internal",
"Key": "Customer"
},
{
"Value": "Demo",
"Key": "Environment"
},
{
"Value": "Brand 1",
"Key": "Branding"
},
{
"Value": "i-fff",
"Key": "instance_id"
}
]
},
{
"SnapshotId": "snap-ccc",
"StartTime": "2014-07-01T05:59:14.000Z",
"VolumeId": "vol-ccc",
"VolumeSize": 8,
"Description": "B Desc",
"Tags": [
{
"Value": "/dev/sda1",
"Key": "device"
},
{
"Value": "External",
"Key": "Customer"
},
{
"Value": "Production",
"Key": "Environment"
},
{
"Value": "i-ccc",
"Key": "instance_id"
},
{
"Value": "B Brand",
"Key": "Branding"
},
{
"Value": "B Name",
"Key": "Name"
},
{
"Value": "AnotherValue",
"Key": "AnotherKey"
}
]
}
]
}
Desired Intermediate:
[
{
"SnapshotId": "snap-fff",
"StartTime": "2014-04-01T06:00:13.000Z",
"VolumeId": "vol-fff",
"VolumeSize": 50,
"Description": "desc1",
"device": "/dev/sdf",
"Name": "a name",
"Customer": "Internal",
"Environment": "Demo",
"Branding": "Brand 1",
"instance_id": "i-fff",
}
{
"SnapshotId": "snap-ccc",
"StartTime": "2014-07-01T05:59:14.000Z",
"VolumeId": "vol-ccc",
"VolumeSize": 8,
"Description": "B Desc",
"device": "/dev/sda1",
"Customer": "External",
"Environment": "Production",
"instance_id": "i-ccc",
"Branding": "B Brand",
"Name": "B Name",
"AnotherKey": "AnotherValue",
}
]
Final Output:
"SnapshotId","StartTime","VolumeId","VolumeSize","Description","device","Name","Customer","Environment","Branding","instance_id","AnotherKey"
"snap-fff","2014-04-01T06:00:13.000Z","vol-fff",50,"desc1","/dev/sdf","a name","Internal","Demo","Brand 1","i-fff",""
"snap-ccc","2014-07-01T05:59:14.000Z","vol-ccc",8,"B Desc","/dev/sda1","External","Production","i-ccc","B Brand","B Name","AnotherValue"
The following jq filter produces the requested intermediate output:
.Snapshots[] | (. + (.Tags|from_entries)) | del(.Tags)
Explanation: from_entries converts the array of key-value objects to an object with the given key-value pairs. This is added to the target object, and finally the "Tags" key is removed.
If the "target" object has a key that also appears in the "Tags" array, then the above filter will favor the value in the "Tags" array. You may accordingly wish to change the order of the operands of "+", or resolve the conflict in some other way.

Modifying elasticsearch score based on nested field value

I want to modify scoring in ElasticSearch (v2+) based on the weight of a field in a nested object within an array.
For instance, using this data:
PUT index/test/0
{
"name": "red bell pepper",
"words": [
{"text": "pepper", "weight": 20},
{"text": "bell","weight": 10},
{"text": "red","weight": 5}
]
}
PUT index/test/1
{
"name": "hot red pepper",
"words": [
{"text": "pepper", "weight": 15},
{"text": "hot","weight": 11},
{"text": "red","weight": 5}
]
}
I want a query like {"words.text": "red pepper"} which would rank "red bell pepper" above "hot red pepper".
The way I am thinking about this problem is "first match the 'text' field, then modify scoring based on the 'weight' field". Unfortunately I don't know how to achieve this, if it's even possible, or if I have the right approach for something like this.
If proposing alternative approach, please try and keep a generalized idea where there are tons of different similar cases (eg: simply modifying the "red bell pepper" document score to be higher isn't really a suitable alternative).
The approach you have in mind is feasible. It can be achieved via function score in a nested query .
An example implementation is shown below :
PUT test
PUT test/test/_mapping
{
"properties": {
"name": {
"type": "string"
},
"words": {
"type": "nested",
"properties": {
"text": {
"type": "string"
},
"weight": {
"type": "long"
}
}
}
}
}
PUT test/test/0
{
"name": "red bell pepper",
"words": [
{"text": "pepper", "weight": 20},
{"text": "bell","weight": 10},
{"text": "red","weight": 5}
]
}
PUT test/test/1
{
"name": "hot red pepper",
"words": [
{"text": "pepper", "weight": 15},
{"text": "hot","weight": 11},
{"text": "red","weight": 5}
]
}
post test/_search
{
"query": {
"bool": {
"disable_coord": true,
"must": [
{
"match": {
"name": "red pepper"
}
}
],
"should": [
{
"nested": {
"path": "words",
"query": {
"function_score": {
"functions": [
{
"field_value_factor": {
"field" : "words.weight",
"missing": 0
}
}
],
"query": {
"match": {
"words.text": "red pepper"
}
},
"score_mode": "sum",
"boost_mode": "replace"
}
},
"score_mode": "total"
}
}
]
}
}
}
Result :
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "0",
"_score": 26.030865,
"_source": {
"name": "red bell pepper",
"words": [
{
"text": "pepper",
"weight": 20
},
{
"text": "bell",
"weight": 10
},
{
"text": "red",
"weight": 5
}
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 21.030865,
"_source": {
"name": "hot red pepper",
"words": [
{
"text": "pepper",
"weight": 15
},
{
"text": "hot",
"weight": 11
},
{
"text": "red",
"weight": 5
}
]
}
}
]
}
The query in a nutshell would score a document that satisfies the must clause as follows : sum up the weights of the matched nested documents with the score of the must clause.

Cloudant Search Index, query get false result

So I have a json document structured like this:
{
"_id": "xxx/room/BACK/reservation",
"_rev": "9-096ac8d55a8fc0400111afdbcb71ff13",
"reservationList": [
{
"contactPerson": {
"_id": "xxx/hutomo",
"fullName": "Hutomo",
"email": "hutomo#xxx.com"
},
"start": "2015-12-29T12:00:00.000Z",
"end": "2015-12-29T13:00:00.000Z",
"title": "yuu",
"workstationName": "16JT02",
"id": 0,
"reserveDate": "2015-12-18T12:00:00.000Z"
},
{
"contactPerson": {
"_id": "xxx/hutomo",
"fullName": "Hutomo",
"email": "hutomo#xxx.com"
},
"start": "2015-12-29T12:00:00.000Z",
"end": "2015-12-29T13:00:00.000Z",
"title": "ajdjdn",
"workstationName": "16JT10",
"id": 1,
"reserveDate": "2015-12-18T12:00:00.000Z"
},
{
"contactPerson": {
"_id": "xxx/hutomo",
"fullName": "Hutomo",
"email": "hutomo#xxx.com"
},
"start": "2015-12-28T18:00:00.000Z",
"end": "2015-12-28T20:00:00.000Z",
"title": "gg",
"workstationName": "16JT02",
"id": 2,
"reserveDate": "2015-12-18T12:00:00.000Z"
},
{
"contactPerson": {
"_id": "xxx/hutomo",
"fullName": "Hutomo",
"email": "hutomo#xxx.com"
},
"start": "2015-12-28T22:00:00.000Z",
"end": "2015-12-28T23:00:00.000Z",
"title": "gg",
"workstationName": "16JT04",
"id": 3,
"reserveDate": "2015-12-18T12:00:00.000Z"
}
]
}
Then I made a search index using this function:
function (doc) {
index("id", doc._id, {"index":true});
for (var i = 0; i<doc.reservationList.length;i++)
{
index("workstationName", doc.reservationList[i].workstationName, {"store":true,"index":true});
index("contactPerson._id", doc.reservationList[i].contactPerson._id, {"store": true, "index":false});
index("contactPerson.fullName", doc.reservationList[i].contactPerson.fullName, {"store": true, "index":false});
index("contactPerson.email", doc.reservationList[i].contactPerson.email, {"store": true, "index":false});
if(doc.reservationList[i].contactPerson.mobilePhone)
index("contactPerson.mobilePhone", doc.reservationList[i].contactPerson.mobilePhone, {"store": true, "index":false});
index("reservationID", doc.reservationList[i].id,{"store": true,"index":false});
index("start",doc.reservationList[i].start,{"store": true,"index":false});
index("end",doc.reservationList[i].end,{"store": true,"index":false});
index("title",doc.reservationList[i].title,{"store": true,"index":false});
}
}
Running lucene query with this
id:xxx* AND workstationName:16JT02
then it returns
{
"id": "xxx/room/BACK/reservation",
"order": [
1.0196553468704224,
0
],
"fields": {
"contactPerson._id": [
"xxx/hutomo",
"xxx/hutomo",
"xxx/hutomo",
"xxx/hutomo"
],
"workstationName": [
"16JT04",
"16JT02",
"16JT10",
"16JT02"
],
"reservationID": [
3,
2,
1,
0
],
"end": [
"2015-12-28T23:00:00.000Z",
"2015-12-28T20:00:00.000Z",
"2015-12-29T13:00:00.000Z",
"2015-12-29T13:00:00.000Z"
],
"title": [
"gg",
"gg",
"ajdjdn",
"yuu"
],
"contactPerson.email": [
"hutomo#xxx.com",
"hutomo#xxx.com",
"hutomo#xxx.com",
"hutomo#xxx.com"
],
"start": [
"2015-12-28T22:00:00.000Z",
"2015-12-28T18:00:00.000Z",
"2015-12-29T12:00:00.000Z",
"2015-12-29T12:00:00.000Z"
],
"contactPerson.fullName": [
"Hutomo",
"Hutomo",
"Hutomo",
"Hutomo"
]
}
}
As we can see, the result contains workstationName 16JT04 and 16JT10
what did i do wrong? and how to fix this? Thanks a lot =D
i believe these may answer your question:
https://stackoverflow.com/a/16336255
https://stackoverflow.com/a/16466134
basically, you could (a) separate reservationList content into their own document or (b) create a view for workstationName (or whatever fields desired):
function(doc) {
if(doc.reservationList) {
doc.reservationList.forEach(function(reservationList) {
emit(reservationList.workstationName,reservationList);
});
}
}
using the view, you can get results where workstationName (i.e., key) is "16JT02" with:
https://[username].cloudant.com/[db_name]/_design/[designdoc_name]/_view/[view_name]?key="16JT02"
The result looks right . Query based on Search Index is returning the matching document details. If you add include_docs:true to your query , you can see this clearly.
It seems your requirement is not matching with what Search Index_query can offer. As others pointed out already, try a view.

Resources