前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python-elasticsearch从创建索引到写入数据

python-elasticsearch从创建索引到写入数据

作者头像
双面人
发布2019-04-10 14:26:02
2.2K0
发布2019-04-10 14:26:02
举报
文章被收录于专栏:热爱IT

创建索引 from elasticsearch import Elasticsearch es = Elasticsearch('192.168.1.1:9200')

mappings = {             "mappings": {                 "type_doc_test": {                           #type_doc_test为doc_type                     "properties": {                         "id": {                             "type": "long",                             "index": "false"                         },                         "serial": {                             "type": "keyword",  # keyword不会进行分词,text会分词                             "index": "false"  # 不建索引                         },                         #tags可以存json格式,访问tags.content                         "tags": {                             "type": "object",                             "properties": {                                 "content": {"type": "keyword", "index": True},                                 "dominant_color_name": {"type": "keyword", "index": True},                                 "skill": {"type": "keyword", "index": True},                             }                         },                         "hasTag": {                             "type": "long",                             "index": True                         },                         "status": {                             "type": "long",                             "index": True                         },                         "createTime": {                             "type": "date",                             "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"                         },                         "updateTime": {                             "type": "date",                             "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"                         }                     }                 }             }         }

res = es.indices.create(index = 'index_test',body =mappings) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 通过以上代码即可创建es索引

写入一条数据 写入数据需要根据 创建的es索引类型对应的数据结构写入:

from elasticsearch import Elasticsearch

es = Elasticsearch('192.168.1.1:9200')

action ={               "id": "1111122222",               "serial":"版本",               #以下tags.content是错误的写法               #"tags.content" :"标签2",               #"tags.dominant_color_name": "域名的颜色黄色",               #正确的写法如下:               "tags":{"content":"标签3","dominant_color_name": "域名的颜色黄色"},               #按照字典的格式写入,如果用上面的那种写法,会直接写成一个tags.content字段。               #而不是在tags中content添加数据,这点需要注意               "tags.skill":"分类信息",               "hasTag":"123",               "status":"11",               "createTime" :"2018-2-2",               "updateTime":"2018-2-3",                 } es.index(index="index_test",doc_type="doc_type_test",body = action) 即可写入一条数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 错误的写入

正确的写入

写入多条数据 from elasticsearch import Elasticsearch from elasticsearch.helpers import bulk

es = Elasticsearch('192.168.1.1:9200')

ACTIONS = []

action1 ={                     "_index": "indes_test",                     "_type": "doc_type_test",                     "_id":"bSlegGUBmJ2C8ZCSC1R1",                     "_source":{                         "id": "1111122222",                         "serial":"版本",                         "tags.content" :"标签2",                         "tags.dominant_color_name": "域名的颜色黄色",                         "tags.skill":"分类信息",                         "hasTag":"123",                         "status":"11",                         "createTime" :"2018-2-2",                         "updateTime":"2018-2-3",                     }                 } action2 ={                     "_index": "indes_test",                     "_type": "doc_type_test",                     "_id":"bSlegGUBmJ2C8ZCSC1R2",                     "_source":{                         "id": "1111122222",                         "serial":"版本",                         "tags.content" :"标签2",                         "tags.dominant_color_name": "域名的颜色黄色",                         "tags.skill":"分类信息",                         "hasTag":"123",                         "status":"11",                         "createTime" :"2018-2-2",                         "updateTime":"2018-2-3",                     }                 }

ACTIONS.append(action1) ACTIONS.append(action2)

res,_ =bulk(es, ACTIONS, index="indes_test", raise_on_error=True) print(res) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 这个方式是手动指定了id,如果把”_id”这个参数去掉即可自动生成id数据.  如下:

action2 ={                     "_index": "indes_test",                     "_type": "doc_type_test",

                    "_source":{                         "id": "1111122222",                         "serial":"版本",                         "tags.content" :"标签2",                         "tags.dominant_color_name": "域名的颜色黄色",                         "tags.skill":"分类信息",                         "hasTag":"123",                         "status":"11",                         "createTime" :"2018-2-2",                         "updateTime":"2018-2-3",                     }                 } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 删除一条数据 from elasticsearch import Elasticsearch

es = Elasticsearch('192.168.1.1:9200')

res = es.delete(index="index_test",doc_type="doc_type_test", id ="bSlegGUBmJ2C8ZCSC1R1") print(res) 直接替换id的即可删除所需的id 1 2 3 4 5 6 7 查询一条数据 from elasticsearch import Elasticsearch

es = Elasticsearch('192.168.1.1:9200')

res = es.get(index="index_test",doc_type="doc_type_test",  id ="bSlegGUBmJ2C8ZCSC1R2") print(res) 直接替换id的即可查询所需的id 1 2 3 4 5 6 7 查询所有数据 from elasticsearch import Elasticsearch

es = Elasticsearch('192.168.1.1:9200')

res = es.search(index="index_test",doc_type="doc_type_test") print(res) print(res['hits']['hits'])

通过['hits']参数,可以解析出查询数据的详细内容 1 2 3 4 5 6 7 8 9 10 根据关键词查找

from elasticsearch import Elasticsearch

es = Elasticsearch('192.168.1.1:9200')

doc = {             "query": {                 "match": {                     "_id": "aSlZgGUBmJ2C8ZCSPVRO"                 }             }         }

res = es.search(index="index_test",doc_type="doc_type_test",body=doc) print(res)

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档