Skip to content

Instantly share code, notes, and snippets.

@dineshsonachalam
Last active February 9, 2020 20:24
Show Gist options
  • Save dineshsonachalam/36b1da2f2a1a41f06ae9b930db20aaef to your computer and use it in GitHub Desktop.
Save dineshsonachalam/36b1da2f2a1a41f06ae9b930db20aaef to your computer and use it in GitHub Desktop.
import requests
import json
def check_if_index_is_present(url):
response = requests.request("GET", url, data="")
json_data = json.loads(response.text)
return json_data
if __name__ == "__main__":
url = "http://localhost:9200/_template/search_engine_template/"
response = requests.request("GET", url, data="")
if(len(response.text)>2):
print("1. Deleted template: search_engine_template")
response_delete = requests.request("DELETE", url)
payload = {
"template": "hacker",
"settings": {
"number_of_shards": 1
},
"mappings": {
"tutorials":{
"_source": {
"enabled": True
},
"properties":{
"upvotes":{
"type":"integer"
},
"topic":{
"type":"text"
},
"title":{
"type":"text"
},
"url":{
"type":"text"
},
"labels":{
"type":"text"
}
}
}
}
}
payload = json.dumps(payload)
headers = {
'Content-Type': "application/json",
'cache-control': "no-cache"
}
response = requests.request("PUT", url, data=payload, headers=headers)
if (response.status_code == 200):
print("2. Created a new template: search_engine_template")
url = "http://localhost:9200/hacker"
json_data = check_if_index_is_present(url)
if(not 'error' in json_data):
print("3. Deleted an index: hacker")
response = requests.request("DELETE", url)
response = requests.request("PUT", url)
if (response.status_code == 200):
print("4. Created an index: hacker")
url = "http://localhost:9200/autocomplete"
json_data = check_if_index_is_present(url)
if(not 'error' in json_data):
print("5. Deleting index: autocomplete")
response = requests.request("DELETE", url)
payload = {
"mappings": {
"titles" : {
"properties" : {
"title" : { "type" : "string" },
"title_suggest" : {
"type" : "completion",
"analyzer" : "standard",
"search_analyzer" : "standard",
"preserve_position_increments": False,
"preserve_separators": False
}
}
}
}
}
payload = json.dumps(payload)
response = requests.request("PUT", url, data=payload, headers=headers)
if(response.status_code==200):
print("6. Created a new index: autocomplete")
@dilipkumarmanavalla
Copy link

hlo bro. it is giving a request status as 400. how to fix that problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment