我有一个我想要标记的关键字字段(用逗号分隔),但它也可能包含带有“+”字符的值。例如:
query_string.keywords = Living,Music,+concerts+and+live+bands,News,Portland
创建索引时,以下功能可以很好地将关键字拆分为逗号:
{
"settings": {
"number_of_shards": 5,
"analysis": {
"analyzer": {
"happy_tokens": {
"type": "pattern",
"pattern": "([,]+)"
}
}
}
},
"mappings": {
"post" : {
"properties" : {
"query_string.keywords" : {
"type": "string",
"analyzer" : "happy_tokens"
}
}
}
}
}
如何在此添加char_filter(见下文)以将+更改为空格或空字符串?
"char_filter": {
"kill_pluses": {
"type": "pattern_replace",
"pattern": "+",
"replace": ""
}
}
答案 0 :(得分:3)
我发现“映射”char_filter
可以将我的加号字符带到空格。标记后,我能够trim
标记删除空格。
custom analyzers page in the elasticsearch guide是一个很大的帮助。
我的工作示例如下:
{
"settings": {
"number_of_shards": 5,
"index": {
"analysis": {
"char_filter": {
"plus_to_space": {
"type": "mapping",
"mappings": ["+=>\\u0020"]
}
},
"tokenizer": {
"split_on_comma": {
"type": "pattern",
"pattern": "([,]+)"
}
},
"analyzer": {
"happy_tokens": {
"type": "custom",
"char_filter": ["plus_to_space"],
"tokenizer": "split_on_comma",
"filter": ["trim"]
}
}
}
}
},
"mappings": {
"post" : {
"properties" : {
"query_string.keywords" : {
"type": "string",
"analyzer" : "happy_tokens"
}
}
}
}
}
答案 1 :(得分:1)
你需要逃避" +"," +"正则表达式中有special meaning。
"char_filter": {
"kill_pluses": {
"type": "pattern_replace",
"pattern": "\+",
"replace": ""
}
}