创建索引后创建自定义分析器

Cri*_*nto 6 analyzer elasticsearch

我正在尝试添加自定义分析器。

curl -XPUT 'http://localhost:9200/my_index' -d '{
    "settings" : {
        "analysis" : {
            "filter" : {
                "my_filter" : {
                    "type" : "word_delimiter",
                    "type_table": [": => ALPHA", "/ => ALPHA"]
                }
            },
            "analyzer" : {
                "my_analyzer" : {
                    "type" : "custom",
                    "tokenizer" : "whitespace",
                    "filter" : ["lowercase", "my_filter"]
                }
            }
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

当我可以在每次需要时重新创建索引时,它在我的本地环境中工作,当我尝试在其他环境(如 qa 或 prod 已创建索引)上执行相同操作时,问题就出现了。

{
    "error": "IndexAlreadyExistsException[[my_index] already exists]",
    "status": 400
}
Run Code Online (Sandbox Code Playgroud)

如何通过 HTTP API 添加我的自定义分析器?

Cri*_*nto 6

文档中,我发现要更新索引设置,我可以这样做:

curl -XPUT 'localhost:9200/my_index/_settings' -d '
{
    "index" : {
        "number_of_replicas" : 4
    }
}'
Run Code Online (Sandbox Code Playgroud)

并更新分析器设置文档说:

“……需要先关闭索引,修改完成后再打开。”

所以我最终这样做了:

curl -XPOST 'http://localhost:9200/my_index/_close'

curl -XPUT 'http://localhost:9200/my_index' -d '{
    "settings" : {
        "analysis" : {
            "filter" : {
                "my_filter" : {
                    "type" : "word_delimiter",
                    "type_table": [": => ALPHA", "/ => ALPHA"]
                }
            },
            "analyzer" : {
                "my_analyzer" : {
                    "type" : "custom",
                    "tokenizer" : "whitespace",
                    "filter" : ["lowercase", "my_filter"]
                }
            }
        }
    }
}'

curl -XPOST 'http://localhost:9200/my_index/_open'
Run Code Online (Sandbox Code Playgroud)

这为我解决了所有问题。

  • 除了 `_close` 和 `_open`,我发现它可以与带有 `_settings` 端点的 `curl -XPUT'http://localhost:9200/my_index/_settings` 一起使用,而不是直接使用 `PUT` 到 `/my_index`。相应地调整 json 级别。 (2认同)