nested - 'Associated' data in ElasticSearch -


for ecommerce platform, we're looking index products. default fields simple as: name_en, name_de, name_fr, description. but, price , stock dependant on value:

  • product a, webshop 1, has price = 1.99, stock = 10, , fits under categories 1, 10, , 50.
  • product a, webshop 2, has price = 5.99, stock = 5, , categories 9, 90, , 500.

i thinking of nested objects, option?

- name_en: product - description_en: product description - webshops: [{     - key: webshop_id       value: 1     - key: price       value: 1.99     - key: stock       value: 10     - key: categories       value: [1, 10, 50]     },{     - key: webshop_id       value: 2     - key: price       value: 5.99     - key: stock       value: 5     - key: categories       value: [9, 90, 500]     } ] 

is easy querying this? can entire document, values webshop.key.webshop_id.value = 1, or webshop.key.categories.value = 500?

is thinking wrong, pointers in right direction?

you can nest did, difficult update price or stock of product in single webshop, because you'll have reindex whole webshops array. there ways around it, that's convoluted.

instead of having nested structure, can denormalize webshop part , include price, stock , categories fields in documents this.

document 1: - name_en: product - description_en: product description - webshop_id: 1 - price: 1.99 - stock: 10 - categories: [1, 10, 50]  document 2: - name_en: product - description_en: product description - webshop_id: 2 - price: 5.99 - stock: 5 - categories: [9, 90, 500] 

then in queries can add constraint webshop = 1 or webshop = 2 (or both) depending on webshop you're querying against. it's easier update price, stock , categories of product in specific shop, have update corresponding document.

this means product data (name, description, etc) copied once per webshop it's not big deal (pretty common in nosql world), have update 2 documents instead of single, _bulk there. @ least, when add new webshops, don't need reindex data (!!!) , change prices, stocks in 1 webshop without interfering others.


Comments