Elasticsearch-dsl-py: Multiple levels of nesting

Created on 9 Jun 2016  路  9Comments  路  Source: elastic/elasticsearch-dsl-py

Is it possible to have multiple levels of nesting defined in persistence using elasticsearch-dsl-py?

Most helpful comment

Yes exactly. Following the documentation, would it be something like this instead?

class Department(InnerObjectWrapper):
    pass

class Employee(InnerObjectWrapper):
    pass

class Company(DocType):
    departments = Nested(
        doc_class = Department,
        properties = {
            'name' : String(),
            'employees' : Nested(
                doc_class = Employee,
                properties = {
                    'lastName' : String(),
                    }
                ),
            }
        )

All 9 comments

Absolutely, I assume you mean nested fields?

class Company(DocType):
    departments = Nested(properties={"employees": Nested()})

Yes exactly. Following the documentation, would it be something like this instead?

class Department(InnerObjectWrapper):
    pass

class Employee(InnerObjectWrapper):
    pass

class Company(DocType):
    departments = Nested(
        doc_class = Department,
        properties = {
            'name' : String(),
            'employees' : Nested(
                doc_class = Employee,
                properties = {
                    'lastName' : String(),
                    }
                ),
            }
        )

Yes, that is exactly what it would look like.

Thank you HonzaKral.

@HonzaKral Can the properties be defined within each InnerObjectWrapper or does it have to be explicitly passed into the Nested class as a properties dictionary key/value argument?

@avelis not on the InnerObjectWrapper but on the Object/Nested field instead:

class Company(DocType):
    department = Nested()
    department.field('title', Text())
    department.field('tags', Keyword(multi=True))

Is it possible to define multilevel nesting by breaking it out into multiple classes? This syntax starts to be very unwieldy with multiple levels of nesting.

Example:

class BurrowKafkaPartitionWindowMarker(Nested):
    offset = Integer()
    timestamp = Date()
    lag = Integer()

class BurrowKafkaPartitionStatus(Nested):
    topic = String()
    partition = Integer()
    status = String()
    window_start = BurrowKafkaPartitionWindowMarker()
    window_end = BurrowKafkaPartitionWindowMarker()

class BurrowKafkaStatus(Nested):
    cluster = String()
    group = String()
    status = String()
    complete = Boolean()
    partitions = BurrowKafkaPartitionStatus(multi=True)
    partition_count = Integer()
    maxlag = BurrowKafkaPartitionStatus()
    totallag = Integer()

class BurrowKafkaLagReport(DocType):
    error = Boolean(required=True)
    message = String(required=True)
    status = BurrowKafkaStatus()
    request_url = String(required=True)

If I used the properties = { ... } syntax for this, it would look horrendous.

Unfortunately my cleaner syntax doesn't seem to work, for whatever reason. Nothing in the BurrowKafkaLagReport.status object gets saved to Elasticsearch.

@petergaultney I agree. The best solution I have found is the one detailed by @JoshCoady here. It would be nice if there were documentation or support for doing this natively.

@petergaultney thank you for the inspiration, I started an implementation as https://github.com/elastic/elasticsearch-dsl-py/pull/790

any feedback is more than welcome!

Was this page helpful?
0 / 5 - 0 ratings