from elasticsearch import Elasticsearch
def delete_indices(es, my_index):
if es.indices.exists(my_index):
es.indices.delete(index=my_index)
def create_indices(es, my_index):
es.indices.create(index=my_index)
def search_index(es, my_index):
body = {
"query":{
"match":{
"name":"jgc"
}
}
}
res = es.search(index=my_index, body=body)
return res
if __name__ == "__main__":
es = Elasticsearch(['http://127.0.0.1:9200'])
print(es.ping())
create_indices(es, "newlab")
delete_indices(es, "newlab")
print(search_index(es, "lab"))
for item in res['hits']['hits']:
print(item['_source'])
from elasticsearch import Elasticsearch
def delete_indices(es, my_index):
if es.indices.exists(my_index):
es.indices.delete(index=my_index)
def create_indices(es, my_index):
mappings = {
"mappings":{
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
}
}
}
}
es.indices.create(index=my_index, body=mappings)
def insert_index(es, my_index):
body = {
"name":"smr",
"age":10
}
es.index(index=my_index, body=body)
def search_index(es, my_index):
body = {
"query":{
"match_all":{
}
}
}
res = es.search(index=my_index, body=body)
return res
if __name__ == "__main__":
es = Elasticsearch(['http://127.0.0.1:9200'])
print(search_index(es, "test"))