Skip to content

Commit

Permalink
Merge branch 'master' of github.com:elastic/elasticsearch-dsl-py
Browse files Browse the repository at this point in the history
  • Loading branch information
honzakral committed May 28, 2019
2 parents 6450153 + e77ea75 commit 65f2a18
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Compatibility
The library is compatible with all Elasticsearch versions since ``2.x`` but you
**have to use a matching major version**:

For **Elasticsearch 6.0** and later, use the major version 7 (``7.x.y``) of the
For **Elasticsearch 7.0** and later, use the major version 7 (``7.x.y``) of the
library.

For **Elasticsearch 6.0** and later, use the major version 6 (``6.x.y``) of the
Expand Down
2 changes: 1 addition & 1 deletion docs/search_dsl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ If you need to limit the fields being returned by elasticsearch, use the
# don't return any fields, just the metadata
s = s.source(False)
# explicitly include/exclude fields
s = s.source(include=["title"], exclude=["user.*"])
s = s.source(includes=["title"], excludes=["user.*"])
# reset the field selection
s = s.source(None)
Expand Down
13 changes: 12 additions & 1 deletion docs/update_by_query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ explicitly:
print(ubq.to_dict())
Also, to use variables in script see below example:

.. code:: python
ubq.script(
source="ctx._source.messages.removeIf(x -> x.somefield == params.some_var)",
params={
'some_var': 'some_string_val'
}
)
Serialization and Deserialization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -94,7 +105,7 @@ the data from the dict:
ubq = UpdateByQuery.from_dict({"query": {"match": {"title": "python"}}})
If you wish to modify an existing ``Update By Query`` object, overriding it'ubq
If you wish to modify an existing ``Update By Query`` object, overriding it's
properties, instead use the ``update_from_dict`` method that alters an instance
**in-place**:

Expand Down
10 changes: 8 additions & 2 deletions elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def get(cls, id, using=None, index=None, **kwargs):
"""
Retrieve a single document from elasticsearch using it's ``id``.
:arg id: ``id`` of the document to be retireved
:arg id: ``id`` of the document to be retrieved
:arg index: elasticsearch index to use, if the ``Document`` is
associated with an index this can be omitted.
:arg using: connection alias to use, defaults to ``'default'``
Expand All @@ -195,7 +195,7 @@ def mget(cls, docs, using=None, index=None, raise_on_error=True,
Retrieve multiple document by their ``id``\s. Returns a list of instances
in the same order as requested.
:arg docs: list of ``id``\s of the documents to be retireved or a list
:arg docs: list of ``id``\s of the documents to be retrieved or a list
of document specifications as per
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html
:arg index: elasticsearch index to use, if the ``Document`` is
Expand Down Expand Up @@ -274,6 +274,12 @@ def delete(self, using=None, index=None, **kwargs):
for k in DOC_META_FIELDS
if k in self.meta
}

# Optimistic concurrency control
if 'seq_no' in self.meta and 'primary_term' in self.meta:
doc_meta['if_seq_no'] = self.meta['seq_no']
doc_meta['if_primary_term'] = self.meta['primary_term']

doc_meta.update(kwargs)
es.delete(
index=self._get_index(index),
Expand Down
4 changes: 2 additions & 2 deletions elasticsearch_dsl/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def analyze(self, using=None, **kwargs):

def refresh(self, using=None, **kwargs):
"""
Preforms a refresh operation on the index.
Performs a refresh operation on the index.
Any additional keyword arguments will be passed to
``Elasticsearch.indices.refresh`` unchanged.
Expand All @@ -327,7 +327,7 @@ def refresh(self, using=None, **kwargs):

