Python elasticsearch-dsl parent child relation

I started using the python elasticsearch-dsl library .

I am trying to implement a parent-child relationship, but it does not work:

    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
source share
2 answers

elasticsearch-dsl has parent-child relationships built 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 that child.save(id=child_id, routing=parent_id)
- Insert dictionary: specify '_parent': parent_idin the dictionary

+7
source

, . , , :

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

+1

Source: https://habr.com/ru/post/1626417/


All Articles