-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add mappings and bulk to quickstart page #2417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -41,6 +41,11 @@ You can generate an API key on the **Management** page under Security. | |||||
|
||||||
.. image:: ../guide/images/create-api-key.png | ||||||
|
||||||
Confirm that the connection was successful. | ||||||
|
||||||
.. code-block:: python | ||||||
|
||||||
print(client.info()) | ||||||
|
||||||
Using the client | ||||||
---------------- | ||||||
|
@@ -49,6 +54,29 @@ Time to use Elasticsearch! This section walks you through the most important | |||||
operations of Elasticsearch. The following examples assume that the Python | ||||||
client was instantiated as above. | ||||||
|
||||||
Create a mapping for your index | ||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||||||
|
||||||
Set the expected types of your features. | ||||||
|
||||||
.. code-block:: python | ||||||
|
||||||
mappings = { | ||||||
"properties": { | ||||||
"foo": { | ||||||
"type" : "text" | ||||||
}, | ||||||
"bar" : { | ||||||
"type" : "text", | ||||||
"fields" : { | ||||||
"keyword" : { | ||||||
"type" : "keyword", | ||||||
"ignore_above" : 256 | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
Creating an index | ||||||
^^^^^^^^^^^^^^^^^ | ||||||
|
@@ -57,7 +85,7 @@ This is how you create the `my_index` index: | |||||
|
||||||
.. code-block:: python | ||||||
|
||||||
client.indices.create(index="my_index") | ||||||
client.indices.create(index="my_index", mappings = mappings) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally all code snippets should be directly runnable, what do you think about defining mappings in the same code block? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a quickstart, I think there should be only one way to create an index, what do you think? I would then remove "Creating an index" and rename "Create a mapping for your index" to "Create an index with mappings" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
||||||
Indexing documents | ||||||
|
@@ -76,6 +104,30 @@ This indexes a document with the index API: | |||||
}, | ||||||
) | ||||||
|
||||||
You can also index multiple documents at once with the bulk API: | ||||||
|
||||||
.. code-block:: python | ||||||
|
||||||
def generate_operations(documents, index_name): | ||||||
operations = [] | ||||||
for i, document in enumerate(documents): | ||||||
operations.append({"index": {"_index": index_name, "_id": i}}) | ||||||
operations.append(document) | ||||||
return operations | ||||||
|
||||||
client.bulk(index=index_name, operations=generate_operations(books, index_name), refresh=True) | ||||||
iuliaferoli marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Alternatively, you can use one of the helper functions: | ||||||
|
||||||
.. code-block:: python | ||||||
|
||||||
from elasticsearch import helpers | ||||||
|
||||||
def generate_docs(documents, index_name): | ||||||
for i, document in enumerate(documents): | ||||||
yield dict(_index=index_name, _id=f"{i}", _source=document) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is equivalent to |
||||||
|
||||||
helpers.bulk(client, generate_docs(books, index_name)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with the mapping, this code is not runnable. Maybe def generate_docs():
for i in range(10):
yield {
"_index": "my_index",
"foo": f"foo {i}",
"bar": "bar",
}
helpers.bulk(client, generate_docs()) The advantages of this version:
|
||||||
|
||||||
Getting documents | ||||||
^^^^^^^^^^^^^^^^^ | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please format the code using https://github.com/psf/black to fix the indentation? You may need a comma after 256 to get the results you want.