2013-10-10 13:20:42 +02:00
.. _helpers:
2013-09-30 18:59:27 +02:00
Helpers
=======
Collection of simple helper functions that abstract some specifics or the raw
API.
2015-10-11 23:45:09 +02:00
Bulk helpers
------------
2017-11-28 10:12:14 -08:00
There are several helpers for the ``bulk`` API since its requirement for
2015-10-11 23:45:09 +02:00
specific formatting and other considerations can make it cumbersome if used directly.
All bulk helpers accept an instance of ``Elasticsearch`` class and an iterable
``actions`` (any iterable, can also be a generator, which is ideal in most
cases since it will allow you to index large datasets without the need of
loading them into memory).
The items in the ``action`` iterable should be the documents we wish to index
in several formats. The most common one is the same as returned by
:meth: `~elasticsearch.Elasticsearch.search` , for example:
.. code :: python
{
'_index' : 'index-name' ,
'_type' : 'document' ,
'_id' : 42 ,
2018-03-31 01:11:53 +05:30
'_routing' : 5 ,
2017-04-17 22:05:32 +02:00
'pipeline' : 'my-ingest-pipeline' ,
2015-10-11 23:45:09 +02:00
'_source' : {
"title" : "Hello World!" ,
"body" : "..."
}
}
Alternatively, if `_source` is not present, it will pop all metadata fields
from the doc and use the rest as the document data:
.. code :: python
{
"_id" : 42 ,
2018-03-31 01:11:53 +05:30
"_routing" : 5 ,
2015-10-11 23:45:09 +02:00
"title" : "Hello World!" ,
"body" : "..."
}
The :meth: `~elasticsearch.Elasticsearch.bulk` api accepts ``index`` , ``create`` ,
``delete`` , and ``update`` actions. Use the ``_op_type`` field to specify an
action (``_op_type`` defaults to ``index`` ):
.. code :: python
{
'_op_type' : 'delete' ,
'_index' : 'index-name' ,
'_type' : 'document' ,
'_id' : 42 ,
}
{
'_op_type' : 'update' ,
'_index' : 'index-name' ,
'_type' : 'document' ,
'_id' : 42 ,
'doc' : { 'question' : 'The life, universe and everything.' }
}
2018-07-10 11:12:46 -06:00
Example:
~~~~~~~~
Lets say we have an iterable of data. Lets say a list of words called ``mywords``
and we want to index those words into individual documents where the structure of the
document is like ``{"word": "<myword>"}`` .
.. code :: python
def gendata ():
mywords = [ 'foo' , 'bar' , 'baz' ]
for word in mywords :
yield {
"_index" : "mywords" ,
2018-08-06 14:28:56 -05:00
"_type" : "document" ,
"doc" : { "word" : word },
2018-07-10 11:12:46 -06:00
}
2018-08-06 14:28:56 -05:00
bulk ( es , gendata ())
2018-07-10 11:12:46 -06:00
For a more complete and complex example please take a look at
https://github.com/elastic/elasticsearch-py/blob/master/example/load.py#L76-L130
2019-05-31 15:25:03 +01:00
The :meth: `~elasticsearch.Elasticsearch.parallel_bulk` api is a wrapper around the :meth: `~elasticsearch.Elasticsearch.bulk` api to provide threading. :meth: `~elasticsearch.Elasticsearch.parallel_bulk` returns a generator which must be consumed to produce results.
To see the results use:
.. code :: python
for success , info in parallel_bulk ( ... ):
if not success :
print ( 'A document failed:' , info )
If you don't care about the results, you can use deque from collections:
.. code :: python
from collections import deque
deque ( parallel_bulk ( ... ), maxlen = 0 )
2015-10-11 23:45:09 +02:00
.. note ::
When reading raw json strings from a file, you can also pass them in
directly (without decoding to dicts first). In that case, however, you lose
the ability to specify anything (index, type, even id) on a per-record
basis, all documents will just be sent to elasticsearch to be indexed
as-is.
2013-09-30 18:59:27 +02:00
.. py:module :: elasticsearch.helpers
2013-11-22 14:38:38 +01:00
.. autofunction :: streaming_bulk
2015-10-11 23:45:09 +02:00
.. autofunction :: parallel_bulk
2013-11-22 14:38:38 +01:00
.. autofunction :: bulk
2013-09-30 18:59:27 +02:00
2015-10-11 23:45:09 +02:00
Scan
----
2013-09-30 18:59:27 +02:00
.. autofunction :: scan
2015-10-11 23:45:09 +02:00
Reindex
-------
2013-09-30 18:59:27 +02:00
.. autofunction :: reindex