3

I am using bulk index in elasticsearch-py to add documents that contain nested items in the form of arrays of dictionaries (address in this case):

{'Name': 'Smith, John',
 'ID': '3327',
 'Nationality': 'English',
 'address': [
      {
      'ID': '5',
      'City': 'Milwaukee',
      'Latlon': '43.0526,-87.9199',
      'State': 'WI'
      },
     ... 
  ]
}

I create a list of actions, one for each document like so:

{
   "_index": "myindex",
   "_type": "mytype",
   "_source": the_doc
}

And then push the actions via helpers.bulk(es, actions)

I want to specify that address is a nested object. Where do I indicate this to ES with elasticsearch-py?

1 Answer 1

2

see: https://github.com/elastic/elasticsearch-py/issues/278#issuecomment-145923743

I used the DSL library. I created a mytype.py file:

from elasticsearch_dsl import DocType, Nested, String

class MyType(DocType):
    name = String()
    nationality = String()

    address = Nested(
        include_in_parent=True,
        properties={
            'location': String(),
            'state': String(),
            'city': String(),
        }
    )

Then I include this file and put the mappings into elasticsearch with include_in_parent to allow for highlighting and others:

from elasticsearch_dsl import Index
from mytype import MyType

myindex = Index('myindex')
myindex.doc_type(MyType)
myindex.create()
Sign up to request clarification or add additional context in comments.

2 Comments

Since you specified the doc type as part of the index, how did you proceed to use helpers.bulk?
@dter i did it in two phases: first i create the index itself as described then build the action array with the proper structure (my actions don't use the custom class but a dictionary) and push it via helpers.bulk. the custom class ensures that the actions get mapped properly in the index. not sure if it is the right way to do it but it works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.