The native APIs for managing documents (index, delete, and update) are the most important after the search APIs. In this recipe, we will see how to use them. In the next recipe, we will proceed to bulk actions to improve performances.
You need an up-and-running Elasticsearch installation as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
A Maven tool, or an IDE that natively supports it for Java programming such as Eclipse or IntelliJ IDEA, must be installed.
The code for this recipe is in the chapter_14/nativeclient
directory and the referred class is DocumentOperations
.
For managing documents, we will perform the following steps:
import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.script.Script; import java.io.IOException; import java.net.UnknownHostException;
public class DocumentOperations { public static void main(String[] args) throws UnknownHostException { String index = "mytest"; String type = "mytype"; Client client = NativeClient.createTransportClient(); IndicesOperations io = new IndicesOperations(client); if (io.checkIndexExists(index)) io.deleteIndex(index);
index
by providing the required mapping:try { client.admin().indices().prepareCreate(index) .addMapping(type, XContentFactory.jsonBuilder() .startObject() .startObject(type) .startObject("properties") .startObject("text").field("type", "text").field("store", "yes").endObject() .endObject() .endObject() .endObject()) .execute().actionGet(); } catch (IOException e) { System.out.println("Unable to create mapping"); }
prepareIndex
call:IndexResponse ir = client.prepareIndex(index, type, "2").setSource("text", "unicorn").execute().actionGet(); System.out.println("Version: " + ir.getVersion());
prepareGet
call:GetResponse gr = client.prepareGet(index, type, "2").execute().actionGet(); System.out.println("Version: " + gr.getVersion());
prepareUpdate
call using a script in painless
:UpdateResponse ur = client.prepareUpdate(index, type, "2").setScript(new Script("ctx._source.text = 'v2'")).execute().actionGet(); System.out.println("Version: " + ur.getVersion());
prepareDelete
call:DeleteResponse dr = client.prepareDelete(index, type, "2").execute().actionGet();
io.deleteIndex(index); //we need to close the client to free resources client.close(); } }
no modules loaded loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin] loaded plugin [org.elasticsearch.percolator.PercolatorPlugin] loaded plugin [org.elasticsearch.script.mustache.MustachePlugin] loaded plugin [org.elasticsearch.transport.Netty3Plugin] loaded plugin [org.elasticsearch.transport.Netty4Plugin] Version: 1 Version: 1 Version: 2
1
.Before executing a document action, a client and the index must be available and document mapping should be created (the mapping is optional, because it can be inferred from the indexed document).
To index a document via the native client, the prepareIndex
method is created. It requires the index and the type as arguments. If an ID is provided, it will be used; otherwise a new one will be created.
In the previous example, we put the source in the form of a key, value, but many forms are available to pass as source. They are:
{"field": "value"}
field1, value1, field2,
value2, field3, value3, field4, value4
jsonBuilder().startObject().field(field,value).endObject()
Obviously, it's possible to add all the parameters that we looked at in the Indexing a Document recipe in Chapter 4, Basic Operations, such as parent, routing, and so on. In the previous example, the call was:
IndexResponse ir=client.prepareIndex(index, type, "2").setSource("text", "unicorn").execute().actionGet();
The IndexReponse
return value can be used in several ways:
To retrieve a document, you need to know the index/type/ID; the client method is prepareGet
. It requires the usual triplet (index
, type
, id
), but a lot of other methods are available to control the routing (such as souring, parent) or fields as we have seen in the Getting a Document recipe in Chapter 4, Basic Operations. In the previous example, the call is as follows:
GetResponse gr=client.prepareGet(index, type, "2").execute().actionGet();
The GetResponse
return type contains all the requests (if the document exists) and document information (source
, version
, index
, type
, id
).
To update a document, it's necessary to know the index/type/ID and provide a script or a document to be used for the update. The client method is prepareUpdate
.
In the previous example, there is:
UpdateResponse ur = client.prepareUpdate(index, type, "2").setScript(new Script("ctx._source.text = 'v2'")).execute().actionGet();
The script code must be a string. If the script language is not defined, the default painless
method is used.
The returned response contains information about the execution and the new version value to manage concurrency.
To delete a document (without the need to execute a query), we need to know the index/type/ID triple and we can use the prepareDelete
client method to create a delete request. In the previous code, we used:
DeleteResponse dr = client.prepareDelete("test", "type", "2").execute().actionGet();
The delete request allows all the parameters to be passed to it that we saw in the Deleting a document recipe in Chapter 4, Basic Operations, to control the routing and version.
In our recipes we have used all the CRUD operations on a document. For more details about these actions refers to:
3.145.174.183