Skip to main content

Python client for Elasticsearch

Project description

Elasticsearch DSL is a high-level library whose aim is to help with writing and running queries against Elasticsearch. It is built on top of the official low-level client (elasticsearch-py).

Philosophy

The DSL inroduced in this library is trying to stay close to the terminology and strucutre of the actual JSON DSL used by Elasticsearch; it doesn’t try to invent a new DSL, instead it aims at providing a more convenient way how to write, and manipulate, queries without limiting you to a subset of functionality. Since it uses the same terminology and building blocks no special knowledge, on top of familiarity with the query DSL, should be required.

Example

With the low-level client you would write something like this:

from elasticsearch import Elasticsearch
es = Elasticsearch()

response = es.search(
    index="my-index",
    body={
      "query": {
        "filtered": {
          "query": {
            "bool": {
              "must": [{"match": {"title": "python"}}],
              "must_not": [{"match": {"description": "beta"}}]
            }
          },
          "filter": {"term": {"category": "search"}}
        }
      },
      "aggs" : {
        "per_tag": {
          "terms": {"field": "tags"},
          "aggs": {
            "max_lines": {"max": {"field": "lines"}}
          }
        }
      }
    }
)
for hit in response['hits']['hits']:
    print(hit['_score'], hit['_source']['title'])

Which could be very hard to modify (imagine adding another filter to that query) and is definitely no fun to write. With the python DSL you can write the same query as:

from elasticsearch_dsl import Search, Q

s = Search(using=es).index("my-index") \
    .filter("term", category="search") \
    .query("match", title="python")   \
    .query(~Q("match", description="beta"))

s.aggs.bucket('per_tag', 'terms', field='tags')\
    .metric('max_lines', 'max', field='lines')

response = s.execute()
for hit in response:
    print(hit._meta.score, hit.title)

for b in response.aggregations.per_tag.buckets:
    print(b.key, b.max_lines.value)

The library will take care of:

  • composing queries/filters into compound queries/filters

  • creating filtered queries when .filter() has been used

  • providing a convenient wrapper around responses

  • no curly or square brackets everywhere!

Migration

If you already have existing code using the elasticsearch-py library you can easily start using this DSL without committing to porting your entire application. You can create the Search object from current query dict, work with it and, at the end, serialize it back to dict to send over the wire:

body = {...} # insert complicated query here
# convert to search
s = Search.from_dict(body)
# add some filters, aggregations, queries, ...
s.filter("term", tags="python")
# optionally convert back to dict to plug back into existing code
body = s.to_dict()

Since the DSL is built on top of the low-level client there should be nothing stopping you from using your existing code or just dropping down to the low level API whenever required; for example for all the APIs not (yet) covered by the DSL.

License

Copyright 2013 Elasticsearch

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elasticsearch-dsl-0.0.2.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

elasticsearch_dsl-0.0.2-py2.py3-none-any.whl (18.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file elasticsearch-dsl-0.0.2.tar.gz.

File metadata

File hashes

Hashes for elasticsearch-dsl-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6ad6ff946323027b255debcb66e3e035e545d5f8427bb5638b62e3b246d4d88a
MD5 83fc58e53a0a1a55b65b5367225c865f
BLAKE2b-256 a83ff0eeae1377e0aa99c0d0ea1e98580a7efd4978dccc39e0ddb97f1f4affd0

See more details on using hashes here.

File details

Details for the file elasticsearch_dsl-0.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for elasticsearch_dsl-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8c465c9649d5de3e17b46385722a490511110b603ca3c13ad9c77132a467c42c
MD5 771827205fa80ee6ca9fa84082234fda
BLAKE2b-256 cab8a61902cf7ee13e4f726ac95bd5646ebf5bca91588c7bd867ac1e6aef15d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page