Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch python client: Getting the ES version through API call

I want to get the current Elasticsearch version through the python API. I could easily get it through a http call like

import requests
requests.get(http://endpoint:9200)

But I am wondering is there any way to get the version through the API call instead of http request to the endpoint. Like

from elasticsearch import Elasticsearch
es = Elasticsearch()

I went through the Elasticsearch python client documentation, but couldn't find the call that would fetch the current ES version (https://elasticsearch-py.readthedocs.org/en/master/api.html)

like image 395
sysuser Avatar asked Jun 11 '15 21:06

sysuser


People also ask

How do I check Elasticsearch version in Python?

OPTION 1: Check Version using Curl from Command Line In this example, Elasticsearch is running locally on the default port so our HTTP request will be to http://localhost:9200 . If Elasticsearch was running on a different server your HTTP request would take the form http://YOURDOMAIN.com:9200 .

What is Doc_type Elasticsearch?

Elasticsearch is a powerful open source search and analytics engine that makes data easy to explore. Doc_type is a field in Elasticsearch that allows you to specify the type of document you are indexing. This is useful for when you have multiple types of documents in the same index.


2 Answers

You can achieve this using the info command:

Example:

from elasticsearch import Elasticsearch
es = Elasticsearch()
es.info()
like image 115
keety Avatar answered Oct 20 '22 20:10

keety


If you want to get only version number, you can do something like this:

#/usr/bin/env python
import json
import logging
def get_cluster_version(server, user, password):
    cluster_version = "version"

    r = do_request(verb='get',
               server='http://{0}'.format(server),
               auth=(user, password),
               verify=False)
    json_data = json.loads(r.content.decode('utf8'))
    version_number = str(json_data["version"]["number"])
    logging.info("Elastic cluster version " + str(version_number))
like image 3
pattypeji Avatar answered Oct 20 '22 20:10

pattypeji