【问题标题】:Elasticsearch mapping for object with dynamic keys具有动态键的对象的 Elasticsearch 映射
【发布时间】:2016-07-07 07:50:00
【问题描述】:

我有这个映射的简单文档:

'product': {
  'properties': {
    'name': { 'type': 'string' },
    'shops': { 
      'type': 'object', 
      'index_name': 'shop', 
      'properties': {
        'name': { 'type': 'string' },
        'url': { 'type': 'string' },
        'price': { 'type': 'integer' },
      }
    }
  }
}

文档如下所示:

{ 
  'name': 'Kindle', 
  'shops': [ 
    { 'name': 'amazon', 'url': 'http://...', 'price': 79 },
    { 'name': 'ebay', 'url': 'http://...', 'price': 99 }
}

但我想以这种格式存储文档:

{
  'name': 'Kindle',
  'shops': {
    'amazon': { 'url': 'http://...', 'price': 79 },
    'ebay': { 'url': 'http://...', 'price': 99 }
  }
}

有没有办法为此进行映射?或者我应该只创建对象“商店”并使其保持无模式。

【问题讨论】:

  • 你打算做什么样的查询?
  • 查询将很简单,在大多数情况下,对特定商店的选定产品进行字符串查询。例如: { "query_string" : { "query" : "exists:shops.amazon.url" } } 我更喜欢将商店存储为对象而不是数组,因为在 ruby​​ 中更容易操作。
  • @mrazicz 你克服了这个问题吗?
  • 嗨,是的,我使用下面答案中的解决方案。但毕竟,我最终得到了我原来的解决方案。事实证明,该对象数组更适合我的用例。

标签: elasticsearch


【解决方案1】:

您可以使用带有 path_match ("shops.*") [1] 的 dynamic_template。

[1]https://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-root-object-type.html

【讨论】:

  • 这可能有效。但是现在,我如何检查模板是否有效?有什么建议吗?
  • 只需添加文档,然后检索映射。
  • 我明白了。感谢您的帮助。
  • @ejain - 你能分享这个例子的 es 映射吗?
猜你喜欢
  • 2021-03-10
  • 2019-07-26
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
相关资源
最近更新 更多