如何使用动态类型名称编写Elasticsearch映射

时间:2017-07-10 06:16:45

标签: elasticsearch elasticsearch-5

我有一个名为test execution的概念实体,每个测试执行都应该是Elasticsearch索引中的一个单独的类型。每个测试执行类型的映射应该相同,并将动态添加到索引中。

我已经为单个测试执行创建了一个映射,如下所示,我想对将来创建的所有类型进行推广。

PUT /test_tool/_mapping/test_execution_20151710_1324_12
{
    "properties": {
        "timestamp":{
            "type": "string",
            "index": "not_analyzed"
        },
        "source":{
            "type": "string",
            "index": "not_analyzed"
        },
            "payload":{
            "type": "string",
            "index": "not_analyzed"
        }
    }
}

如何为动态类型创建通用映射,例如:为“test_execution _ *”类型创建通配符。

[更新]

在查看以下答案后,我考虑过不对不同的执行使用单独的类型,并希望使用单独的密钥来识别同一测试执行中的文档。

PUT /test_tool/_mapping/executions
{
    "properties": {
        "timestamp":{
            "type": "string",
            "index": "not_analyzed"
        },
        "source":{
            "type": "string",
            "index": "not_analyzed"
        },
            "payload":{
            "type": "string",
            "index": "not_analyzed"
        },
            "test_execution":{
            "type": "string",
            "index": "not_analyzed"
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您需要的是使用模板from official docs

   PUT _template/template_1
{
  "template": "test_too*",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}
  

定义名为template_1的模板,模板模式为te *。设置和映射将应用于与te *模式匹配的任何索引名称。

因此,在您的情况下,您会想要使用类似:"template": "test_to*"

的内容

答案 1 :(得分:1)

我不确定每次执行都有自己的类型是否是一个好主意(我会避免使用它),因为这会破坏你的映射。

您可以使用索引模板中的_default_类型实现此目的,请参阅the docs

然而,类型将在Elasticsearch的下一个版本中消失,所以也许你想重新考虑这个策略。