In this blog post, we'll show you how to use the cat API to get index and status information for an Elasticsearch cluster.
Introducing the cat API
To use the cat API, you need to specify the index name and the type of information that you want to retrieve. For example, to get information about the indices in your cluster, you can use the following command.
curl -X GET localhost:9200/_cat/indices - shows the details about the indices in our cluster.
This will return information about the health of your cluster indices, including the number of documents the index contains and the count of deleted documents.
In the above response, the header section is missing by default. Elasticsearch does not append a header to the cat response. If you want the header then append v=true as query value in the URL, also You can also filter the index based on health status like below where we filtered indices those health is yellow.
curl -X GET localhost:9200/_cat/indices?health=yellow&v=true
Finally, you can use sort the index based on columns such as if we want to sort the response based on document count or storage size, to sort the response s=columnsName:asec or desc.
Below we have sorted the responses in descending order by document count.
curl -X GET localhost:9200/_cat/indices?v=true&s=docs.count:desc
By default, the response is in table format. We can also export the response in other formats such as JSON. Give the format you prefer in the URL like below.
curl -X GET localhost:9200 /_cat/health?v=true&format=json
How to use the cat api to get index and status information for an Elasticsearch cluster
By sending a request from Kibana, we can see the request and the response for the cat API. If you don’t have Kibana installed, you could have tried with CURL in the terminal.
Let's see how we can leverage the cat API using a Java client within a Spring Boot application. To do this, first add the maven dependency and create the elastic search client.
let's create a model class to hold the query parameters we will use to filter the cat API response.
Model class
The IndexingRequest.filter contains the fields to hold the query parameter we can send in a cat request.
@Data
public class IndexingRequest {
private String indexname;
private Object mapping;
private Setting setting;
@Data
public class Setting {
private String numberOfShards;
private String numberOfReplicas;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Filter {
private HealthStatus health;
private List < ExpandWildcard > status;
private List < String > indices;
}
}
Service class
This class maintains the functionality to create the cat request based on the available filter and send the created request using ElasticsearchClient.
IndicesRequest.Builder() class uses the builder pattern. We will build the request object based on the available filters. The build() method returns the request object created by the builder, and the filters are used to create the request object.
@Service
public class IndexService {
@Autowired
ElasticsearchClient client;
public IndicesResponse getIndexReport(Filter filter) throws IOException {
co.elastic.clients.elasticsearch.cat.IndicesRequest.Builder builder = new IndicesRequest.Builder();
if (Objects.nonNull(filter)) {
if (Objects.nonNull(filter.getHealth())) {
builder.health(filter.getHealth());
}
if (Objects.nonNull(filter.getStatus()) && !filter.getStatus().isEmpty()) {
builder.expandWildcards(filter.getStatus());
}
if (Objects.nonNull(filter.getIndices()) && !filter.getIndices().isEmpty()) {
builder.index(filter.getIndices());
}
}
return client.cat().indices(builder.build());
}
}
Controller class
The /index/report GET endpoint handles the request and returns the cat response. The getResponse() method takes the response from Elasticsearch and writes them into a string to return in JSON format.
The cat API response returns all indexes in Elasticsearch, their status (whether it's open or closed), documents in the index, and the number of documents deleted.
@RestController
@RequestMapping("/index")
public class ElasticIndexController {
@Autowired
IndexService indexService;
@GetMapping(path = "report", produces = "application/json")
public String getIndexReport(@RequestBody IndexingRequest.Filter filter)
throws ElasticsearchException, IOException {
return getResponse(indexService.getIndexReport(filter));
}
private String getResponse(JsonpSerializable response) throws IOException {
StringWriter writer = new StringWriter();
JsonGenerator generator = new JacksonJsonpGenerator(new JsonFactory().createGenerator(writer));
response.serialize(generator, new JacksonJsonpMapper());
generator.flush();
return writer.toString();
}
}
Please start our application,send request from postman to and see the response.We are able to get 200 postive reponse as below in postman.