弹性搜索何时添加动态映射

时间:2015-11-16 15:54:14

标签: dynamic elasticsearch mapping

我一直在使用弹性搜索(ES)动态映射。好像我在陷阱22。 https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html

主要目标是将所有字符串存储到ES中。

我尝试了什么:

  1. 在ES中,在索引完成之前,您无法创建动态映射 创建。好的,有道理。

  2. 我无法创建空索引,所以如果     发送到索引的第一项不是字符串,我不能     重新分配......我不知道第一个是什么类型的对象     由于应用程序接受各种对象/事件的方式,索引中的项目可以是任何类型。

  3. 所以如果我不能提前创建映射,并且我无法插入空索引来创建映射,而事实上我无法更改映射,我该如何处理第一项如果它不是一个字符串???

    这是我目前正在做的事情(使用Javascript客户端)。

    createESIndex = function (esClient){
        esClient.index({
            index: 'timeline-2015-11-21',
            type: 'event',
            body: event
        },function (error, response) {
            if (error) {
                logger.log(logger.SEVERITY.ERROR, 'acceptEvent elasticsearch create failed with: '+ error + " req:" + JSON.stringify(event));
                console.log(logger.SEVERITY.ERROR, 'acceptEvent elasticsearch create failed with: '+ error + " req:" + JSON.stringify(event));
                res.status(500).send('Error saving document');
            } else {
                res.status(200).send('Accepted');
            }
        });
    }
    
    esClientLookup.getClient( function(esClient) {
    
        esClient.indices.putTemplate({
            name: "timeline-mapping-template",
            body:{
                "template": "timeline-*",
                "mappings": {
                    "event": {
                        "dynamic_templates": [
                            { "timestamp-only": {
                                  "match":              "@timestamp",
                                  "match_mapping_type": "date",
                                  "mapping": {
                                      "type":           "date",
                                  }
                            }},
                            { "all-others": {
                                  "match":              "*",
                                  "match_mapping_type": "string",
                                  "mapping": {
                                      "type":           "string",
                                  }
                                }
                            }
                        ]
                    }
                }
            }
        }).then(function(res){
            console.log("put template response: " + JSON.stringify(res));
            createESIndex(esClient);
    
        }, function(error){
            console.log(error);
            res.status(500).send('Error saving document');
        });
    });
    

1 个答案:

答案 0 :(得分:1)

Index templates救援!!这正是您所需要的,我们的想法是创建索引模板,并且只要您希望在该索引中存储文档,ES就会使用您提供的映射(即使是动态映射)为您创建它

curl -XPUT localhost:9200/_template/my_template -d '{
  "template": "index_name_*",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type_name": {
      "dynamic_templates": [
        {
          "strings": {
            "match": "*",
            "match_mapping_type": "*",
            "mapping": {
              "type": "string"
            }
          }
        }
      ],
      "properties": {}
    }
  }
}'

然后,当您索引名称与index_name_*匹配的索引中的任何内容时,将使用上面的动态映射创建索引。

例如:

curl -XPUT localhost:9200/index_name_1/type_name/1 -d '{
  "one": 1,
  "two": "two", 
  "three": true
}'

这将创建一个名为index_name_1的新索引,其type_name的映射类型所有属性都为string。您可以使用

验证
curl -XGET localhost:9200/index_name_1/_mapping/type_name

响应:

{
  "index_name_1" : {
    "mappings" : {
      "type_name" : {
        "dynamic_templates" : [ {
          "strings" : {
            "mapping" : {
              "type" : "string"
            },
            "match" : "*",
            "match_mapping_type" : "*"
          }
        } ],
        "properties" : {
          "one" : {
            "type" : "string"
          },
          "three" : {
            "type" : "string"
          },
          "two" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

请注意,如果您愿意通过Javascript API执行此操作,则可以使用indices.putTemplate来电。