In [10]:
import requests
import json
from elasticsearch import Elasticsearch

headers = {'Content-Type': 'application/json'}

In [34]:
print(requests.get('http://localhost:9200/_cat/health?v').text)


epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1509882436 14:47:16  elasticsearch yellow          1         1     16  16    0    0       16             0                  -                 50.0%


In [4]:
print(requests.get('http://localhost:9200/_cat/nodes?v').text)


ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           17          63  10    0.70    0.55     0.73 mdi       *      -L9u61j


In [35]:
print(requests.get('http://localhost:9200/_cat/indices?v').text)


health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   zadolbali W7y-y1w-Q_S-7oMdE0_FSA   5   1          0            0       810b           810b
yellow open   customer  qxf6xU2sQ6263aPjs6-04A   5   1          2            0      7.4kb          7.4kb
yellow open   bank      _9xGLRodRhKMl2zsDd1JLQ   5   1       1000            0    640.8kb        640.8kb
yellow open   .kibana   L5hEXnBERGe6czr2OJchfg   1   1          2            1     10.8kb         10.8kb


In [9]:
print(requests.put('http://localhost:9200/customer').text)
print(requests.get('http://localhost:9200/_cat/indices?v').text)


{"error":{"root_cause":[{"type":"index_already_exists_exception","reason":"index [customer/qxf6xU2sQ6263aPjs6-04A] already exists","index_uuid":"qxf6xU2sQ6263aPjs6-04A","index":"customer"}],"type":"index_already_exists_exception","reason":"index [customer/qxf6xU2sQ6263aPjs6-04A] already exists","index_uuid":"qxf6xU2sQ6263aPjs6-04A","index":"customer"},"status":400}
health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana   L5hEXnBERGe6czr2OJchfg   1   1          2            1     10.8kb         10.8kb
yellow open   zadolbali W7y-y1w-Q_S-7oMdE0_FSA   5   1          0            0       810b           810b
yellow open   customer  qxf6xU2sQ6263aPjs6-04A   5   1          0            0       324b           324b


In [8]:
print(requests.delete('http://localhost:9200/test').text)


{"acknowledged":true}

In [15]:
print(requests.put('http://localhost:9200/customer/external/1?pretty', headers=headers, data=json.dumps({
  "name": "Jane Doe"
})).text)


{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : false
}


In [28]:
print(requests.get('http://localhost:9200/customer/external/1?pretty').text)


{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4,
  "found" : true,
  "_source" : {
    "name" : "Jane Doe",
    "age" : 25
  }
}


In [20]:
print(requests.post('http://localhost:9200/customer/external?pretty', headers=headers, data=json.dumps({
  "name": "Jane Doe"
})).text)


{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "AV-L5b2i2_-3iU64yVCW",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}


In [27]:
print(requests.post('http://localhost:9200/customer/external/1/_update?pretty', headers=headers, data=json.dumps({
  "script" : "ctx._source.age += 5"
})).text)


{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}


In [33]:
print(requests.delete('http://localhost:9200/customer/external/1?pretty').text)


{
  "found" : true,
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 5,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}


In [37]:
print(requests.delete('http://localhost:9200/bank').text)
print(requests.delete('http://localhost:9200/customer').text)


{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"bank","index_uuid":"_na_","index":"bank"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"bank","index_uuid":"_na_","index":"bank"},"status":404}
{"acknowledged":true}

In [39]:
print(requests.get('http://localhost:9200/zadolbali?pretty').text)


{
  "zadolbali" : {
    "aliases" : { },
    "mappings" : {
      "stories" : {
        "properties" : {
          "id" : {
            "type" : "integer"
          },
          "likes" : {
            "type" : "integer"
          },
          "published" : {
            "type" : "date",
            "format" : "yyyyMMdd"
          },
          "tags" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "text" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "title" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "url" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      },
      "story" : {
        "properties" : {
          "id" : {
            "type" : "integer"
          },
          "likes" : {
            "type" : "integer"
          },
          "published" : {
            "type" : "date",
            "format" : "yyyyMMdd"
          },
          "tags" : {
            "type" : "text"
          },
          "text" : {
            "type" : "text"
          },
          "title" : {
            "type" : "text"
          },
          "url" : {
            "type" : "text"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1509882560528",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "bPio5U0rRG6WPetRLzMvnQ",
        "version" : {
          "created" : "5060399"
        },
        "provided_name" : "zadolbali"
      }
    }
  }
}


In [ ]: