Dec 8th, 2018: [EN][Python/Elasticsearch] Getting started with Elasticsearch DSL and Python

As we can see we are again relying on the knowledge of Elasticsearch to tell us where to use Keyword vs Text, including the use of multi-fields where the syntax exactly copies the one in Elasticsearch. The only option that is python-only is the multi=True which signifies that this field is always expected to hold a list. Like Elasticsearch we place no limit on whether a field contains a single value or an array. Setting multi to True just makes a python list the default so that those fields would default to an empty list making it possible for us to just start appending items to it:

# create an empty commit object
c = Commit()
# accessing c.files gives us an empty list so we directly start appending
c.files.append('__init__.py')

Just defining the python class doesn't create the index or populate the mappings in Elasticsearch so we first need to do that:

Commit.init()

Now we have an index created in Elasticsearch, including the proper mappings and settings that we asked for in our Commit class. This is an important step and needs to always be done before we start indexing any data into Elasticsearch.

Next time we will learn how to load data into our newly created index and then how to run queries and aggregations, stay tuned!

Acknowledgements

I cannot talk or write about elasticsearch-dsl without acknowledging its origins, I borrowed a lot from other projects when designing this library and leaned heavily on the feedback from the excellent Python community. I would like to thank the Django project for inspiration and especially Will Kahn-Greene and Rob Hudson, the maintainers of now deprecated elasticutils for great feedback at the project's conception.

If you are interested in the details of the design process you can see the conference talk about the conception of elasticsearch-dsl here.

1 Like