Last active
May 16, 2018 17:57
-
-
Save jasdeep06/414283997e9dfed8df3ece0d4e0d5be7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from elasticsearch import Elasticsearch | |
import json | |
import requests | |
from elasticsearch import helpers | |
#To get over memory contraints | |
#retardness of Macs | |
#curl - XPUT - H "Content-Type: application/json" http: // localhost:9200 / _all / _settings - d'{"index.blocks.read_only_allow_delete": false}' | |
#helper function to make calls and parse response | |
def getResponse(url): | |
resp=requests.get(url) | |
return json.loads(resp.content) | |
#connect to es | |
es=Elasticsearch([{"host":"localhost","port":9200,"verify_certs":True}]) | |
#our document | |
body=getResponse("https://anapioficeandfire.com/api/characters/2") | |
#Create an index and insert the object in es | |
es.index(index="got",doc_type="characters" ,body=body,id=2) | |
#gets the document with given specifications | |
print(es.get(index="got",doc_type="characters",id=2)) | |
#gets all the indices in cluster | |
print(es.indices.get("*")) | |
# partial update a document | |
es.update(index="got",doc_type="characters",id=2 ,body={"doc":{"name":"Jasdeep"}}) | |
#checking if it was updated | |
print(es.get(index="got",doc_type="characters",id=2)) | |
#preparing list for bulk imports | |
items=[{"_index":"got","_type":"characters", | |
"_id":i, | |
"_source":getResponse("https://anapioficeandfire.com/api/characters/"+str(i))} for i in range(3,10)] | |
#bulk importing to es | |
helpers.bulk(es,items) | |
#could not find porridge which was object with id=7 | |
print("Porrige",es.search(index="got",doc_type="characters",body={"query":{"match":{"aliases":"Porridge"}}})) | |
#refreshing the count of documents | |
es.indices.refresh("got") | |
#found porridge.Means it needs refresh to get things into search space. | |
print("Porrige",es.search(index="got",doc_type="characters",body={"query":{"match":{"aliases":"Porridge"}}})) | |
#number of documents | |
print(es.count(index="got").get("count")) | |
#searching in es | |
print(es.search(body={"query": {"match":{"aliases":"Quickfinger"}}})) | |
#deleting a document | |
es.delete(index="got",doc_type="characters",id=2) | |
#refreshing the count of documents | |
es.indices.refresh("got") | |
#number of documents | |
print(es.count(index="got").get("count")) | |
#deleting the index "got" | |
es.indices.delete(index="got") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment