ElasticSearch 中的“关联"数据 [英] 'Associated' data in ElasticSearch

查看:40
本文介绍了ElasticSearch 中的“关联"数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于电子商务平台,我们希望将产品编入索引.默认字段很简单:name_enname_dename_frdescription.但是,价格和库存取决于另一个值:

For an ecommerce platform, we're looking to index products. Default fields are simple as: name_en, name_de, name_fr, description. But, price and stock are dependant on another value:

  • Product A,对于 webshop 1,具有 price = 1.99stock = 10,并且适合类别 1、10 和 50.
  • Product A,对于 webshop 2,具有 price = 5.99stock = 5 和类别 9、90 和 500.
  • Product A, for webshop 1, has price = 1.99, stock = 10, and fits under categories 1, 10, and 50.
  • Product A, for webshop 2, has price = 5.99, stock = 5, and categories 9, 90, and 500.

我在考虑嵌套对象,但这是一种选择吗?

I was thinking of nested objects, but is that even an option?

- name_en: Product A
- description_en: Product A 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]
    }
]

像这样查询容易吗?我们可以轻松获取整个文档,其中的值是 webshop.key.webshop_id.value = 1webshop.key.categories.value = 500 吗?

Is it easy querying like this? Can we easily get the entire document, with the values where webshop.key.webshop_id.value = 1, or webshop.key.categories.value = 500?

我的想法是不是错了,有什么正确的方向吗?

Is my thinking wrong, any pointers in the right direction?

推荐答案

您可以像以前一样嵌套,但是很难在单个网上商店中更新产品的价格或库存,因为您必须重新编制索引整个 webshops 数组.有很多方法可以解决它,但这很复杂.

You can nest as you did, but it will get difficult to update the price or stock of a product in a single webshop, because you'll have to reindex the whole webshops array. There are ways to around it, but that's convoluted.

除了具有嵌套结构之外,您还可以对网店部分进行非规范化,只需将 pricestockcategories 字段包含在像这样的文件.

Instead of having a nested structure, you can also denormalize the webshop part and simply include the price, stock and categories fields in the documents like this.

Document 1:
- name_en: Product A
- description_en: Product A description
- webshop_id: 1
- price: 1.99
- stock: 10
- categories: [1, 10, 50]

Document 2:
- name_en: Product A
- description_en: Product A description
- webshop_id: 2
- price: 5.99
- stock: 5
- categories: [9, 90, 500]

然后在您的查询中,您可以简单地为 webshop = 1webshop = 2(或两者)添加约束,具体取决于您查询的网店.更新特定商店中产品的价格、库存和类别也更容易,您只需更新相应的文档即可.

Then in your queries you can simply add a constraint for webshop = 1 or webshop = 2 (or both) depending on which webshop you're querying against. It's also much easier to update the price, stock and categories of a product in a specific shop, all you have to do is update the corresponding document.

这意味着您的产品数据(名称、描述等)将在每个网上商店复制一次,但这通常没什么大不了的(在 NoSQL 世界中很常见),您只需要更新 2 个文档而不是单个文档,但 _bulk 会有所帮助.至少,当您添加新的网店时,您不需要重新索引所有数据 (!!!) 并且您可以在不干扰其他网店的情况下更改价格和库存.

This means that your product data (name, description, etc) will be copied once per webshop but it's not a big deal usually (pretty common in the NoSQL world), you just have to update 2 documents instead of a single, but _bulk will help there. At least, when you add new webshops, you don't need to reindex all your data (!!!) and you change the prices, stocks in one webshop without interfering with the others.

这篇关于ElasticSearch 中的“关联"数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