您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關python中elasticsearch如何創建索引并寫入數據,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
python elasticsearch從創建索引到寫入數據
創建索引
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)
通過以上代碼即可創建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)
即可寫入一條數據
錯誤的寫入
正確的寫入
寫入多條數據
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)
這個方式是手動指定了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", } }
刪除一條數據
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
查詢一條數據
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
查詢所有數據
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']參數,可以解析出查詢數據的詳細內容
根據關鍵詞查找
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)
關于“python中elasticsearch如何創建索引并寫入數據”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。