Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 693
Renjith Avatar asked Jan 28 '16 18:01

Renjith


1 Answers

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

like image 148
Kamil Sindi Avatar answered Sep 23 '22 16:09

Kamil Sindi