Python Elasticsearch:尝试将分析器应用于索引文档时出错

Python Elasticsearch: Errors when trying to apply an analyzer to Index documents

所以我正在尝试将分析器应用于我的索引,但无论我做什么,我都会遇到某种错误。我整天都在寻找东西,但无法让它工作。如果我 运行 它如下所示,我会收到一条错误消息

elasticsearch.exceptions.RequestError: RequestError(400, 'illegal_argument_exception', 'analyzer [{settings={analysis={analyzer={filter=[lowercase], type=custom, tokenizer=keyword}}}}] has not been configured in mappings')

如果我在代码的 body= 部分下方和“属性”部分上方添加一个“映射”,我会收到此错误

elasticsearch.exceptions.RequestError: RequestError(400, 'mapper_parsing_exception', 'Root mapping definition has unsupported parameters: [mappings : {properties={Name={analyzer={settings={analysis={analyzer={filter=[lowercase], type=custom, tokenizer=keyword}}}}(它将遍历代码正文部分中的每个名称)

def text_normalization():
    normalization_analyzer = {
        "settings": {
            "analysis": {
                "analyzer": {
                    "type": "custom",
                    "tokenizer": "keyword",
                    "filter": ["lowercase"]
                }
            }
        }
    }

    elasticsearch.indices.put_mapping(
        index=index_name,
        body={
            "properties": {
                "Year of Birth": {
                    "type": "integer",
                },
                "Name": {
                    "type": "text",
                    "analyzer": normalization_analyzer
                },
                "Status": {
                    "type": "text",
                    "analyzer": normalization_analyzer
                },
                "Country": {
                    "type": "text",
                    "analyzer": normalization_analyzer
                },
                "Blood Type": {
                    "type": "text",
                    "analyzer": normalization_analyzer
                }
            }
        }
    )

    match_docments = elasticsearch.search(index=index_name, body={"query": {"match_all": {}}})
    print(match_docments)

如有任何帮助,我们将不胜感激。

您的分析器只是缺少一个名称,您应该这样指定它:

normalization_analyzer = {
    "settings": {
        "analysis": {
            "analyzer": {
                "normalization_analyzer": {                <--- add this
                    "type": "custom",
                    "tokenizer": "keyword",
                    "filter": ["lowercase"]
                }
            }
        }
    }
}

您需要使用

安装此分析器
elasticsearch.indices.put_settings(...)

同样在映射部分,您需要按名称引用分析器,因此您只需将分析器名称添加为字符串

    body={
        "properties": {
            "Year of Birth": {
                "type": "integer",
            },
            "Name": {
                "type": "text",
                "analyzer": "normalization_analyzer"
            },
            "Status": {
                "type": "text",
                "analyzer": "normalization_analyzer"
            },
            "Country": {
                "type": "text",
                "analyzer": "normalization_analyzer"
            },
            "Blood Type": {
                "type": "text",
                "analyzer": "normalization_analyzer"
            }
        }
    }