首页 > 解决方案 > 根据 7.X 语法使用时,Elasticsearch 分析 API 显示 1.X 版本的错误令牌

问题描述

在处理用户的一个查询时,最初我认为他使用的是最新版本,而当他显示分析 API时,这令人惊讶。

需要检查令牌的自定义分析器

{
    "settings": {
        "analysis": {
            "filter": {
                "splcharfilter": {
                    "type": "pattern_capture",
                    "preserve_original": true,
                    "patterns": [
                        "([?/])"
                    ]
                }
            },
            "analyzer": {
                "splcharanalyzer": {
                    "tokenizer": "keyword",
                    "filter": [
                        "splcharfilter",
                        "lowercase"
                    ]
                }
            }
        }
    }
}

分析 API

POST /_analyze

{
    "analyzer": "splcharanalyzer",
    "text" : "And/or"
}

Output

{
    "tokens": [
        {
            "token": "analyzer", --> why this token
            "start_offset": 7,
            "end_offset": 15,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "splcharanalyzer", --> why this token
            "start_offset": 19,
            "end_offset": 34,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "text", --> why this token
            "start_offset": 42,
            "end_offset": 46,
            "type": "<ALPHANUM>",
            "position": 3
        },
        {
            "token": "and",
            "start_offset": 51,
            "end_offset": 54,
            "type": "<ALPHANUM>",
            "position": 4
        },
        {
            "token": "or",
            "start_offset": 58,
            "end_offset": 60,
            "type": "<ALPHANUM>",
            "position": 5
        }
    ]
}

正如上面清楚显示的那样,它生成了这么多不正确的令牌,当检查时用户提到他使用的是 1.7 版本并遵循最新版本的 elasticsearch 中提供的语法。

标签: elasticsearchelasticsearch-analyzerselasticsearch-1.7.5

解决方案


由于 Elasticsearch 1.X 版本相当老旧,并且 Elasticsearch 默认会打开最新版本的 API,并且想知道分析 API 以解决 Elasticsearch 中的许多问题的重要性,我在这里发布了 1.X 版本的正确语法,希望这会帮助其他旧版本的 Elasticsearch 用户。

可以在此处找到 Elasticsearch 1.X 分析 API 文档,下面是为问题中提到的文本生成的正确标记。

  GET  /_analyze?analyzer=splcharanalyzer&text=And/or --> note its GET request

And/or为 1.X 发布的有问题的分析器生成的正确令牌

{
    "tokens": [
        {
            "token": "and/or",
            "start_offset": 0,
            "end_offset": 6,
            "type": "word",
            "position": 1
        },
        {
            "token": "/",
            "start_offset": 0,
            "end_offset": 6,
            "type": "word",
            "position": 1
        }
    ]
}

推荐阅读