我正在建立目录列表类型的网站,我正在使用elasticsearch7实现搜索。网站是建立在django之上,使用django elasticsearch dsl(即在引擎盖下使用elasticsearch dsl)。
我在数据库中的所有记录都具有相同的总体结构,只是在一个字段上有所不同。这是django elasticsearch dsl库用于定义文档的示例代码。
from django_elasticsearch_dsl.registries import registry
from records.models import Record
@registry.register_document
class RecordDocument(Document):
"""
Elastic-search index document for records.
"""
category = fields.KeywordField(attr='category_to_es')
exposed_until = fields.DateField(attr='exposed_until')
active_until = fields.DateField(attr='active_until')
active = fields.BooleanField(attr='active')
media_count = fields.IntegerField()
url = fields.KeywordField(attr='get_absolute_url')
meta = fields.NestedField(dynamic=True)
def prepare_meta(self, instance):
if instance.category > 5:
return {'}
return {..}
在这种情况下,元字段可以根据记录类别包含不同的属性。若我简单地将字典传递到meta字段,elasticsearch会将所有内容存储为文本。在尝试对嵌套对象进行聚合时,这会导致问题。
下面是一个简单地将字典转储到元字段时出错的例子。
requesterror(400,'搜索\阶段\执行\异常','文本字段未针对需要每个文档字段数据(如聚合和排序)的操作进行优化
因此,最明显的是使用适当的嵌套对象表示法,而不是将所有内容存储为文本的动态对象表示法。
这是我想要实现的一个例子,其中meta字段根据记录类别的不同而不同。
class RecordDocument(Document):
....
meta = fields.NestedField(properties={
'key1': fields.KeywordField(),
'key2': field.KeywordField()
})
我的目标是能够在嵌套文档上进行聚合。
问题是:
索引时如何更改每条记录的嵌套字段
像这样的事情被认为是不好的做法吗?我想避免使用多个索引,因为我有很多不同的元字段类型。
暂无答案!
目前还没有任何答案,快来回答吧!