def flush(self, using=None, **kwargs):
"""
Preforms a flush operation on the index.
Performs a flush operation on the index.
Any additional keyword arguments will be passed to
``Elasticsearch.indices.flush`` unchanged.
Expand Down
10 changes: 5 additions & 5 deletions elasticsearch_dsl/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def update_from_dict(self, d):
s.setdefault('text', text)
if 'script_fields' in d:
self._script_fields = d.pop('script_fields')
self._extra = d
self._extra.update(d)
return self

def script_fields(self, **kwargs):
Expand Down Expand Up @@ -478,19 +478,19 @@ def source(self, fields=None, **kwargs):
:arg fields: wildcard string, array of wildcards, or dictionary of includes and excludes
If ``fields`` is None, the entire document will be returned for
each hit. If fields is a dictionary with keys of 'include' and/or
'exclude' the fields will be either included or excluded appropriately.
each hit. If fields is a dictionary with keys of 'includes' and/or
'excludes' the fields will be either included or excluded appropriately.
Calling this multiple times with the same named parameter will override the
previous values with the new ones.
Example::
s = Search()
s = s.source(include=['obj1.*'], exclude=["*.description"])
s = s.source(includes=['obj1.*'], excludes=["*.description"])
s = Search()
s = s.source(include=['obj1.*']).source(exclude=["*.description"])
s = s.source(includes=['obj1.*']).source(excludes=["*.description"])
"""
s = self._clone()
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch_dsl/update_by_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def update_from_dict(self, d):
self.query._proxied = Q(d.pop('query'))
if 'script' in d:
self._script = d.pop('script')
self._extra = d
self._extra.update(d)
return self

def script(self, **kwargs):
Expand Down
7 changes: 7 additions & 0 deletions test_elasticsearch_dsl/test_integration/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ def test_save_automatically_uses_seq_no_and_primary_term(data_client):
with raises(ConflictError):
elasticsearch_repo.save()

def test_delete_automatically_uses_seq_no_and_primary_term(data_client):
elasticsearch_repo = Repository.get('elasticsearch-dsl-py')
elasticsearch_repo.meta.seq_no += 1

with raises(ConflictError):
elasticsearch_repo.delete()

def assert_doc_equals(expected, actual):
for f in expected:
assert f in actual
Expand Down
35 changes: 25 additions & 10 deletions test_elasticsearch_dsl/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,32 +421,32 @@ def test_source():

assert {
'_source': {
'include': ['foo.bar.*'],
'exclude': ['foo.one']
'includes': ['foo.bar.*'],
'excludes': ['foo.one']
}
} == search.Search().source(include=['foo.bar.*'], exclude=['foo.one']).to_dict()
} == search.Search().source(includes=['foo.bar.*'], excludes=['foo.one']).to_dict()

assert {
'_source': False
} == search.Search().source(False).to_dict()

assert {
'_source': ['f1', 'f2']
} == search.Search().source(include=['foo.bar.*'], exclude=['foo.one']).source(['f1', 'f2']).to_dict()
} == search.Search().source(includes=['foo.bar.*'], excludes=['foo.one']).source(['f1', 'f2']).to_dict()

def test_source_on_clone():
assert {
'_source': {
'include': ['foo.bar.*'],
'exclude': ['foo.one']
'includes': ['foo.bar.*'],
'excludes': ['foo.one']
},
'query': {
'bool': {
'filter': [{'term': {'title': 'python'}}],
}
}
} == search.Search().source(include=['foo.bar.*']).\
source(exclude=['foo.one']).\
} == search.Search().source(includes=['foo.bar.*']).\
source(excludes=['foo.one']).\
filter('term', title='python').to_dict()\

assert {'_source': False,
Expand All @@ -459,8 +459,8 @@ def test_source_on_clone():

def test_source_on_clear():
assert {
} == search.Search().source(include=['foo.bar.*']).\
source(include=None, exclude=None).to_dict()
} == search.Search().source(includes=['foo.bar.*']).\
source(includes=None, excludes=None).to_dict()

def test_suggest_accepts_global_text():
s = search.Search.from_dict({
Expand Down Expand Up @@ -530,3 +530,18 @@ def test_delete_by_query(mock_client):
index=None,
body={"query": {"match": {"lang": "java"}}}
)

def test_update_from_dict():
s = search.Search()
s.update_from_dict({"indices_boost": [{"important-documents": 2}]})
s.update_from_dict({"_source": ["id", "name"]})

assert {
'indices_boost': [{
'important-documents': 2
}],
'_source': [
'id',
'name'
]
} == s.to_dict()

0 comments on commit 65f2a18

Please sign in to comment.