注册自定义分析器并在模板中使用它

问题描述 投票:0回答:2

我一直在尝试在elasticsearch中添加自定义分析器,目的是将其用作索引模板中的默认分析器。 到目前为止,当显式定义为属性的分析器(在模板内定义时)时,我已经能够使其工作,但在尝试将其用作默认值时却无法工作。 这是我到目前为止所拥有的:

附加到elasticsearch.yml:

### custom analyzer ###
index:
    analysis:
        analyzer:
            custom_whitespace:
                type: standard
                tokenizer: whitespace
                filter: [lowercase]

Elasticsearch 启动时没有错误,但是当我尝试使用模板创建索引时,例如:

{
    "aliases": {},
    "order": 0,
    "settings": {
        "index.analysis.analyzer.default.stopwords": "_none_",
        "index.analysis.analyzer.default.type": "custom_whitespace",
        "index.refresh_interval": "5s"
    },
    "template": "goldstone-*",
    "mappings": {
        "_default_": {
            "_timestamp": {
                "enabled": true,
                "path": "@timestamp"
            },
            "_source": {
                "enabled": true
            },
            "properties": {
                "@timestamp": {
                    "type": "date"
                }

            }
        }
    }
}

创建索引时出现错误:

IndexCreationException[[goldstone-2014.05.05] failed to create index]; nested: ElasticsearchIllegalArgumentException[failed to find analyzer type [custom_whitespace] or tokenizer for [default]]; nested: NoClassSettingsException[Failed to load class setting [type] with value [custom_whitespace]]; nested: ClassNotFoundException[org.elasticsearch.index.analysis.customwhitespace.CustomWhitespaceAnalyzerProvider]

我能够成功注册自定义分析器的唯一方法是在模板中定义它,但随后我无法使用“index.analysis.analyzer.default.type”参数将其用作默认分析器,仅通过为每个属性指定显式分析器。 该配置看起来像:

{
    "aliases": {},
    "order": 0,
    "settings": {
        "analysis": {
            "analyzer": {
                "custom_whitespace": {
                    "filter": ["lowercase"],
                    "tokenizer": "whitespace"
                }
            }
        },
        "index.analysis.analyzer.default.stopwords": "_none_",
        "index.analysis.analyzer.default.type": "whitespace",
        "index.refresh_interval": "5s"
    },
    "template": "goldstone-*",
    "mappings": {
        "keystone_service_list": {
            "_timestamp": {
                "enabled": true,
                "path": "@timestamp"
            },
            "_source": {
                "enabled": true
            },
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "region": {
                    "type": "string",
                    "analyzer": "custom_whitespace"
                }
            }
        }
   }
}

有什么方法可以定义此分析器,以便它可以用作索引模板中所有类型的所有属性的默认值?

elasticsearch
2个回答
1
投票

您可以尝试动态模板,例如:

// only show 'mappings' part
"mappings": {
    "keystone_service_list": {
        "_timestamp": {
                "enabled": true,
                "path": "@timestamp"
            },
            // _source enabled default
            //"_source": {
            //    "enabled": true
            //},
            "dynamic_templates": [
                {
                    "string_fields": {
                        "match": "*",
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "string",
                            "analyzer": "custom_whitespace"
                        }
                    }
                }
            ]
        }
    }
}

这将对“keystone_service_list”中所有类型为字符串的字段采用“custom_whitespace”分析器。


0
投票

当名称为

default
时,会自动使用自定义分析器,请参阅 https://discuss.elastic.co/t/specifying-default-custom-analyzer/97587

© www.soinside.com 2019 - 2024. All rights reserved.