def doc_delete(self, index_name, doc_id):
res = self._client.delete(index=index_name, id=doc_id)
return res
参数
index_name:索引的名字
doc_id:文档的id
一共就这几个数据,我就不删除了
修改
def doc_update(self, index_name, doc_id, doc):
res = self._client.update(index=index_name, id=doc_id, body=doc)
return res
参数
index_name:索引的名字
doc_id:文档的id
doc:文档
测试代码
def test_doc_update(self):
sun = {
"doc": {
"age": 21
}
}
res = es.doc_update("heros", "1002", sun)
print(res["result"])
结果
查询
查询所有
def doc_get__all(self, index_name):
res = self._client.search(index=index_name)
return res['hits']['hits']
参数
index_name:索引的名字
这里仅返回hit到的json对象列表
测试代码
def test_doc_get_all(self):
res = es.doc_get__all("heros")
for hit in res:
print(hit["_source"])
结果
根据id查询
def doc_get_id(self, index_name, id_):
res = self._client.get(index=index_name, id=id_)
return res["_source"]
参数
index_name:索引的名字
id_:文档的id
测试代码
def test_doc_get_id(self):
res = es.doc_get_id("heros", "1002")
print(res)
结果
根据条件查询
def doc_get_condition(self, index_name, con):
res = self._client.search(index=index_name, body=con)
return res['hits']['hits']
参数
index_name:索引的名字
con:查询的请求体,即条件
单个条件
测试代码
def test_doc_get_con_1(self):
name = {
"name": "小云"
}
con = {
"query": {
}
}
con["query"]["match"] = name
res = es.doc_get_condition("heros", con)
for hit in res:
print(hit["_source"])
结果
因为name在前面设置映射时,设置为了text,所以即使不完全匹配也会有类似的结果。
分页查询
测试代码
def test_doc_get_page(self):
page_num = 2
size = 3
con = {
"from": (page_num - 1) * size,
"size": size
}
res = es.doc_get_condition("heros", con)
for hit in res:
print(hit["_source"])
结果
对结果排序
测试代码
def test_doc_get_sort(self):
page_num = 1
size = 3
field = "age"
order = "desc"
con = {
"from": (page_num - 1) * size,
"size": size,
"sort": {
field: {
"order": order
}
}
}
res = es.doc_get_condition("heros", con)
for hit in res:
print(hit["_source"])
结果
多个条件查询
测试代码
def test_doc_get_con_more(self):
eighteen_match = {
"match": {
"age": "18"
}
}
daye_match = {
"match": {
"role": "打野"
}
}
con = {
"query": {
"bool": {
"must_not": []
}
}
}
con["query"]["bool"]["must_not"].append(eighteen_match)
con["query"]["bool"]["must_not"].append(daye_match)
res = es.doc_get_condition("heros", con)
for hit in res:
print(hit["_source"])
结果
范围查询
测试代码
def test_doc_get_scope(self):
filter = {
"range": {
"age": {
"gt": 19
}
}
}
con = {
"query": {
"bool": {
"filter": filter
}
}
}
res = es.doc_get_condition("heros", con)
for hit in res:
print(hit["_source"])
结果
完全匹配
测试代码
def test_doc_get_phrase(self):
name = {
"name": "赵云"
}
con = {
"query": {
}
}
con["query"]["match_phrase"] = name
res = es.doc_get_condition("heros", con)
for hit in res:
print(hit["_source"])
结果
高亮查询
测试代码
def test_doc_get_highlight(self):
name = {
"name": "云"
}
con = {
"query": {
},
"highlight": {
"fields": {
"name": {}
}
}
}
con["query"]["match"] = name
res = es.doc_get_condition("heros", con)
for hit in res:
print(hit["highlight"])
结果
聚合查询
def doc_get__agg(self, index_name, con):
res = self._client.search(index=index_name, body=con)
return res['aggregations']
参数
index: 索引的名字
body:请求体,即聚合的字段
测试代码
def test_doc_get_aggs(self):
con = {
"aggs": {
"role_groups": {
"terms": {
"field": "role"
}
}
},
}
res = es.doc_get__agg("heros", con)
for cnt in res["role_groups"]["buckets"]:
print(cnt["key"], " ", cnt["doc_count"])