Summaries/Courses/PostgreSQL-for-Everybody-Specialization/Database Architecture, Scale, and NoSQL with Elasticsearch/test.ipynb
2024-01-18 22:20:52 +01:00

4.1 KiB

In [20]:
from elasticsearch import Elasticsearch
from elasticsearch import RequestsHttpConnection
import time
import copy
import hidden
import uuid
import json
import hashlib
import requests
import json
import hidden
from datetime import datetime
In [17]:
secrets = hidden.elastic()

es = Elasticsearch(
    [ secrets['host'] ],
    http_auth=(secrets['user'], secrets['pass']),
    url_prefix = secrets['prefix'],
    scheme=secrets['scheme'],
    port=secrets['port'],
    connection_class=RequestsHttpConnection,
)
indexname = secrets["user"]
In [18]:
res = es.indices.delete(index=indexname, ignore=[400, 404])
print("Dropped index")
print(res)
Dropped index
{'acknowledged': True}
In [19]:
res = es.indices.create(index=indexname)
print("Created the index...")
print(res)
Created the index...
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'pg4e_4cb8bb5508'}
In [24]:
tweet = '''building on your left Your friend is very late and calls you to
tell you that they are on a farm and walking around behind a barn
with no sign of a restaurant Then you say did you turn left or
right at the gas station and they say I followed your directions
perfectly I have them written down it says turn left and go one'''

doc = {
    "author": "kimchy",
    "type": "tweet",
    "text": tweet,
    "timestamp": datetime.now(),
}
In [25]:
res = es.index(index=indexname, id="abc", body=doc)
print("Added document...")
print(res["result"])
Added document...
updated
In [26]:
res = es.get(index=indexname, id="abc")
print("Retrieved document...")
print(res)
Retrieved document...
{'_index': 'pg4e_4cb8bb5508', '_type': '_doc', '_id': 'abc', '_version': 2, '_seq_no': 1, '_primary_term': 1, 'found': True, '_source': {'author': 'kimchy', 'type': 'tweet', 'text': 'building on your left Your friend is very late and calls you to\ntell you that they are on a farm and walking around behind a barn\nwith no sign of a restaurant Then you say did you turn left or\nright at the gas station and they say I followed your directions\nperfectly I have them written down it says turn left and go one', 'timestamp': '2024-01-18T21:42:09.213160'}}