Managing indices with the native client

In the previous recipe, we saw how to initialize a client to send calls to an Elasticsearch cluster. In this recipe, we will see how to manage indices via client calls.

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 IndicesOperations.

How to do it...

An Elasticsearch client maps all index operations under the admin.indices object of the client. All the indices operation are here (create, delete, exists, open, close, optimize, and so on).

The following code retrieves a client and executes the main operations on indices:

  1. We import the required classes:
            import 
            org.elasticsearch.action.admin.indices.exists.indices
            .IndicesExistsResponse; 
            import org.elasticsearch.client.Client; 
    
  2. We define an IndicesOperations class that manages the index operations:
            public class IndicesOperations { 
                private final Client client; 
     
                public IndicesOperations(Client client) { 
                    this.client = client; 
                } 
    
  3. We define a function used to check the index's existence:
            public boolean checkIndexExists(String name){ 
                IndicesExistsResponse   
           response=client.admin().indices().prepareExists(name).execute()
           .actionGet(); 
                return response.isExists(); 
            } 
    
  4. We define a function used to create an index:
            public void createIndex(String name){ 
             client.admin().indices().prepareCreate(name).execute()
            .actionGet(); 
            } 
    
  5. We define a function used to delete an index:
            public void deleteIndex(String name){ 
            client.admin().indices().prepareDelete(name).execute()
            .actionGet(); 
            } 
    
  6. We define a function used to close an index:
            public void closeIndex(String name){ 
            client.admin().indices().prepareClose(name).execute()
            .actionGet(); 
            } 
    
  7. We define a function used to open an index:
            public void openIndex(String name){ 
            client.admin().indices().prepareOpen(name).execute()
            .actionGet(); 
            } 
    
  8. We test all the previously defined functions:
            public static void main( String[] args ) throws   
            InterruptedException, IOException, NodeValidationException { 
            NativeClient nativeClient=new NativeClient(); 
            Client client =nativeClient.getClient(); 
            IndicesOperations io=new IndicesOperations(client); 
            String myIndex = "test"; 
            if(io.checkIndexExists(myIndex)) 
                io.deleteIndex(myIndex); 
            io.createIndex(myIndex); 
            Thread.sleep(1000); 
            io.closeIndex(myIndex); 
            io.openIndex(myIndex); 
            io.deleteIndex(myIndex); 
     
            //we need to close the client to free resources 
            nativeClient.close(); 
            } 
    

How it works...

Before executing every index operation, a client must be available (we saw how to create one in the previous recipe).

The client has a lot of methods grouped by functionalities:

  • In the root client.* we have record operations such as index, deletion of records, search, and update
  • Under admin.indices.* we have index related methods, such as create index, delete index, and so on
  • Under admin.cluster.*, we have cluster-related methods, such as state and health

Client methods usually follow some conventions:

  • Methods starting with prepare* (that is prepareCreate) returns a request builder that can be executed with the execute method
  • Methods that starts with a verb (that is create) require a build request and optional some action listener

After building the request, it can be executed with an actionGet that can receive an optional timeout, and a response is returned.

In the previous example, we have several index calls:

  • Checking the existence, the method call is prepareExists and returns an IndicesExistsResponse object, which contains information about whether the index exists or not:
        IndicesExistsResponse      
        response=client.admin().indices().prepareExists(name).execute()
        .actionGet(); 
                return response.isExists(); 
  • Creating an index, with the prepareCreate call:
        client.admin().indices().prepareCreate(name).execute()
        .actionGet(); 
  • Closing an index, with the prepareClose call:
        client.admin().indices().prepareClose(name).execute()
       .actionGet(); 
  • Opening an index, with the prepareOpen call:
        client.admin().indices().prepareOpen(name).execute()
        .actionGet(); 
  • Deleting an index with the prepareDelete call:
        client.admin().indices().prepareDelete(name).execute()
        .actionGet(); 

Note

We have put a delay of 1 second (Thread.wait(1000)) in the code to prevent fast actions on indices, because their shard allocations are asynchronous and they require some milliseconds to be ready. The best practice is not to use a similar hack, but to poll an index's state before perform further operations, and only performing those operations when it goes green.

See also

  • The Creating an index recipe in Chapter 4, Basic Operations for details on index creation
  • The Deleting an index recipe in Chapter 4, Basic Operations for details on index deletion
  • The Opening/closing an index recipe in Chapter 4, Basic Operations for the description of opening/closing index APIs
..................Content has been hidden....................

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