如何在elasticsearch中组合模式分析器和char_filter

时间:2015-04-09 19:02:32

标签: elasticsearch

我有一个我想要标记的关键字字段(用逗号分隔),但它也可能包含带有“+”字符的值。例如:

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": ""
            }
        }

2 个答案:

答案 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": ""
        }
    }