Persisting Nested Objects with elasticsearch-dsl


(Andy Barilla) #1

I'm attempting to persist an object to elasticsearch with a nested object. However instead of using syntax like:

child = field.Object(properties={'name': field.String()})

I'd like to have child be it's own object.

from elasticsearch_dsl import field, document
from elasticsearch_dsl import connections
connections.connections.create_connection(hosts=['localhost'], timeout=20)


class Child(field.Object):
    name = field.String()


class Parent(document.DocType):
    name = field.String()
    child = Child()

    class Meta:
        index = 'myindex'

parent = Parent(name='test parent')
parent.child = Child(name='test other child')
parent.save()

However, doing it this way results in:

  File "/home/abarilla/PycharmProjects/iocdb/iocdb/model/test.py", line 25, in <module>
    parent.child = Child(name='test other child')
  File "/home/abarilla/.virtualenvs/iocdb/local/lib/python2.7/site-packages/elasticsearch_dsl/document.py", line 115, in __setattr__
    return super(DocType, self).__setattr__(name, value)
  File "/home/abarilla/.virtualenvs/iocdb/local/lib/python2.7/site-packages/elasticsearch_dsl/utils.py", line 418, in __setattr__
    value = self._doc_type.mapping[name].to_python(value)
  File "/home/abarilla/.virtualenvs/iocdb/local/lib/python2.7/site-packages/elasticsearch_dsl/field.py", line 63, in to_python
    return self._to_python(data)
  File "/home/abarilla/.virtualenvs/iocdb/local/lib/python2.7/site-packages/elasticsearch_dsl/field.py", line 133, in _to_python
    return self._doc_class(self.properties, **data)
TypeError: type object argument after ** must be a mapping, not Child

Is there a way I can accomplish this?


(system) #2