Skip to content

Commit 7bf5ea3

Browse files
barseghyanartursafwanrahman
authored andcommitted
Backwards compatibility with Elasticsearch 6.x (#288)
* ES6 compatibility * Clean up * Clean up
1 parent 68b342a commit 7bf5ea3

File tree

9 files changed

+102
-36
lines changed

9 files changed

+102
-36
lines changed

.travis.yml

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,69 @@ dist: xenial # default "precise" distro doesn't include Java 8 for Elasticsearch
77

88
matrix:
99
include:
10-
- env: TOX_ENV=py36-django-111-es6
10+
- env: TOX_ENV=py36-django-111-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
1111
python: 3.6
12-
- env: TOX_ENV=py37-django-111-es6
12+
- env: TOX_ENV=py36-django-111-es7
13+
python: 3.6
14+
15+
- env: TOX_ENV=py37-django-111-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
1316
python: 3.7
14-
- env: TOX_ENV=py38-django-111-es6
17+
- env: TOX_ENV=py37-django-111-es7
18+
python: 3.7
19+
20+
- env: TOX_ENV=py38-django-111-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
1521
python: 3.8
16-
- env: TOX_ENV=py27-django-111-es6
22+
- env: TOX_ENV=py38-django-111-es7
23+
python: 3.8
24+
25+
- env: TOX_ENV=py27-django-111-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
26+
python: 2.7
27+
- env: TOX_ENV=py27-django-111-es7
1728
python: 2.7
18-
- env: TOX_ENV=py36-django-2-es6
29+
30+
- env: TOX_ENV=py36-django-2-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
31+
python: 3.6
32+
- env: TOX_ENV=py36-django-2-es7
1933
python: 3.6
20-
- env: TOX_ENV=py37-django-2-es6
34+
35+
- env: TOX_ENV=py37-django-2-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
2136
python: 3.7
22-
- env: TOX_ENV=py37-django-21-es6
37+
- env: TOX_ENV=py37-django-2-es7
2338
python: 3.7
24-
- env: TOX_ENV=py37-django-22-es6
39+
40+
- env: TOX_ENV=py37-django-21-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
41+
python: 3.7
42+
- env: TOX_ENV=py37-django-21-es7
2543
python: 3.7
26-
- env: TOX_ENV=py37-django-30-es6
44+
45+
- env: TOX_ENV=py37-django-22-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
2746
python: 3.7
28-
- env: TOX_ENV=py38-django-2-es6
47+
- env: TOX_ENV=py37-django-22-es7
48+
python: 3.7
49+
50+
- env: TOX_ENV=py37-django-30-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
51+
python: 3.7
52+
- env: TOX_ENV=py37-django-30-es7
53+
python: 3.7
54+
55+
- env: TOX_ENV=py38-django-2-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
56+
python: 3.8
57+
- env: TOX_ENV=py38-django-2-es7
2958
python: 3.8
30-
- env: TOX_ENV=py38-django-21-es6
59+
60+
- env: TOX_ENV=py38-django-21-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
61+
python: 3.8
62+
- env: TOX_ENV=py38-django-21-es7
63+
python: 3.8
64+
65+
- env: TOX_ENV=py38-django-22-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
66+
python: 3.8
67+
- env: TOX_ENV=py38-django-22-es7
3168
python: 3.8
32-
- env: TOX_ENV=py38-django-22-es6
69+
70+
- env: TOX_ENV=py38-django-30-es6 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt
3371
python: 3.8
34-
- env: TOX_ENV=py38-django-30-es6
72+
- env: TOX_ENV=py38-django-30-es7
3573
python: 3.8
3674

3775
cache: pip

django_elasticsearch_dsl/documents.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
TextField,
2424
)
2525
from .search import Search
26+
from .versions import ES_MAJOR_VERSION
2627

