Managing documents

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.

Getting ready

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.

How to do it...

For managing documents, we will perform the following steps:

  1. We'll need to import the required classes to execute all the document CRUD operations via the native client:
            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; 
    
  2. We create the client and will remove the index that contains our data if it exists:
            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); 
    
  3. We will call the create 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"); 
               } 
    
  4. Now, we can store a document in Elasticsearch via the prepareIndex call:
            IndexResponse ir = client.prepareIndex(index, type, 
            "2").setSource("text", "unicorn").execute().actionGet(); 
            System.out.println("Version: " + ir.getVersion()); 
    
  5. We can retrieve the stored document via the prepareGet call:
            GetResponse gr = client.prepareGet(index, type, 
            "2").execute().actionGet(); 
            System.out.println("Version: " + gr.getVersion()); 
    
  6. We can update the stored document via the 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()); 
    
  7. We can delete the stored document via the prepareDelete call:
            DeleteResponse dr = client.prepareDelete(index, type, 
            "2").execute().actionGet(); 
    
  8. We can now free up the resources used:
            io.deleteIndex(index); 
            //we need to close the client to free resources 
            client.close(); 
             } 
            } 
    
  9. The console output result will be:
            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 
    
  10. The document version, after an update action and if the document is re-indexed with new changes, is always incremented by 1.

How it works...

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:

  • A JSON string {"field": "value"}
  • A string and a value (from 1 up to 4 couples): field1, value1, field2,value2, field3, value3, field4, value4
  • A builder jsonBuilder().startObject().field(field,value).endObject()
  • A byte arrays

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:

  • Checking if the index was successfully
  • Getting the ID of the indexed document, if it was not provided during index action
  • Retrieving the document version

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.

See also

In our recipes we have used all the CRUD operations on a document. For more details about these actions refers to:

  • The Indexing a document recipe in Chapter 4, Basic Operations on indexing a document
  • The Getting a document recipe in Chapter 4, Basic Operations on retrieving a stored document
  • The Deleting a document recipe in Chapter 4, Basic Operations about deleting a document
  • The Updating a document recipe in Chapter 4, Basic Operations on updating a document
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.174.183