python elasticsearch-dsl父子关系

时间:2016-01-28 18:09:18

标签: python elasticsearch elasticsearch-dsl

我开始使用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()

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)

在创建父doc-type

之前