简体   繁体   中英

python elasticsearch-dsl parent child relationship

I started using the python library elasticsearch-dsl .

I am trying to implement a parent-child relationship but it is not working:

    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()

elasticsearch-dsl has parent-child relationship built in using 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')

How to insert and query (HT to @Maresh):
- DSL get: ChildDoc.get(id=child_id, routing=parent_id)
- DSL insert: I believe it's child.save(id=child_id, routing=parent_id)
- Dictionary insert: specify '_parent': parent_id in dictionary

OK, thanks all. The simple and messy solution that worked for me was to use:

from elasticsearch_dsl import Mapping

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

BEFORE creating the parent doc-type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM