I would like to list all indexes present on an ElasticSearch server. I tried this:
curl -XGET localhost:9200/
but it just gives me this:
You can query localhost:9200/_status
and that will give you a list of indices and information about each. The response will look something like this:
{
"ok" : true,
"_shards" : { ... },
"indices" : {
"my_index" : { ... },
"another_index" : { ... }
}
}
Accessing the Secured Elastic Search though Curl (Update 2020)
If the Elastic Search
is secured, You can use this command to list indices
curl http://username:password@localhost:9200/_aliases?pretty=true
For Elasticsearch 6.X, I found the following the most helpful. Each provide different data in the response.
# more verbose
curl -sS 'localhost:9200/_stats' | jq -C ".indices" | less
# less verbose, summary
curl -sS 'localhost:9200/_cluster/health?level=indices' | jq -C ".indices" | less
I use this to get all indices:
$ curl --silent 'http://127.0.0.1:9200/_cat/indices' | cut -d\ -f3
With this list you can work on...
$ curl -s 'http://localhost:9200/_cat/indices' | head -5
green open qa-abcdefq_1458925279526 1 6 0 0 1008b 144b
green open qa-test_learnq_1460483735129 1 6 0 0 1008b 144b
green open qa-testimportd_1458925361399 1 6 0 0 1008b 144b
green open qa-test123p_reports 1 6 3868280 25605 5.9gb 870.5mb
green open qa-dan050216p_1462220967543 1 6 0 0 1008b 144b
To get the 3rd column above (names of the indices):
$ curl -s 'http://localhost:9200/_cat/indices' | head -5 | cut -d\ -f3
qa-abcdefq_1458925279526
qa-test_learnq_1460483735129
qa-testimportd_1458925361399
qa-test123p_reports
qa-dan050216p_1462220967543
NOTE: You can also use awk '{print $3}'
instead of cut -d\ -f3
.
You can also suffix the query with a ?v
to add a column header. Doing so will break the cut...
method so I'd recommend using the awk..
selection at this point.
$ curl -s 'http://localhost:9200/_cat/indices?v' | head -5
health status index pri rep docs.count docs.deleted store.size pri.store.size
green open qa-abcdefq_1458925279526 1 6 0 0 1008b 144b
green open qa-test_learnq_1460483735129 1 6 0 0 1008b 144b
green open qa-testimportd_1458925361399 1 6 0 0 1008b 144b
green open qa-test123p_reports 1 6 3868280 25605 5.9gb 870.5mb
If you're working in scala, a way to do this and use Future
's is to create a RequestExecutor, then use the IndicesStatsRequestBuilder and the administrative client to submit your request.
import org.elasticsearch.action.{ ActionRequestBuilder, ActionListener, ActionResponse }
import scala.concurrent.{ Future, Promise, blocking }
/** Convenice wrapper for creating RequestExecutors */
object RequestExecutor {
def apply[T <: ActionResponse](): RequestExecutor[T] = {
new RequestExecutor[T]
}
}
/** Wrapper to convert an ActionResponse into a scala Future
*
* @see http://chris-zen.github.io/software/2015/05/10/elasticsearch-with-scala-and-akka.html
*/
class RequestExecutor[T <: ActionResponse] extends ActionListener[T] {
private val promise = Promise[T]()
def onResponse(response: T) {
promise.success(response)
}
def onFailure(e: Throwable) {
promise.failure(e)
}
def execute[RB <: ActionRequestBuilder[_, T, _, _]](request: RB): Future[T] = {
blocking {
request.execute(this)
promise.future
}
}
}
The executor is lifted from this blog post which is definitely a good read if you're trying to query ES programmatically and not through curl. One you have this you can create a list of all indexes pretty easily like so:
def totalCountsByIndexName(): Future[List[(String, Long)]] = {
import scala.collection.JavaConverters._
val statsRequestBuider = new IndicesStatsRequestBuilder(client.admin().indices())
val futureStatResponse = RequestExecutor[IndicesStatsResponse].execute(statsRequestBuider)
futureStatResponse.map { indicesStatsResponse =>
indicesStatsResponse.getIndices().asScala.map {
case (k, indexStats) => {
val indexName = indexStats.getIndex()
val totalCount = indexStats.getTotal().getDocs().getCount()
(indexName, totalCount)
}
}.toList
}
}
client
is an instance of Client which can be a node or a transport client, whichever suits your needs. You'll also need to have an implicit ExecutionContext
in scope for this request. If you try to compile this code without it then you'll get a warning from the scala compiler on how to get that if you don't have one imported already.
I needed the document count, but if you really only need the names of the indices you can pull them from the keys of the map instead of from the IndexStats
:
indicesStatsResponse.getIndices().keySet()
This question shows up when you're searching for how to do this even if you're trying to do this programmatically, so I hope this helps anyone looking to do this in scala/java. Otherwise, curl users can just do as the top answer says and use
curl http://localhost:9200/_aliases
To get all the details in Kibana.
GET /_cat/indices
To get names only in Kibana.
GET /_cat/indices?h=index
Without using Kibana ,You can send a get request in postman or type this in Brower so you will get a list of indices names
http://localhost:9200/_cat/indices?h=index