@@ -27,7 +27,7 @@ Features
27
27
28
28
- Django >= 1.10
29
29
- Python 2.7, 3.5, 3.6, 3.7
30
- - Elasticsearch >= 2 .0 < 7.0
30
+ - Elasticsearch >= 6 .0 < 7.0
31
31
32
32
.. _Search : http://elasticsearch-dsl.readthedocs.io/en/stable/search_dsl.html
33
33
@@ -39,13 +39,8 @@ Install Django Elasticsearch DSL::
39
39
pip install django-elasticsearch-dsl
40
40
41
41
# Elasticsearch 6.x
42
- pip install 'elasticsearch-dsl>=6.0,<6.2 '
42
+ pip install 'elasticsearch-dsl>=6.3. 0,<7.0 '
43
43
44
- # Elasticsearch 5.x
45
- pip install 'elasticsearch-dsl>=5.0,<6.0'
46
-
47
- # Elasticsearch 2.x
48
- pip install 'elasticsearch-dsl>=2.1,<3.0'
49
44
50
45
Then add ``django_elasticsearch_dsl `` to the INSTALLED_APPS
51
46
@@ -81,30 +76,31 @@ Then for a model:
81
76
(4 , " SUV" ),
82
77
])
83
78
84
- To make this model work with Elasticsearch, create a subclass of ``django_elasticsearch_dsl.DocType ``
85
- and create a ``django_elasticsearch_dsl.Index `` to define your Elasticsearch indices, names, and settings. This classes must be
86
- defined in a ``documents.py `` file.
79
+ To make this model work with Elasticsearch, create a subclass of ``django_elasticsearch_dsl.Document ``,
80
+ create a ``class Index `` inside the ``Document `` class
81
+ to define your Elasticsearch indices, names, settings etc and at last register the class using
82
+ ``registry.register_document `` decorator.
87
83
88
84
.. code-block :: python
89
85
90
86
# documents.py
91
87
92
- from django_elasticsearch_dsl import DocType, Index
88
+ from django_elasticsearch_dsl import Document
89
+ from django_elasticsearch_dsl.registries import registry
93
90
from .models import Car
94
91
95
- # Name of the Elasticsearch index
96
- car = Index(' cars' )
97
- # See Elasticsearch Indices API reference for available settings
98
- car.settings(
99
- number_of_shards = 1 ,
100
- number_of_replicas = 0
101
- )
102
92
93
+ @registry.register_document
94
+ class CarDocument (Document ):
95
+ class Index :
96
+ # Name of the Elasticsearch index
97
+ name = ' cars'
98
+ # See Elasticsearch Indices API reference for available settings
99
+ settings = {' number_of_shards' : 1 ,
100
+ ' number_of_replicas' : 0 }
103
101
104
- @car.doc_type
105
- class CarDocument (DocType ):
106
- class Meta :
107
- model = Car # The model associated with this DocType
102
+ class Django :
103
+ model = Car # The model associated with this Document
108
104
109
105
# The fields of the model you want to be indexed in Elasticsearch
110
106
fields = [
@@ -205,25 +201,25 @@ the model to a string, so we'll just add a method for it:
205
201
else :
206
202
return " SUV"
207
203
208
- Now we need to tell our ``DocType `` subclass to use that method instead of just
204
+ Now we need to tell our ``Document `` subclass to use that method instead of just
209
205
accessing the ``type `` field on the model directly. Change the CarDocument to look
210
206
like this:
211
207
212
208
.. code-block :: python
213
209
214
210
# documents.py
215
211
216
- from django_elasticsearch_dsl import DocType , fields
212
+ from django_elasticsearch_dsl import Document , fields
217
213
218
214
# ... #
219
215
220
- @car.doc_type
221
- class CarDocument (DocType ):
216
+ @registry.register_document
217
+ class CarDocument (Document ):
222
218
# add a string field to the Elasticsearch mapping called type, the
223
219
# value of which is derived from the model's type_to_string attribute
224
220
type = fields.TextField(attr = " type_to_string" )
225
221
226
- class Meta :
222
+ class Django :
227
223
model = Car
228
224
# we removed the type field from here
229
225
fields = [
@@ -240,7 +236,7 @@ Using prepare_field
240
236
~~~~~~~~~~~~~~~~~~~
241
237
242
238
Sometimes, you need to do some extra prepping before a field should be saved to
243
- Elasticsearch. You can add a ``prepare_foo(self, instance) `` method to a DocType
239
+ Elasticsearch. You can add a ``prepare_foo(self, instance) `` method to a Document
244
240
(where foo is the name of the field), and that will be called when the field
245
241
needs to be saved.
246
242
@@ -250,7 +246,7 @@ needs to be saved.
250
246
251
247
# ... #
252
248
253
- class CarDocument (DocType ):
249
+ class CarDocument (Document ):
254
250
# ... #
255
251
256
252
foo = TextField()
@@ -292,18 +288,11 @@ You can use an ObjectField or a NestedField.
292
288
293
289
# documents.py
294
290
295
- from django_elasticsearch_dsl import DocType, Index , fields
291
+ from django_elasticsearch_dsl import Document , fields
296
292
from .models import Car, Manufacturer, Ad
297
293
298
- car = Index(' cars' )
299
- car.settings(
300
- number_of_shards = 1 ,
301
- number_of_replicas = 0
302
- )
303
-
304
-
305
- @car.doc_type
306
- class CarDocument (DocType ):
294
+ @registry.register_document
295
+ class CarDocument (Document ):
307
296
manufacturer = fields.ObjectField(properties = {
308
297
' name' : fields.TextField(),
309
298
' country_code' : fields.TextField(),
@@ -314,7 +303,10 @@ You can use an ObjectField or a NestedField.
314
303
' pk' : fields.IntegerField(),
315
304
})
316
305
317
- class Meta :
306
+ class Index :
307
+ name = ' cars'
308
+
309
+ class Django :
318
310
model = Car
319
311
fields = [
320
312
' name' ,
@@ -367,14 +359,14 @@ So for example you can use a custom analyzer_:
367
359
char_filter = [" html_strip" ]
368
360
)
369
361
370
- @car.doc_type
371
- class CarDocument (DocType ):
362
+ @registry.register_document
363
+ class CarDocument (Document ):
372
364
description = fields.TextField(
373
365
analyzer = html_strip,
374
366
fields = {' raw' : fields.KeywordField()}
375
367
)
376
368
377
- class Meta :
369
+ class Django :
378
370
model = Car
379
371
fields = [
380
372
' name' ,
@@ -417,20 +409,19 @@ instance.
417
409
418
410
Index
419
411
-----
420
-
421
- To define an Elasticsearch index you must instantiate a ``django_elasticsearch_dsl.Index `` class and set the name
422
- and settings of the index. This class inherits from elasticsearch-dsl-py Index _.
423
- After you instantiate your class, you need to associate it with the DocType you
424
- want to put in this Elasticsearch index.
412
+ In typical scenario using `class Index ` on a `Document ` class is sufficient to perform any action.
413
+ In a few cases though it can be useful to manipulate an Index object directly.
414
+ To define an Elasticsearch index you must instantiate a ``elasticsearch_dsl.Index `` class and set the name
415
+ and settings of the index.
416
+ After you instantiate your class, you need to associate it with the Document you
417
+ want to put in this Elasticsearch index and also add the `registry.register_document ` decorator.
425
418
426
419
427
- .. _Index : http://elasticsearch-dsl.readthedocs.io/en/stable/persistence.html#index
428
-
429
420
.. code-block :: python
430
421
431
422
# documents.py
432
-
433
- from django_elasticsearch_dsl import DocType, Index
423
+ from elasticsearch_dsl import Index
424
+ from django_elasticsearch_dsl import Document
434
425
from .models import Car, Manufacturer
435
426
436
427
# The name of your index
@@ -441,33 +432,35 @@ want to put in this Elasticsearch index.
441
432
number_of_replicas = 0
442
433
)
443
434
444
-
445
- @car.doc_type
446
- class CarDocument (DocType ):
447
- class Meta :
435
+ @registry.register_document
436
+ @car.document
437
+ class CarDocument (Document ):
438
+ class Django :
448
439
model = Car
449
440
fields = [
450
441
' name' ,
451
442
' color' ,
452
443
]
453
444
454
- @car.doc_type
455
- class ManufacturerDocument (DocType ):
456
- class Meta :
445
+ @registry.register_document
446
+ class ManufacturerDocument (Document ):
447
+ class Index :
448
+ name = ' manufacture'
449
+ settings = {' number_of_shards' : 1 ,
450
+ ' number_of_replicas' : 0 }
451
+
452
+ class Django :
457
453
model = Car
458
454
fields = [
459
- ' name' , # If a field as the same name in multiple DocType of
460
- # the same Index, the field type must be identical
461
- # (here fields.TextField)
455
+ ' name' ,
462
456
' country_code' ,
463
457
]
464
458
465
459
When you execute the command::
466
460
467
461
$ ./manage.py search_index --rebuild
468
462
469
- This will create an index named ``cars `` in Elasticsearch with two mappings:
470
- ``manufacturer_document `` and ``car_document ``.
463
+ This will create two index named ``cars `` and ``manufacture `` in Elasticsearch with appropriate mapping.
471
464
472
465
473
466
Management Commands
565
558
- Add support for --using (use another Elasticsearch cluster) in management commands.
566
559
- Add management commands for mapping level operations (like update_mapping....).
567
560
- Dedicated documentation.
568
- - Generate ObjectField/NestField properties from a DocType class.
561
+ - Generate ObjectField/NestField properties from a Document class.
569
562
- More examples.
570
563
- Better ``ESTestCase `` and documentation for testing
0 commit comments