简体   繁体   English

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

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

So I'm trying to apply an analyzer to my index but no matter what I do I get some sort of error.所以我试图将分析器应用于我的索引,但无论我做什么,我都会遇到某种错误。 I've been looking stuff up all day but can't get it to work.我整天都在找东西,但无法正常工作。 If I run it as it is below, I get an error which says如果我按照下面的方式运行它,我会收到一个错误,上面写着

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

if I add a "mappings" below the body= part of the code and above the "properties" part, I get this error如果我在代码的 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}}}} (and it'll go through every name in the body part of the code) 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}}}} (它将 go 通过代码正文部分的每个名称)

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)

Any help would be appreciated.任何帮助,将不胜感激。

Your analyzer is simply missing a name, you should specify it like this:您的分析器只是缺少一个名称,您应该像这样指定它:

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

You need to install this analyzer using您需要使用安装此分析器

elasticsearch.indices.put_settings(...)

Also in the mappings section, you need to reference the analyzer by name, so you simply need to add the analyzer name as a string同样在映射部分,您需要按名称引用分析器,因此您只需将分析器名称添加为字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM