Skip to content

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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion docs/sphinx/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------
Expand All @@ -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
}
}
}
}
}
Copy link
Member

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.


Creating an index
^^^^^^^^^^^^^^^^^
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
client.indices.create(index="my_index", mappings = mappings)
client.indices.create(index="my_index", mappings=mappings)



Indexing documents
Expand All @@ -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)

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent to yield {"_index": index_name, "_id": f"{i}", "_source": document}. Is there a specific reason to prefer using dict?


helpers.bulk(client, generate_docs(books, index_name))
Copy link
Member

@pquentin pquentin Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the mapping, this code is not runnable. Maybe generate_docs should be defined like that?

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:

  • It can be copy/pasted directly
  • It reuses the index created above
  • It's easy to adapt to generate much more documents
  • It does not specify a doc id, which is better for performance


Getting documents
^^^^^^^^^^^^^^^^^
Expand Down