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

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

最佳答案

我发现了“映射”char_filter,它可以将我的加号字符转换为空格。标记化后,我能够修剪标记以删除空格。

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

关于elasticsearch - 如何在elasticsearch中结合模式分析器和char_filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29546816/

相关文章:

elasticsearch - Elasticsearch 7 track_total_hits 如何提高查询速度?

ruby-on-rails - 这种搜索称为什么类型,什么是最好的搜索方式?

elasticsearch - 使用表达式的脚本字段 - 只能访问成员变量 [value] 或成员方法

python - Elasticsearch Python库 “post”一个文档

python - 如何使用 python API 连接到 Elasticsearch 中的特定集群

java - Elasticsearch (Java) - 集成测试

java - Elasticsearch : no configured node available

python - 为什么会出现超出最大尝试次数的错误?

elasticsearch - 按查询更新

elasticsearch - ElasticSearch | SimpleQueryString | FieldBoosting无法正常工作