【问题标题】:Mapping a Map with Dynamic Keys in ElasticSearch在 ElasticSearch 中使用动态键映射地图
【发布时间】:2016-01-16 16:16:33
【问题描述】:

我正在尝试将此类数据存储在 Elasticsearch 索引中

{
  "id": "5644596f9bf67301645999d9",
  "headline": "Scientists Look Beyond Solar System to Study Planet",
  "renditions": {
    "baseImage": {
      "height": 933,
      "href": "www.imgur.com/animage",
      "mimetype": "image/jpeg",
      "width": 1400
    },
    "preview": {
      "height": 500,
      "href": "www.imgur.com/animage",
      "mimetype": "image/jpeg",
      "width": 400
    },
    "thumbnail": {
      "height": 150,
      "href": "www.imgur.com/animage",
      "mimetype": "image/jpeg",
      "width": 125
    }
  }
}

但是,映射不断为“演绎版”的所有不同子代添加唯一块。

理想情况下,我会为“演绎”定义一种子类型,然后断言其下的所有对象都应如此。

这可能吗?

我研究过动态映射,但对这一切有点困惑......

【问题讨论】:

  • 你能改变你的数据结构吗?还是你必须使用那个表格?
  • 理想情况下它会保持这种状态,因为它也存储在 Mongo 中,因此共享相同的模型结构将是最简单的。如果我确实改变了模型,你会建议什么?将其存储为数组?
  • 是的,可能。可能是"renditions":[{ "key_name": "thumbnail", "value": {...} },...]。在这种情况下,您可能希望使用 nested 类型。

标签: elasticsearch


【解决方案1】:

您可以尝试使用动态模板进行此映射:

PUT /test

PUT /test/_mapping/test
{
   "properties": {
      "id": {
         "type": "string"
      },
      "headline": {
         "type": "string"
      }
   },
   "dynamic_templates": [
      {
         "renditions_objects": {
            "mapping": {
               "dynamic": "strict",
               "type": "object",
               "properties": {
                  "height": {
                     "type": "integer"
                  },
                  "href": {
                     "type": "string"
                  },
                  "mimetype": {
                     "type": "string"
                  },
                  "width": {
                     "type": "integer"
                  }
               }
            },
           "match_mapping_type": "object",
           "path_match": "renditions.*"            
         }      
      }
   ]
}

线

"dynamic": "strict",

确保您不能索引任何存在额外字段的文档。如果您尝试对以下内容进行索引,该过程将失败:

POST /test/test
{
  "id": "5644596f9bf67301645999d9",
  "headline": "Scientists Look Beyond Solar System to Study Planet",
  "renditions": {
    "baseImage": {
      "height": 933,
      "href": "www.imgur.com/animage",
      "mimetype": "image/jpeg",
      "width": 1400,
      "dummy": 12
    },
    "preview": {
      "height": 500,
      "href": "www.imgur.com/animage",
      "mimetype": "image/jpeg",
      "width": 400
    },
    "thumbnail": {
      "height": 150,
      "href": "www.imgur.com/animage",
      "mimetype": "image/jpeg",
      "width": 125
    }
  }
}

注意baseImage 中额外的dummy 键。它将失败并出现错误:

{
   "error": "StrictDynamicMappingException[mapping set to strict, dynamic introduction of [dummy] within [renditions.baseImage] is not allowed]",
   "status": 400
}

如果你想让它稍微轻松一点,并允许对此类文档进行索引,但不希望对额外的字段进行索引,则将dynamic 设置为false。这不会在索引此类文档时导致任何失败,额外的字段也将出现在 _source 字段中。但是,该字段不会被索引,您将无法对该字段执行任何查询或过滤。希望这会有所帮助

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2017-08-30
相关资源
最近更新 更多