# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda root]
#     language: python
#     name: conda-root-py
# ---

# # Ealsticsearch tutorial 

#import required modules
import requests
import json
import uuid
from datetime import datetime


# ## Helper functions

# +
def pprint(data):
    print (json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))

def get_new_id():
    return str(uuid.uuid1()).replace('-','')
    
def get(url,data):
    return requests.get(url).json()

def delete(url,data):
    return requests.delete(url).json()

def put(url, payload):
    return requests.put(url,data= json.dumps(payload) ).json()

def post(url, payload):
    return requests.post(url,data= json.dumps(payload) ).json()

services={
    'GET':get,
    'PUT':put,
    'POST':post,
    'DELETE':delete
}

def curl(url, method, payload={}):
    return services[method](url,payload)


# -

# ## Check Elastic Server

url = 'http://127.0.0.1:9200'
res = curl(url,'GET')
pprint(res)

# ## Let's Define index name and doc type

index_name = "bigdatairan"
doc_type = 'tweet'

# ## Create new Index with schema

_mappings={
        "mappings": {
            "tweet" : {
                "properties" : {
                    "coordinate":{"type" : "geo_point"}
                }
            }
        }
    }

url = 'http://127.0.0.1:9200/{}'.format(index_name)
res = curl(url,'POST',_mappings)
pprint (res)

# ## Create new document

tweet={
    'author':'<NAME>',
    'tweet_text':"150tr gigabytes. That's the amount of information in a human body",
    'coordinate' : [35.6683226, 35.6683226],
    'user_lang':'Fa',
    'tweet_lang':'En',
    'user_followers_count':203,
    'gender':'male',
    'created_at':datetime.now().isoformat()
}

# ## Index document

documet_id=get_new_id()
url = 'http://127.0.0.1:9200/{}/{}/{}'.format(index_name, doc_type, documet_id)
res = curl(url,'PUT',tweet)
pprint (res)

# ## Let's check current schema

url = 'http://127.0.0.1:9200/bigdatairan/_mapping'
res = curl(url,'GET')
pprint(res)

# ## Delete Index

url = 'http://127.0.0.1:9200/{}'.format(index_name)
res = curl(url,'DELETE')
pprint (res)



StarcoderdataJupyterScriptsDedupFiltered