【发布时间】:2020-08-19 14:32:15
【问题描述】:
我正在开发一个包含 django、elasticsearch 和 django-elasticsearch-dsl 的项目。我正在收集大量数据并将其保存到 postgres 并通过 django-elasticsearch-dsl 将其索引到 elasticsearch。
我遇到了一个我不明白的问题,我也没有任何进一步的提示:
Django的models.py文件的相关部分:
class LinkDenorm(BaseModel):
...
link = CharField(null=True, max_length=2710, db_index=True)
link_expanded = TextField(null=True, db_index=True)
title = TextField(null=True, db_index=True)
text = TextField(null=True)
...
django-elasticsearch-dsl documents.py 文件的相关部分:
@registry.register_document
class LinkDenorm(Document):
link_expanded = fields.KeywordField(attr='link_expanded')
class Index:
name = 'denorms_v10'
class Django:
model = models.LinkDenorm
fields = [
...
'link',
'title',
'text',
...
]
成功索引数据后,我验证索引是否包含正确的字段:
curl -X GET -u <myuser>:<mypasswd> "http://<my-hostname>/denorms_v10/?pretty"
{
"denorms_v10" : {
"mappings" : {
"properties" : {
...
"link" : {
"type" : "text"
},
"title" : {
"type" : "text"
},
"text" : {
"type" : "text"
}
"link_expanded" : {
"type" : "keyword"
},
...
}
}
}
}
经过一定时间(有时是几周,有时是几天)后,索引字段会发生变化。执行与以前相同的 CURL 查找会给我:
curl -X GET -u <myuser>:<mypasswd> "http://<my-hostname>/denorms_v10/?pretty"
{
"denorms_v10" : {
"mappings" : {
"properties" : {
...
"link" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"text" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"link_expanded" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
...
}
}
}
}
更改发生后,查询失败,因为数据类型不正确。在调查了 elasticsearch 和 django 日志之后,没有任何线索可以说明索引会发生什么。
我有点迷茫,没有想法。任何建议都非常受欢迎。谢谢!
【问题讨论】:
-
一旦设置了字段类型,它们就不可更改,修改后的行为看起来像 Elastic 的默认(无模式)自动映射,它尝试获取字段类型并存储字符字段的文本和关键字版本
标签: django elasticsearch elasticsearch-dsl