Skip to content

Commit b351110

Browse files
committed
Some doc, examples and formating fix
1 parent a252208 commit b351110

File tree

7 files changed

+71
-21
lines changed

7 files changed

+71
-21
lines changed

HISTORY.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
History
44
-------
55

6+
0.5.0 (2018-04-22)
7+
~~~~~~~~~~~~~~~~~~
8+
* Add Support for Elasticsearch 6 thanks to HansAdema
9+
10+
Breaking Change:
11+
~~~~~~~~~~~~~~~~
12+
Django string fields now point to ES text field by default.
13+
Nothing should change for ES 2.X but if you are using ES 5.X,
14+
you may need to rebuild and/or update some of your documents.
15+
16+
617
0.4.5 (2018-04-22)
718
~~~~~~~~~~~~~~~~~~
819
* Fix prepare with related models when deleted (See PR #99)

README.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ like this:
220220
class CarDocument(DocType):
221221
# add a string field to the Elasticsearch mapping called type, the
222222
# value of which is derived from the model's type_to_string attribute
223-
type = fields.StringField(attr="type_to_string")
223+
type = fields.TextField(attr="type_to_string")
224224
225225
class Meta:
226226
model = Car
@@ -252,7 +252,7 @@ needs to be saved.
252252
class CarDocument(DocType):
253253
# ... #
254254
255-
foo = StringField()
255+
foo = TextField()
256256
257257
def prepare_foo(self, instance):
258258
return " ".join(instance.foos)
@@ -304,12 +304,12 @@ You can use an ObjectField or a NestedField.
304304
@car.doc_type
305305
class CarDocument(DocType):
306306
manufacturer = fields.ObjectField(properties={
307-
'name': fields.StringField(),
308-
'country_code': fields.StringField(),
307+
'name': fields.TextField(),
308+
'country_code': fields.TextField(),
309309
})
310310
ads = fields.NestedField(properties={
311-
'description': fields.StringField(analyzer=html_strip),
312-
'title': fields.StringField(),
311+
'description': fields.TextField(analyzer=html_strip),
312+
'title': fields.TextField(),
313313
'pk': fields.IntegerField(),
314314
})
315315
@@ -368,9 +368,9 @@ So for example you can use a custom analyzer_:
368368
369369
@car.doc_type
370370
class CarDocument(DocType):
371-
description = fields.StringField(
371+
description = fields.TextField(
372372
analyzer=html_strip,
373-
fields={'raw': fields.StringField(index='not_analyzed')}
373+
fields={'raw': fields.KeywordField()}
374374
)
375375
376376
class Meta:
@@ -405,7 +405,7 @@ Available Fields
405405
- ObjectField(properties, attr=None, \*\*elasticsearch_properties)
406406
- NestedField(properties, attr=None, \*\*elasticsearch_properties)
407407

408-
- Elasticsearch 5 Fields
408+
- Elasticsearch >=5 Fields
409409

410410
- TextField(attr=None, \*\*elasticsearch_properties)
411411
- KeywordField(attr=None, \*\*elasticsearch_properties)
@@ -457,7 +457,7 @@ want to put in this Elasticsearch index.
457457
fields = [
458458
'name', # If a field as the same name in multiple DocType of
459459
# the same Index, the field type must be identical
460-
# (here fields.StringField)
460+
# (here fields.TextField)
461461
'country_code',
462462
]
463463

django_elasticsearch_dsl/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def get_value_from_instance(self, instance, field_value_to_ignore=None):
207207

208208
if isinstance(_file, FieldFile):
209209
return _file.url if _file else ''
210-
return _file
210+
return _file if _file else ''
211211

212212

213213
# ES5+ has text types Keyword and Text, ES2 has type String.

example/example/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# SECURITY WARNING: don't run with debug turned on in production!
2323
DEBUG = True
2424

25-
ALLOWED_HOSTS = ['*']
25+
ALLOWED_HOSTS = []
2626

2727
# Application definition
2828

@@ -84,7 +84,7 @@
8484

8585
ELASTICSEARCH_DSL = {
8686
'default': {
87-
'hosts': '192.168.99.100:9200'
87+
'hosts': 'localhost:9200'
8888
},
8989
}
9090

example/test_app/documents.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
@car.doc_type
2323
class CarDocument(DocType):
2424
manufacturer = fields.ObjectField(properties={
25-
'name': fields.StringField(),
26-
'country': fields.StringField(),
25+
'name': fields.TextField(),
26+
'country': fields.TextField(),
2727
'logo': fields.FileField(),
2828
})
2929

3030
ads = fields.NestedField(properties={
31-
'description': fields.StringField(analyzer=html_strip),
32-
'title': fields.StringField(),
31+
'description': fields.TextField(analyzer=html_strip),
32+
'title': fields.TextField(),
3333
'pk': fields.IntegerField(),
3434
})
3535

3636
categories = fields.NestedField(properties={
37-
'title': fields.StringField(),
37+
'title': fields.TextField(),
3838
})
3939

4040
class Meta:
@@ -73,6 +73,46 @@ class Meta:
7373
]
7474

7575

76+
class CarWithPrepareDocument(DocType):
77+
manufacturer = fields.ObjectField(properties={
78+
'name': fields.TextField(),
79+
'country': fields.TextField(),
80+
})
81+
82+
manufacturer_short = fields.ObjectField(properties={
83+
'name': fields.TextField(),
84+
})
85+
86+
class Meta:
87+
model = Car
88+
related_models = [Manufacturer]
89+
index = 'car_with_prepare_index'
90+
fields = [
91+
'name',
92+
'launched',
93+
'type',
94+
]
95+
96+
def prepare_manufacturer_with_related(self, car, related_to_ignore):
97+
if (car.manufacturer is not None and car.manufacturer !=
98+
related_to_ignore):
99+
return {
100+
'name': car.manufacturer.name,
101+
'country': car.manufacturer.country(),
102+
}
103+
return {}
104+
105+
def prepare_manufacturer_short(self, car):
106+
if car.manufacturer is not None:
107+
return {
108+
'name': car.manufacturer.name,
109+
}
110+
return {}
111+
112+
def get_instances_from_related(self, related_instance):
113+
return related_instance.car_set.all()
114+
115+
76116
class AdDocument(DocType):
77117
description = fields.TextField(
78118
analyzer=html_strip,
@@ -96,7 +136,7 @@ def __init__(self, *args, **kwargs):
96136

97137
class Meta:
98138
model = Ad
99-
index = 'test_ads'
139+
index = 'test_ads2'
100140
fields = [
101141
'title',
102142
]

requirements_dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ bumpversion==0.5.3
22
wheel==0.29.0
33
django>=2.0,<2.1
44
elasticsearch-dsl>=2.1.0,<7.0.0
5-
5+
-e .

tests/documents.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ def get_instances_from_related(self, related_instance):
123123
return related_instance.car_set.all()
124124

125125

126-
127126
ad_index = Index('test_ads').settings(**index_settings)
128127

129128

0 commit comments

Comments
 (0)