Data manipulation with REST API
ElasticSearch REST API can be used for various tasks. Thanks to it, we can manage indexes, change instance parameters, check nodes and cluster status, index data, and search it. But for now, we will concentrate on using the CRUD (create-retrieve-update-delete
) part of the API, which allows us to use ElasticSearch in a similar way to how you would use a NoSQL database.
Before moving on to a description of various operations, a few words about REST itself. In a REST-like architecture, every request is directed to a concrete object indicated by the path part of the address. For example, if /books/
is a reference to a list of books in our library, /books/1
is a reference to the book with the identifier 1. Note that these objects can be nested. /books/1/chapter/6
is the sixth chapter in the first book in the library, and so on. We have the subject of our API call. What about an operation that we would like to execute, such as GET
or POST
? To indicate that, request types are used. An HTTP protocol gives us quite a long list of request types to use as verbs in the API calls. Logical choices are GET
in order to obtain the current state of the requested object, POST
for changing the object state, PUT
for object creation, and DELETE
for destroying an object. There is also a HEAD
request that is only used for fetching the base information about an object.
If we look at the examples of the operations discussed in the Shutting down ElasticSearch section, everything should make more sense:
GET http://localhost:9000/
: Retrieves information about an instance as a whole
GET http://localhost:9200/_cluster/nodes/
: Retrieves information about the nodes in an ElasticSearch cluster
POST http://localhost:9200/_cluster/nodes/_shutdown
: Sends information to shut down an object in the nodes in a cluster of ElasticSearch
Now we will check how these operations can be used to store, fetch, alter, and delete data from ElasticSearch.
Storing data in ElasticSearch
In ElasticSearch, every piece of data has a defined index and type. You can think about an index as a collection of documents or a table in a database. In contrast to database records, documents added to an index have no defined structure and field types. More precisely, a single field has its type defined, but ElasticSearch can do some magic and guess the corresponding type.
Now we will try to index some documents. For our example, let's imagine that we are building some kind of CMS for our blog. One of the entities in this blog is (surprise!) articles. Using the JSON notation, a document can be presented as shown in the following example:
As we can see, the JSON document contains a set of fields, where each field can have a different form. In our example, we have a number (priority
), text (title
), and an array of strings (tags
). In the next examples, we will show you the other types. As mentioned earlier in this chapter, ElasticSearch can guess these type (because JSON is semi-typed; that is, the numbers are not in quotation marks) and automatically customize the way of storing this data in its internal structures.
Now we want to store this record in the index and make it available for searching. Choosing the index name as blog
and type as article
, we can do this by executing the following command:
You can notice a new option to cURL, -d
. The parameter value of this option is the text that should be used as a request payload—a request body. This way we can send additional information such as a document definition.
Note that the unique identifier is placed in the URL, not in the body. If you omit this identifier, the search returns an error, similar to the following:
If everything is correct, the server will answer with a JSON response similar to this:
In the preceding reply, ElasticSearch includes information about the status of the operation and shows where the new document was placed. There is information about the document's unique identifier and current version, which will be incremented automatically by ElasticSearch every time the document changes.
In the above example, we've specified the document identifier ourselves. But ElasticSearch can generate this automatically. This seems very handy, but only when an index is the only source of data. If we use a database for storing data and ElasticSearch for full text searching, synchronization of this data will be hindered unless the generated identifier is stored in the database as well. Generation of a unique key can be achieved by using the following command:
Notice POST
instead of PUT
. Referring to the previous description of the REST verbs, we wanted to change the list of documents in an index rather than create a new entity, and that's why we used POST
instead of PUT
. The server should respond with a response similar to the following:
Note the highlighted line, which has an automatically generated unique identifier.
We already have documents stored in our instance. Now let's try to retrieve them:
Then the server returns the following response:
In the response, besides index, type, identifier, and version, we also see the information saying that the document was found and the source of this document. If the document is not found, we get a reply as follows:
Of course, there is no information about the version and source.
Updating documents in an index is a more complicated task. Internally, ElasticSearch must fetch the document, take its data from the _source
field, remove the old document, apply changes, and index it as a new document. ElasticSearch implements this through a script given as a parameter. This allows us to do more sophisticated document transformation than simple field changes. Let's see how it works in a simple case.
After executing the following command:
The server replies with the following:
It works! To be sure, let's retrieve the current document:
The server changed the contents of our article and the version number for this document. Notice that we didn't have to send the whole document, only the changed parts. But remember that to use the update functionality, we need to use the _source
field—we will describe how to use the _source
field in the Extending your index structure with additional internal information section in Chapter 3, Extending Your Structure and Search.
There is one more thing about document updates—if your script uses a field value from a document that is to be updated, you can set a value that will be used if the document doesn't have that value present. For example, if you would like to increment the counter field of the document and it is not present, you can use the upsert
section in your request to provide the default value that is going to be used. For example:
In the preceding example, if the document we are updating doesn't have a value in the counter field, the value of 0
will be used.
We have already seen how to create (PUT
) and retrieve (GET
) documents. A document can be removed in the similar way but the only difference is in the verb used. Let's execute the following delete command:
Now we are able to use the CRUD operations. This lets us create applications using ElasticSearch as a simple key-value store. But this is only the beginning!