#引入es库:
from elasticsearch import Elasticsearch
#链接到对应的es
es = Elasticsearch(host = "192.168.1.1",
http_auth=(username, password))
# 精准查询
def es_search_body(value, key):
"""
将传参封装为es查询的body,可根据实际需求,判断是否新增此函数
:param value:
:param key:
:return:
"""
body = {
"query": {
"match": {
key: value
}
}
}
return body
#使用es进行搜索
es.search(index=your_index, body=es_search_body(value, key))
#使用es进行删除
es.delete_by_query(index=your_index, body=es_search_body(value, key))
一些可能会用到的body
# bool 多条件查询
# gte 大于等于
# gt 大于
# lte是小于等于
# lt是小于
# 下面是查询学生姓名为小红,且成绩大于等于60的数据
GET index/_search
{
"query": {
"bool": {
"filter": [{
"match": {
"student": "小红"
}
},
{
"range": {
"grade": {
"gte": "60"
}
}
}
]
}
}
}