7

我开始使用 python 库elasticsearch-dsl

我正在尝试实现父子关系,但它不起作用:

    class Location(DocType):
        name = String(analyzer='snowball', fields={'raw': String(index='not_analyzed')})
        latitude = String(analyzer='snowball')
        longitude = String(analyzer='snowball')
        created_at = Date()

   class Building(DocType):
       parent = Location()
4

2 回答 2

10

elasticsearch-dsl 使用MetaField内置了父子关系:

class Location(DocType):
    name = String(analyzer='snowball', fields={'raw': String(index='not_analyzed')})
    latitude = String(analyzer='snowball')
    longitude = String(analyzer='snowball')
    created = Date()

    class Meta:
        doc_type = 'location' 

class Building(DocType):

    class Meta:
        doc_type = 'building'
        parent = MetaField(type='location')

如何插入和查询(HT 到 @Maresh):
- DSL 获取:ChildDoc.get(id=child_id, routing=parent_id)
- DSL 插入:我相信它是child.save(id=child_id, routing=parent_id)
- 字典插入:'_parent': parent_id在字典中 指定

于 2016-03-14T18:57:14.610 回答
1

好的,谢谢大家。对我有用的简单而混乱的解决方案是使用:

from elasticsearch_dsl import Mapping

mcc = Mapping(typeChild)
mcc.meta('_parent', type=typeParent)
mcc.field(fieldName, 'string', fielddata=True, store=True)
mcc.save(index)

在创建父文档类型之前

于 2016-11-27T13:46:36.453 回答