python - Python Elasticsearch:尝试将分析器应用于索引文档时出错
问题描述
所以我试图将分析器应用于我的索引,但无论我做什么,我都会遇到某种错误。我整天都在找东西,但无法正常工作。如果我按照下面的方式运行它,我会收到一个错误,上面写着
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"
}
}
}
推荐阅读
- java - 在 Anylogic 中使用 Java - 如何将变量字符串转换为类型“TargetLine”
- regex - 获取匹配模式perl的所有出现
- java - 我可以将 Spring WebFlux 应用程序部署为 WAR
- google-cloud-platform - 在谷歌云计算引擎上使用 smtp.gmail.com 和 465 端口从 php 发送电子邮件
- java - Tomcat 不调用控制器 api
- symfony - Symfony3 反序列化
- linux-kernel - centos/redhat 从 5.x 升级到 6.x
- ios - 子类化文本字段时,区域设置未从情节提要中获取
- hibernate - 在 org.hibernate.internal.ExceptionConverterImpl.convertCommitException
- php - 在html提交表单中使用php发送电子邮件不起作用