Python中集成es两种方式

1.第一种原生集成

1.1安装

pip3 install elasticsearch -i https://pypi.douban.com/simple

1.2实例化对象

from elasticsearch import Elasticsearch

obj = Elasticsearch()  # 得到一个对象

1.3 创建索引

# 创建索引(Index)
# index:索引名称,ignore:忽略400的报错
result = obj.indices.create(index='user', ignore=400)
print(result)

1.4 删除索引

result = obj.indices.delete(index='user', ignore=[400, 404])

1.5 插入单数据


data = {'userid': '1', 'username': 'ymq', 'password': '123'}
# index:插入的索引名称,doc_type:索引类型默认就是_doc ,id:唯一标识,body:插入的数据
result = obj.create(index='user', doc_type='_doc', id=4, body=data)
print(result)

1.6 批量插入

1. 这样可以插入不同的索引及其类型,也可以手动指定_id

2. _type:代表document属于index中的哪个类别(type)

3 .必须两个为一个组合,前者为索引设置,后者为数据

doc = [
    {'index': {'_index': 'user', '_type': '_doc', '_id': 1}},
    {'userid': '1', 'username': '刘备', 'password': 'male', 'birthday': '1995-01-01', 'salary': 8000},

    {'index': {'_index': 'user', '_type': '_doc', '_id': 2}},
    {'userid': '1', 'username': '张飞', 'password': 'male', 'birthday': '1995-01-01', 'salary': 8000},

    {'index': {'_index': 'user', '_type': '_doc', '_id': 3}},
    {'userid': '1', 'username': '张飞', 'password': 'male', 'birthday': '1995-01-01', 'salary': 8000},
]
result = obj.bulk(index='user', doc_type='_doc', body=doc)
print(result)

1.7 更新数据

# 修改数据,如果原有的字段没有,提交修改的数据有,自动忽略,只修改原有的字段
data = {'doc': {'userid': '1', 'username': 'lqz', 'password': '123ee', 'test': 'test', 'name': 'dbj'}}
result = obj.update(index='user', doc_type='_doc', body=data, id=1)
print(result)

1.8 删除数据

result = obj.delete(index='user', doc_type='_doc', id=1)
print(result)

1.9 查询

# 查找所有文档
# query = {'query': {'match_all': {}}}
#  查找名字叫做jack的所有文档
# query = {'query': {'match': {'username': '张飞'}}}

# 查找salary大于5000的所有文档
query = {'query': {'range': {'salary': {'gt': 5000}}}}

allDoc = obj.search(index='user', doc_type='_doc', body=query)

print(allDoc)
print(allDoc['hits']['hits'][0]['_source'])

2. dsl

2.1安装

pip3 install elasticsearch-dsl  -i https://pypi.douban.com/simple

2.2 使用 

# Elasticsearch DSL is a high-level

# pip3 install elasticsearch-dsl  -i https://pypi.douban.com/simple


from datetime import datetime
from elasticsearch_dsl import Document, Date, Nested, Boolean, analyzer, InnerDoc, Completion, Keyword, Text, Integer

from elasticsearch_dsl.connections import connections

connections.create_connection(hosts=["localhost"])


class Article(Document):
    title = Text(analyzer='ik_max_word')
    author = Text()

    class Index:
        # 给索引起个名称
        name = 'myindex'

    def save(self, **kwargs):
        return super(Article, self).save(**kwargs)


if __name__ == '__main__':
    # Article.init()  # 创建索引
    # 保存数据
    # article = Article()
    # article.title = "测试测试阿斯顿发送到发斯蒂芬啊啊士大夫阿斯蒂芬"
    # article.author = "lqz"
    # article.save()  # 数据就保存了

    # 查询数据
    # s=Article.search()
    # s = s.filter('match', title="测试")
    #
    # results = s.execute()  # 执行
    # print(results[0].title)

    # 删除数据
    s = Article.search()
    s = s.filter('match', title="李清照").delete()

    # 修改数据
    # s = Article().search()
    # s = s.filter('match', title="测试")
    # results = s.execute()
    # print(results[0])
    # results[0].title="李清照阿斯顿发送到发送阿斯蒂"
    # results[0].save()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑猪去兜风z1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值