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.
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
.
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:
import org.elasticsearch.action.admin.indices.exists.indices .IndicesExistsResponse; import org.elasticsearch.client.Client;
IndicesOperations
class that manages the index operations:public class IndicesOperations { private final Client client; public IndicesOperations(Client client) { this.client = client; }
public boolean checkIndexExists(String name){ IndicesExistsResponse response=client.admin().indices().prepareExists(name).execute() .actionGet(); return response.isExists(); }
public void createIndex(String name){ client.admin().indices().prepareCreate(name).execute() .actionGet(); }
public void deleteIndex(String name){ client.admin().indices().prepareDelete(name).execute() .actionGet(); }
public void closeIndex(String name){ client.admin().indices().prepareClose(name).execute() .actionGet(); }
public void openIndex(String name){ client.admin().indices().prepareOpen(name).execute() .actionGet(); }
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(); }
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:
client.*
we have record operations such as index, deletion of records, search, and updateadmin.indices.*
we have index related methods, such as create index, delete index, and so onadmin.cluster.*
, we have cluster-related methods, such as state and healthClient methods usually follow some conventions:
prepare*
(that is prepareCreate
) returns a request builder that can be executed with the execute
methodcreate
) require a build request and optional some action listenerAfter 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:
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();
prepareCreate
call:client.admin().indices().prepareCreate(name).execute() .actionGet();
prepareClose
call:client.admin().indices().prepareClose(name).execute() .actionGet();
prepareOpen
call:client.admin().indices().prepareOpen(name).execute() .actionGet();
prepareDelete
call:client.admin().indices().prepareDelete(name).execute() .actionGet();
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.
18.118.2.225