使用Python创建Elasticsearch索引时遇analyser参数错误求助
解决方案
你遇到的错误核心原因是拼写错误:Elasticsearch使用美式拼写analyzer,但你代码里全部写成了英式拼写analyser,这导致Elasticsearch无法识别该参数。
修正步骤:
- 将
settings中analysis下的analyser改为analyzer - 将每个text字段定义里的
analyser改为analyzer - 显式指定
es.indices.create的index参数,符合Python API 7.x的规范
修正后的完整代码:
def create_expert_index(index_name="expert_query"): es = connect() if es.indices.exists(index=index_name): es.indices.delete(index=index_name, ignore=[400, 404]) mappings = { "settings": { "analysis": { "analyzer": { # 修正拼写:analyser → analyzer "standard_asciifolding": { "type": "custom", "tokenizer": "standard", "filter": ["asciifolding", "lowercase"] } } } }, "mappings": { "properties": { "first_name": { "type": "text", "analyzer": "standard_asciifolding" # 修正拼写 }, "last_name": { "type": "text", "analyzer": "standard_asciifolding" # 修正拼写 }, "email": { "type": "text", "analyzer": "standard_asciifolding" # 修正拼写 }, "specializations": { "type": "text", "analyzer": "standard_asciifolding" # 修正拼写 }, "institution": { "type": "text", "analyzer": "standard_asciifolding" # 修正拼写 } } } } es.indices.create(index=index_name, body=mappings)
运行修正后的代码即可成功创建索引。
内容的提问来源于stack exchange,提问作者Francois Gosselin
相关产品推荐
相关产品推荐

