examples/python/elastic_write.py

from datetime import datetime
import random
from elasticsearch import Elasticsearch
import time
import random

hosts = ['localhost']
es = Elasticsearch(hosts)

while True:
    doc = {
        'timestamp': datetime.utcnow(), # Watch out for timezones. I spent two hours looking for my data, but it was in the future....
        'load':      random.random() * 8,
        'rnd':       random.randrange(100),
        'name':      random.choice(['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptunus']),
        'yes':       random.choice([True, False]),
    }

    index_name = 'experimental'
    print(index_name)
    print(doc)
    res = es.index(index = index_name, doc_type = 'samples', body = doc)
    print(res)
    time.sleep(random.random())

# curl -X PUT -H "Content-Type: application/json" -d '{ "index": { "number_of_replicas": 0 } }' http://localhost:9200/_settings

examples/python/elastic_search.py

from datetime import datetime
from elasticsearch import Elasticsearch

hosts = ['localhost']
es = Elasticsearch(hosts)

index_name = 'experimental'

#res = es.search(index = index_name , body={"query": {"match_all": {}}})             # fetch all the records
#res = es.search(index = index_name , body={"query": {"match": {'name': 'Joe'} }})   # select by equality
#res = es.search(index = index_name , body={"query": {"match": {'name': 'Joe'} }}, size = 3)  # limit the number of results
#res = es.search(index = index_name , body={"query": {"range": {"rnd": {'gte': 20} }}})
#res = es.search(index = index_name , body={"query": {"range": {"timestamp": {'gte': datetime.now()} }}})  # some way to pass a datetime object
res = es.search(index = index_name , body={"query": {"range": {"timestamp": {'gte': '2019-03-13T12:02:49'} }}}) # or a string representing a date

print("Got {} Hits:".format(res['hits']['total']))
for hit in res['hits']['hits']:
    print(hit)

Data Generator

The following script will create and index some random time series.

examples/python/elastic_search_data_generator.py

from elasticsearch import Elasticsearch
from datetime import datetime
import sys
import random
import time

def connect():
   es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
   return es

def list_indices(es):
    for index in es.indices.get('*'):
        print(index)
        print(index.__class__.__name__)
    print('---')

if __name__ == '__main__':
    es = connect()
    list_indices(es)

    for _ in range(100):
        es.index('experimental', doc_type = 'experimental', body = {
            'timestamp': datetime.utcnow(),
            'name': random.choice(['Doc', 'Grumpy', 'Happy', 'Sleepy', 'Bashful', 'Sneezy', 'Dopey', 'Snowwhite']),
            'load': random.random(),
        })
        time.sleep(1 + random.random())