尽管已停用dynamic_mapping,Logstash仍会继续创建字段

时间:2017-09-05 09:12:09

标签: templates elasticsearch mapping logstash

我已经定义了我自己的模板供logstash使用,我已经停用了动态映射:

{
    "my_index": {
        "order": 0,
        "template": "my_index",
        "settings": {
            "index": {
                "mapper": {
                    "dynamic": "false"
                },
                "analysis": {
                    "analyzer": {
                        "nlp_analyzer": {
                            "filter": [
                                "lowercase"
                            ],
                            "type": "custom",
                            "tokenizer": "nlp_tokenizer"
                        }
                    },
                    "tokenizer": {
                        "nlp_tokenizer": {
                            "pattern": ""
                            "(\w+)|(\s*[\s+])"
                            "",
                            "type": "pattern"
                        }
                    }
                },
                "number_of_shards": "1",
                "number_of_replicas": "0"
            }
        },
        "mappings": {
            "author": {
                "properties": {
                    "author_name": {
                        "type": "keyword"
                    },
                    "author_pseudo": {
                        "type": "keyword"
                    },
                    "author_location": {
                        "type": "text",
                        "fields": {
                            "standard": {
                                "analyzer": "standard",
                                "term_vector": "yes",
                                "type": "text"
                            },
                            "nlp": {
                                "analyzer": "nlp_analyzer",
                                "term_vector": "yes",
                                "type": "text"
                            }
                        }
                    }
                }
            }
        }
    }
}

为了测试elasticsearch是否不会生成新字段,我尝试让我的事件中的字段不在我的映射中,让我们说我有这个事件:

{
“type” => “author”,
“author_pseudo” => “chloemdelorenzo”,
“author_name” => “Chloe DeLorenzo”,
“author_location” => “US”,
}

索引此事件时,Elasticsearch将在映射中生成一个新字段:

"type": {
     "type": "text",
      "fields": {
           "keyword": {
                "type": "keyword",
                "ignore_above": 256
           }
      }
 }

我知道Logstash正在使用我的模板,因为在我的映射中我使用自定义分析器,我可以找回它生成的映射。但显然没有考虑到动态字段被禁用。

我希望elasticsearch忽略我的映射中不存在的字段,但要索引具有已定义映射的字段。如何避免logstash创建新字段?

2 个答案:

答案 0 :(得分:1)

您应该在文档类型级别强制执行映射。

https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-mapping.html

  

无论此设置的值如何,仍可添加类型   在创建索引或使用PUT映射API时显式。

所以你的映射看起来像:

"mappings": {
    "author": {
        "dynamic": false,
        "properties": {
            "author_name": {
                "type": "keyword"
            },
            "author_pseudo": {
                "type": "keyword"
            },
            "author_location": {
                "type": "text",
                "fields": {
                    "standard": {
                        "analyzer": "standard",
                        "term_vector": "yes",
                        "type": "text"
                    },
                    "nlp": {
                        "analyzer": "nlp_analyzer",
                        "term_vector": "yes",
                        "type": "text"
                    }
                }
            }
        }
    }
}

答案 1 :(得分:0)

这个答案并不完全是您的要求,但您可以使用logstash过滤器手动删除字段,如下所示:

filter {
  mutate {
    remove_field => ["fieldname"]
  }
}

如果您的活动有已定义的字段列表,您可以通过这种方式解决问题。