2728
model_field_class_to_field_class = {
2829
models.AutoField: IntegerField,
@@ -166,14 +167,17 @@ def generate_id(cls, object_instance):
166167
return object_instance.pk
167168

168169
def _prepare_action(self, object_instance, action):
169-
return {
170+
_val = {
170171
'_op_type': action,
171172
'_index': self._index._name,
172173
'_id': self.generate_id(object_instance),
173174
'_source': (
174175
self.prepare(object_instance) if action != 'delete' else None
175176
),
176177
}
178+
if ES_MAJOR_VERSION < 7:
179+
_val['_type'] = self._doc_type.name
180+
return _val
177181

178182
def _get_actions(self, object_list, action):
179183
for object_instance in object_list:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from elasticsearch_dsl import VERSION
2+
3+
ES_MAJOR_VERSION = VERSION[0]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
],
4343
include_package_data=True,
4444
install_requires=[
45-
'elasticsearch-dsl>=7.0.0<8.0.0',
45+
'elasticsearch-dsl>=6.4.0<8.0.0',
4646
'six',
4747
],
4848
license="Apache Software License 2.0",

tests/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
from elasticsearch_dsl import VERSION
2-
3-
ES_MAJOR_VERSION = VERSION[0]

tests/test_documents.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django_elasticsearch_dsl.exceptions import (ModelFieldNotMappedError,
1212
RedeclaredFieldError)
1313
from django_elasticsearch_dsl.registries import registry
14-
from tests import ES_MAJOR_VERSION
14+
from django_elasticsearch_dsl.versions import ES_MAJOR_VERSION
1515

1616
from .models import Article
1717
from .documents import ArticleDocument, ArticleWithSlugAsIdDocument
@@ -62,6 +62,8 @@ class Index:
6262

6363
class DocTypeTestCase(TestCase):
6464

65+
maxDiff = None
66+
6567
def test_model_class_added(self):
6668
self.assertEqual(CarDocument.django.model, Car)
6769

@@ -136,8 +138,12 @@ def test_to_field_with_unknown_field(self):
136138
def test_mapping(self):
137139
text_type = 'string' if ES_MAJOR_VERSION == 2 else 'text'
138140

139-
self.assertEqual(
140-
CarDocument._doc_type.mapping.to_dict(), {
141+
doc_type_mapping = CarDocument._doc_type.mapping.to_dict()
142+
if ES_MAJOR_VERSION < 7:
143+
doc_type_mapping = doc_type_mapping['car_document']
144+
145+
self.assertDictEqual(
146+
doc_type_mapping, {
141147
'properties': {
142148
'name': {
143149
'type': text_type
@@ -164,7 +170,7 @@ def test_prepare(self):
164170
car = Car(name="Type 57", price=5400000.0, not_indexed="not_indexex")
165171
doc = CarDocument()
166172
prepared_data = doc.prepare(car)
167-
self.assertEqual(
173+
self.assertDictEqual(
168174
prepared_data, {
169175
'color': doc.prepare_color(None),
170176
'type': car.type(),
@@ -188,7 +194,7 @@ class Index:
188194
car = Car(name="Type 57", price=5400000.0, not_indexed="not_indexex")
189195
doc = CarDocumentDSlBaseField()
190196
prepared_data = doc.prepare(car)
191-
self.assertEqual(
197+
self.assertDictEqual(
192198
prepared_data, {
193199
'name': car.name,
194200
'price': car.price
@@ -212,9 +218,13 @@ def test_model_instance_update(self):
212218
},
213219
'_index': 'car_index',
214220
}]
221+
if ES_MAJOR_VERSION < 7:
222+
actions[0]['_type'] = 'car_document'
223+
215224
self.assertEqual(1, mock.call_count)
216225
self.assertEqual(
217-
actions, list(mock.call_args_list[0][1]['actions'])
226+
actions, list(mock.call_args_list[0][1]['actions']),
227+
str(list(mock.call_args_list[0][1]['actions']))
218228
)
219229
self.assertTrue(mock.call_args_list[0][1]['refresh'])
220230
self.assertEqual(
@@ -251,8 +261,11 @@ def test_model_instance_iterable_update(self):
251261
},
252262
'_index': 'car_index'
253263
}]
264+
if ES_MAJOR_VERSION < 7:
265+
actions[0]['_type'] = 'car_document'
266+
actions[1]['_type'] = 'car_document'
254267
self.assertEqual(1, mock.call_count)
255-
self.assertEqual(
268+
self.assertListEqual(
256269
actions, list(mock.call_args_list[0][1]['actions'])
257270
)
258271
self.assertTrue(mock.call_args_list[0][1]['refresh'])

tests/test_fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ListField, LongField,
1414
NestedField, ObjectField, ShortField, TextField
1515
)
16-
from tests import ES_MAJOR_VERSION
16+
from django_elasticsearch_dsl.versions import ES_MAJOR_VERSION
1717

1818

1919
class DEDFieldTestCase(TestCase):

tests/test_integration.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from elasticsearch.exceptions import NotFoundError
1111
from elasticsearch_dsl import Index as DSLIndex
1212
from django_elasticsearch_dsl.test import ESTestCase
13-
from tests import ES_MAJOR_VERSION
13+
from django_elasticsearch_dsl.versions import ES_MAJOR_VERSION
1414

1515
from .documents import (
1616
ad_index,
@@ -31,6 +31,9 @@
3131
"--elasticsearch not set"
3232
)
3333
class IntegrationTestCase(ESTestCase, TestCase):
34+
35+
maxDiff = None
36+
3437
def setUp(self):
3538
super(IntegrationTestCase, self).setUp()
3639
self.manufacturer = Manufacturer(
@@ -103,7 +106,7 @@ def test_get_doc_with_reverse_relationships(self):
103106
result = s.execute()
104107
self.assertEqual(len(result), 1)
105108
car1_doc = result[0]
106-
self.assertEqual(car1_doc.ads, [
109+
self.assertListEqual(list(car1_doc.ads), [
107110
{
108111
'title': self.ad1.title,
109112
'description': self.ad1.description,
@@ -123,7 +126,7 @@ def test_get_doc_with_many_to_many_relationships(self):
123126
result = s.execute()
124127
self.assertEqual(len(result), 1)
125128
car1_doc = result[0]
126-
self.assertEqual(car1_doc.categories, [
129+
self.assertListEqual(list(car1_doc.categories), [
127130
{
128131
'title': self.category1.title,
129132
'slug': self.category1.slug,
@@ -141,7 +144,7 @@ def test_doc_to_dict(self):
141144
result = s.execute()
142145
self.assertEqual(len(result), 1)
143146
car2_doc = result[0]
144-
self.assertEqual(car2_doc.to_dict(), {
147+
self.assertDictEqual(car2_doc.to_dict(), {
145148
'type': self.car2.type,
146149
'launched': self.car2.launched,
147150
'name': self.car2.name,
@@ -160,7 +163,7 @@ def test_doc_to_dict(self):
160163
result = s.execute()
161164
self.assertEqual(len(result), 1)
162165
car3_doc = result[0]
163-
self.assertEqual(car3_doc.to_dict(), {
166+
self.assertDictEqual(car3_doc.to_dict(), {
164167
'type': self.car3.type,
165168
'launched': self.car3.launched,
166169
'name': self.car3.name,
@@ -188,7 +191,7 @@ def test_index_to_dict(self):
188191

189192
index_dict = test_index.to_dict()
190193

191-
self.assertEqual(index_dict['settings'], {
194+
self.assertDictEqual(index_dict['settings'], {
192195
'number_of_shards': 1,
193196
'number_of_replicas': 0,
194197
'analysis': {
@@ -203,7 +206,14 @@ def test_index_to_dict(self):
203206
}
204207
}
205208
})
206-
self.assertEqual(index_dict['mappings'], {
209+
210+
index_dict_mappings = index_dict['mappings']
211+
if ES_MAJOR_VERSION < 7:
212+
index_dict_mappings = index_dict_mappings['doc']
213+
214+
self.assertDictEqual(
215+
index_dict_mappings,
216+
{
207217
'properties': {
208218
'ads': {
209219
'type': 'nested',
@@ -235,7 +245,8 @@ def test_index_to_dict(self):
235245
'launched': {'type': 'date'},
236246
'type': {'type': text_type}
237247
}
238-
})
248+
}
249+
)
239250

240251
def test_related_docs_are_updated(self):
241252
# test foreignkey relation

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tox]
22
envlist =
3-
py27-django-111-es7
4-
{py36,py37,py38}-django-{111,2,21,22,30}-{es7}
3+
py27-django-111-es{6,7}
4+
{py36,py37,py38}-django-{111,2,21,22,30}-es{6,7}
55

66
[testenv]
77
setenv =

0 commit comments

Comments
 (0)