gpt4 book ai didi

django - 在wagtail中索引自定义Django模型字段

转载 作者:行者123 更新时间:2023-12-02 22:48:53 26 4
gpt4 key购买 nike

我们正在使用Django MarkupField存储Markdown文本,并且效果很好。

但是,当我们尝试在Wagtail中为这些字段建立索引时,会从Elasticsearch中获得序列化错误,如下所示:

File "/usr/local/lib/python3.5/dist-packages/wagtail/wagtailsearch/management/commands/update_index.py", line 120, in handle
self.update_backend(backend_name, schema_only=options.get('schema_only', False))
File "/usr/local/lib/python3.5/dist-packages/wagtail/wagtailsearch/management/commands/update_index.py", line 87, in update_backend
index.add_items(model, chunk)
File "/usr/local/lib/python3.5/dist-packages/wagtail/wagtailsearch/backends/elasticsearch.py", line 579, in add_items
bulk(self.es, actions)
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/helpers/__init__.py", line 195, in bulk
for ok, item in streaming_bulk(client, actions, **kwargs):
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/helpers/__init__.py", line 162, in streaming_bulk
for bulk_actions in _chunk_actions(actions, chunk_size, max_chunk_bytes, client.transport.serializer):
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/helpers/__init__.py", line 61, in _chunk_actions
data = serializer.dumps(data)
File "/usr/local/lib/python3.5/dist-packages/elasticsearch/serializer.py", line 50, in dumps
raise SerializationError(data, e)
elasticsearch.exceptions.SerializationError: ({'_partials': [<markupfield.fields.Markup object at 0x7faa6e238e80>, <markupfield.fields.Markup object at 0x7faa6dbc4da0>], 'pk': '1', 'research_interests': <markupfield.fields.Markup object at 0x7faa6e238e80>, 'bio': <markupfield.fields.Markup object at 0x7faa6dbc4da0>}, TypeError("Unable to serialize <markupfield.fields.Markup object at 0x7faa6e238e80> (type: <class 'markupfield.fields.Markup'>)",))

一种解决方法是对返回 field.raw的可调用对象建立索引,但随后我们必须为模型中的每个Markdown字段属性编写一个这样的可调用对象。我以为我们可以通过使用 Markup方法扩展field属性(即django-markupfield MarkupField类来替换 get_searchable_content(value))来解决此问题,但是序列化错误仍然存​​在。

有没有人在Wagtail + elasticsearch中为自定义Django字段建立索引的提示?

最佳答案

有几种方法可以做到这一点。最好的方法是在elasticsearch-dsl中创建自己的字段,例如参见(0),并将其用于(反)序列化。另一种选择是创建自己的JSONSerializer(1)子类,并将其作为serializer=MyJSONSerializer()传递到Elasticsearch构造函数中,该构造函数可以处理markupfield.fields.Markup对象。

0-https://github.com/elastic/elasticsearch-dsl-py/blob/master/test_elasticsearch_dsl/test_document.py#L49-L58
1-https://github.com/elastic/elasticsearch-py/blob/master/elasticsearch/serializer.py#L24

关于django - 在wagtail中索引自定义Django模型字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46859022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com