python操作ElasticSearch的基础组件
Project description
zdppy_elasticsearch库使用
项目地址 GitHub开源地址:https://github.com/zhangdapeng520/zdppy_elasticsearch
主要看dev分支的代码。
版本历史
- 2023/01/30 v1.0.1 使用zdppy_requests完全重构项目
快速入门
安装
pip install zdppy_elasticsearch
创建Mapping
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
body = {
"mappings": {
"properties": {
"name": {
"type": "text"
},
"price": {
"type": "double"
},
"author": {
"type": "text"
},
"pub_date": {
"type": "date"
}
}
}
}
index = "books"
print(es.add_mapping(index, body))
查询Mapping
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
print(es.get_mapping(index))
删除Mapping
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
print(es.delete_index(index))
根据ID新增数据
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
did = 1
body = {
"name": "《JavaScript全栈开发实战》",
"author": "张大鹏",
"price": 123,
"pub_date": "2019-12-12"
}
print(es.add(index, did, body))
根据ID查询图书
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
did = 1
print(es.get(index, did))
print(es.get(index, did, is_source=True))
根据ID删除图书
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
did = 1
print(es.delete(index, did))
print(es.get(index, did, is_source=True))
批量插入数据
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
data = [
{"index": {"_index": "books", "_type" : "_doc", "_id" : "1"}},
{"name": "《JavaScript全栈开发实战》", "author": "张大鹏", "price": 123, "pub_date": "2019-12-12" },
{"index": {"_index": "books", "_type" : "_doc", "_id" : "2"}},
{"name": "《React学习手册》", "author": "张大鹏", "price": 122, "pub_date": "2019-12-12" },
{"index": {"_index": "books", "_type" : "_doc", "_id" : "3",}},
{"name": "《精通Go语言》", "price": 128, "pub_date": "2019-12-12" }
]
data1 = [
{"name": "《JavaScript全栈开发实战》", "author": "张大鹏", "price": 123, "pub_date": "2019-12-12" },
{"name": "《React学习手册》", "author": "张大鹏", "price": 122, "pub_date": "2019-12-12" },
{"name": "《精通Go语言》", "price": 128, "pub_date": "2019-12-12" }
]
# 自定义索引和和ID
print(es.add_many(data))
# 自动生成ID
print(es.add_many(data1, index=index))
搜索所有图书
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
# 搜索所有图书
print(es.search(index))
搜索特定价格范围的图书
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
query = {
"query": {
"range": {
"price": {
"gt": 123,
"lte": 130
}
}
}
}
# 搜索所有图书
print(es.search(index, query))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file zdppy_elasticsearch-1.0.1.tar.gz
.
File metadata
- Download URL: zdppy_elasticsearch-1.0.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.9.13 Linux/5.15.0-58-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee23593e40f6a0932f1f93b59bc590afd6652b4febc4d2c4147c50c6ec8947f1 |
|
MD5 | 37412f4863d2a34b3a28aeca4796fd26 |
|
BLAKE2b-256 | fb60e824f157a3fe2a861ecb3c54ebb542ef759f3d68e873a7a8319034f316db |
File details
Details for the file zdppy_elasticsearch-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: zdppy_elasticsearch-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.9.13 Linux/5.15.0-58-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c38cce7145d56db2275d9217de294a30748809e7d4f88301798381a05c3eb775 |
|
MD5 | 826f1a0de9d1d6072dd02b4ca1d80a3b |
|
BLAKE2b-256 | ccca5509e886304d02208a3cd60932e92d8b337d6dffa92c8151dd644944a441 |