Following instructions when migrating from elasticsearch-py I do the following:
body = {...}
s = Search.from_dict(body)
# Add some filters and aggregations...
a = A('cardinality', field='person_id')
s.aggs.bucket('distinct_person', a)
s.execute() # here it fails
Exception:
KeyError: "There is no connection with alias 'default'."
Where should I specify my alias, because even if I put it like this:
s = Search(using='custom_connection_alias').from_dict(body)
I get the same error about no connection with alias default. It looks like my custom_connection_alias get lost somewhere.
The default alias is fine. The problem is that you haven't configured any cluster connections. The easiest way is to just call:
from elasticsearch_dsl.connections import connections
connections.create_connection(...) # pass the exact same arguments here that you would pass into Elasticsearch()
You can read more in the configurations chapter - http://elasticsearch-dsl.readthedocs.io/en/latest/configuration.html
OK, it looks like I don't need custom alias.
Even this doesn't work:
client = Elasticsearch()
s = Search(using=client, index='contactposition_current', doc_type='modelresult')
s.execute() # This test does work and returns all documents from this index
body = {...} # complicated query
s.from_dict(body)
s._index # The result is fine: ['contactposition_current']
s.to_dict() # {'query': {'match_all': {}}} (Why does my complicated query change to this?)
from_dict is a class method that returns a new Search object, it doesn't mutate the current one, for that you would have to use update_from_dict
Thanks, I overlooked the update_from_dict. Works like a charm.
Most helpful comment
The
defaultalias is fine. The problem is that you haven't configured any cluster connections. The easiest way is to just call:You can read more in the configurations chapter - http://elasticsearch-dsl.readthedocs.io/en/latest/configuration.html