一、Python操作ES之基本使用
安装:pip3 install elasticsearch
from elasticsearch import Elasticsearch
obj = Elasticsearch()
# 创建索引(Index)
# result = obj.indices.create(index='user',ignore=400)
#
# print(result)
# print(result)
# 删除索引
# result = obj.indices.delete(index='user')
# print(result)
# 插入数据
# data = {'userid': '1', 'username': 'lqz','password':'123'}
# data = {'userid': '2', 'username': 'egon','password':'123','hobby':'篮球'}
# result = obj.create(index='news', doc_type='_doc', id=2, body=data)
# print(result)
# 更新数据
'''
不用doc包裹会报错
ActionRequestValidationException[Validation Failed: 1: script or doc is missing
'''
# data ={'doc':{'userid': '1', 'username': 'lqz','password':'123ee','test':'test'}}
# result = obj.update(index='news', doc_type='_doc', body=data, id=1)
# print(result)
# 删除数据
# result = obj.delete(index='news', doc_type='_doc', id=1)
# print(result)
# 查询
# 查找所有文档
# query = {'query': {'match_all': {}}}
# 查找名字叫做jack的所有文档
query = {
'query': {
'term': {
'from': 'gu'}}}
# 查找年龄大于11的所有文档
# query = {'query': {'range': {'age': {'gt': 11}}}}
#
allDoc = obj.search(index='lqz', body=query) # 7.x以后,只有一个表了
# print(allDoc)

本文介绍了Python操作Elasticsearch的基本和高级使用,重点讲解了如何进行集群搭建,包括基于广播和单播的方式,并讨论了集群可能出现的脑裂问题及其解决方案,同时提到了Elasticsearch的打分机制。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



