How to run optimize query query using Java API?

Hi,

I want to run optimize query on indexes using Java Api.
Please suggest me how can i achieve this.

Thanks,
Sumit Gupta

The Optimize API
The Optimize API invokes the segments merge process on the given
indices. Just like
the RESTful version of this API, we are allowed to set the target number
of segments
or only remove the deleted documents from the index. For example, the
following
code snippet executes the optimize operation on the library index,
specifies that
the maximum number of segments is 2. The flush should be performed after the
operation and we are not only interested in removing the deleted documents:
OptimizeResponse response = client.admin().indices()
.prepareOptimize("library")
.setMaxNumSegments(2)
.setFlush(true)
.setOnlyExpungeDeletes(false)
.execute().actionGet();