# zdppy_elasticsearch库使用
项目地址
GitHub开源地址:https://github.com/zhangdapeng520/zdppy_elasticsearch
主要看dev分支的代码。
# 版本历史
- 2023/01/30 v1.0.1 使用zdppy_requests完全重构项目
# 快速入门
## 安装
```bash
pip install zdppy_elasticsearch
```
## 创建Mapping
```python
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
```python
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
print(es.get_mapping(index))
```
## 删除Mapping
```python
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
print(es.delete_index(index))
```
## 根据ID新增数据
```python
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查询图书
```python
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删除图书
```python
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))
```
## 批量插入数据
```python
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))
```
## 搜索所有图书
```python
import zdppy_elasticsearch as ze
es = ze.ElasticSearch(password="zhangdapeng520")
index = "books"
# 搜索所有图书
print(es.search(index))
```
## 搜索特定价格范围的图书
```python
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))
```
Raw data
{
"_id": null,
"home_page": "https://github.com/zhangdapeng520/zdppy_elasticsearch",
"name": "zdppy-elasticsearch",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9,<4.0",
"maintainer_email": "",
"keywords": "zdppy,ElasticSearch,search,zhangdapeng",
"author": "\u5f20\u5927\u9e4f",
"author_email": "1156956636@qq.com",
"download_url": "https://files.pythonhosted.org/packages/fb/60/e824f157a3fe2a861ecb3c54ebb542ef759f3d68e873a7a8319034f316db/zdppy_elasticsearch-1.0.1.tar.gz",
"platform": null,
"description": "# zdppy_elasticsearch\u5e93\u4f7f\u7528\n\u9879\u76ee\u5730\u5740\nGitHub\u5f00\u6e90\u5730\u5740\uff1ahttps://github.com/zhangdapeng520/zdppy_elasticsearch\n\n\u4e3b\u8981\u770bdev\u5206\u652f\u7684\u4ee3\u7801\u3002\n\n# \u7248\u672c\u5386\u53f2\n- 2023/01/30 v1.0.1 \u4f7f\u7528zdppy_requests\u5b8c\u5168\u91cd\u6784\u9879\u76ee\n\n# \u5feb\u901f\u5165\u95e8\n## \u5b89\u88c5\n```bash\npip install zdppy_elasticsearch\n```\n\n## \u521b\u5efaMapping\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nbody = {\n \"mappings\": {\n \"properties\": {\n \"name\": {\n \"type\": \"text\"\n },\n \"price\": {\n \"type\": \"double\"\n },\n \"author\": {\n \"type\": \"text\"\n },\n \"pub_date\": {\n \"type\": \"date\"\n }\n }\n }\n}\nindex = \"books\"\nprint(es.add_mapping(index, body))\n```\n\n## \u67e5\u8be2Mapping\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nindex = \"books\"\nprint(es.get_mapping(index))\n```\n\n## \u5220\u9664Mapping\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nindex = \"books\"\nprint(es.delete_index(index))\n```\n\n## \u6839\u636eID\u65b0\u589e\u6570\u636e\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nindex = \"books\"\ndid = 1\nbody = {\n\t\"name\": \"\u300aJavaScript\u5168\u6808\u5f00\u53d1\u5b9e\u6218\u300b\",\n\t\"author\": \"\u5f20\u5927\u9e4f\",\n\t\"price\": 123,\n\t\"pub_date\": \"2019-12-12\"\n}\nprint(es.add(index, did, body))\n```\n\n## \u6839\u636eID\u67e5\u8be2\u56fe\u4e66\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nindex = \"books\"\ndid = 1\nprint(es.get(index, did))\nprint(es.get(index, did, is_source=True))\n```\n\n## \u6839\u636eID\u5220\u9664\u56fe\u4e66\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nindex = \"books\"\ndid = 1\nprint(es.delete(index, did))\nprint(es.get(index, did, is_source=True))\n```\n\n## \u6279\u91cf\u63d2\u5165\u6570\u636e\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nindex = \"books\"\ndata = [\n\t{\"index\": {\"_index\": \"books\", \"_type\" : \"_doc\", \"_id\" : \"1\"}},\n\t{\"name\": \"\u300aJavaScript\u5168\u6808\u5f00\u53d1\u5b9e\u6218\u300b\", \"author\": \"\u5f20\u5927\u9e4f\", \"price\": 123, \"pub_date\": \"2019-12-12\" },\n\t{\"index\": {\"_index\": \"books\", \"_type\" : \"_doc\", \"_id\" : \"2\"}},\n\t{\"name\": \"\u300aReact\u5b66\u4e60\u624b\u518c\u300b\", \"author\": \"\u5f20\u5927\u9e4f\", \"price\": 122, \"pub_date\": \"2019-12-12\" },\n\t{\"index\": {\"_index\": \"books\", \"_type\" : \"_doc\", \"_id\" : \"3\",}},\n\t{\"name\": \"\u300a\u7cbe\u901aGo\u8bed\u8a00\u300b\", \"price\": 128, \"pub_date\": \"2019-12-12\" }\n]\n\ndata1 = [\n\t{\"name\": \"\u300aJavaScript\u5168\u6808\u5f00\u53d1\u5b9e\u6218\u300b\", \"author\": \"\u5f20\u5927\u9e4f\", \"price\": 123, \"pub_date\": \"2019-12-12\" },\n\t{\"name\": \"\u300aReact\u5b66\u4e60\u624b\u518c\u300b\", \"author\": \"\u5f20\u5927\u9e4f\", \"price\": 122, \"pub_date\": \"2019-12-12\" },\n\t{\"name\": \"\u300a\u7cbe\u901aGo\u8bed\u8a00\u300b\", \"price\": 128, \"pub_date\": \"2019-12-12\" }\n]\n\n\n# \u81ea\u5b9a\u4e49\u7d22\u5f15\u548c\u548cID\nprint(es.add_many(data))\n\n# \u81ea\u52a8\u751f\u6210ID\nprint(es.add_many(data1, index=index))\n```\n\n## \u641c\u7d22\u6240\u6709\u56fe\u4e66\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nindex = \"books\"\n\n# \u641c\u7d22\u6240\u6709\u56fe\u4e66\nprint(es.search(index))\n```\n\n## \u641c\u7d22\u7279\u5b9a\u4ef7\u683c\u8303\u56f4\u7684\u56fe\u4e66\n```python\nimport zdppy_elasticsearch as ze\n\nes = ze.ElasticSearch(password=\"zhangdapeng520\")\nindex = \"books\"\nquery = {\n \"query\": {\n \"range\": {\n \"price\": {\n \"gt\": 123,\n \"lte\": 130\n }\n }\n }\n}\n\n# \u641c\u7d22\u6240\u6709\u56fe\u4e66\nprint(es.search(index, query))\n```",
"bugtrack_url": null,
"license": "MIT",
"summary": "python\u64cd\u4f5cElasticSearch\u7684\u57fa\u7840\u7ec4\u4ef6",
"version": "1.0.1",
"split_keywords": [
"zdppy",
"elasticsearch",
"search",
"zhangdapeng"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ccca5509e886304d02208a3cd60932e92d8b337d6dffa92c8151dd644944a441",
"md5": "826f1a0de9d1d6072dd02b4ca1d80a3b",
"sha256": "c38cce7145d56db2275d9217de294a30748809e7d4f88301798381a05c3eb775"
},
"downloads": -1,
"filename": "zdppy_elasticsearch-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "826f1a0de9d1d6072dd02b4ca1d80a3b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9,<4.0",
"size": 4489,
"upload_time": "2023-01-30T08:20:07",
"upload_time_iso_8601": "2023-01-30T08:20:07.210569Z",
"url": "https://files.pythonhosted.org/packages/cc/ca/5509e886304d02208a3cd60932e92d8b337d6dffa92c8151dd644944a441/zdppy_elasticsearch-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fb60e824f157a3fe2a861ecb3c54ebb542ef759f3d68e873a7a8319034f316db",
"md5": "37412f4863d2a34b3a28aeca4796fd26",
"sha256": "ee23593e40f6a0932f1f93b59bc590afd6652b4febc4d2c4147c50c6ec8947f1"
},
"downloads": -1,
"filename": "zdppy_elasticsearch-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "37412f4863d2a34b3a28aeca4796fd26",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9,<4.0",
"size": 4374,
"upload_time": "2023-01-30T08:20:08",
"upload_time_iso_8601": "2023-01-30T08:20:08.626076Z",
"url": "https://files.pythonhosted.org/packages/fb/60/e824f157a3fe2a861ecb3c54ebb542ef759f3d68e873a7a8319034f316db/zdppy_elasticsearch-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-30 08:20:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "zhangdapeng520",
"github_project": "zdppy_elasticsearch",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "zdppy-elasticsearch"
}