python操作elasticsearch(python操作es数据库)

网友投稿 530 2022-08-28


python操作elasticsearch(python操作es数据库)

from elasticsearch import Elasticsearch# 删除索引def delete_indices(es, my_index): if es.indices.exists(my_index): # 确认删除再改为True es.indices.delete(index=my_index) # 删除# 创建索引示例1def 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) # res = es.search(index=my_index, body=body, filter_path=['hits.hits'] return resif __name__ == "__main__": # 链接数据库 es = Elasticsearch([' # 测试是否能链接 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): # 确认删除再改为True 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 } # 也可以在body里添加"_id"指定id,默认id为随机值 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) # res = es.search(index=my_index, body=body, filter_path=['hits.hits'] return resif __name__ == "__main__": # 链接数据库 es = Elasticsearch([' # 测试是否能链接 # print(es.ping()) # 创建索引 # create_indices(es, "test") # 插入一条数据 # insert_index(es, "test") # 查询 print(search_index(es, "test")) # for item in res['hits']['hits']: # print(item['_source'])


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java中通过行为参数化传递代码方案
下一篇:python异步asyncio(python异步处理)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~