我一直在尝试在 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"
}
}
}
}
}
有什么方法可以定义这个分析器,以便它可以用作索引模板中所有类型的所有属性的默认值?
最佳答案
你可以试试dynamic-templates ,比如:
// 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”分析器。
关于elasticsearch - 注册自定义分析器并在模板中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23463466/