当前位置: 首页 > 后端技术 > Python

python中的Elasticsearch操作

时间:2023-03-26 02:06:01 Python

importpackagefromelasticsearchimportElasticsearchlocalconnectiones=Elasticsearch(['127.0.0.1:9200'])createindexes.indices.create(index="python_es01",ignore=400)ingore=400ingore表示忽略,400表示未找到删除索引es.indices.delete(index="python_es01")检查索引是否存在es.indices.exists(index="python_es01")插入数据es.index(index="python_es01"")python_es01",doc_type="doc",id=1,body={"name":"kitty","age":50})也可以不同时加id,即es.index(index="python_es01",doc_type="doc",body={"name":"kitty","age":10})queryoperationbyidqueryresult=es.get(index="python_es01",doc_type="doc",id=1)会有一个返回值fullsearchbody={"query":{"match_all":{}}}result=es.search(index="python_es01",body=body)useGETforid,其他搜索删除操作result=es.delete(index="goods",doc_type="type1",id=2)按查询结果删除result=es.delete_by_query(index="goods",body=body)buildmappingbody={"mappings":{"properties":{"name":{"type":"text"},"price":{"type":"long"}}}}result=es.indices.create(index="shang",body=body)文章同步发布:https://www.geek-share.com/detail/2783476700.html