python elasticsearch從創(chuàng)建索引到寫(xiě)入數(shù)據(jù)的全過(guò)程
python elasticsearch從創(chuàng)建索引到寫(xiě)入數(shù)據(jù)
創(chuàng)建索引
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不會(huì)進(jìn)行分詞,text會(huì)分詞
"index": "false" # 不建索引
},
#tags可以存json格式,訪問(wèn)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)
通過(guò)以上代碼即可創(chuàng)建es索引
寫(xiě)入一條數(shù)據(jù)
寫(xiě)入數(shù)據(jù)需要根據(jù) 創(chuàng)建的es索引類型對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu)寫(xiě)入:
from elasticsearch import Elasticsearch
es = Elasticsearch('192.168.1.1:9200')
action ={
"id": "1111122222",
"serial":"版本",
#以下tags.content是錯(cuò)誤的寫(xiě)法
#"tags.content" :"標(biāo)簽2",
#"tags.dominant_color_name": "域名的顏色黃色",
#正確的寫(xiě)法如下:
"tags":{"content":"標(biāo)簽3","dominant_color_name": "域名的顏色黃色"},
#按照字典的格式寫(xiě)入,如果用上面的那種寫(xiě)法,會(huì)直接寫(xiě)成一個(gè)tags.content字段。
#而不是在tags中content添加數(shù)據(jù),這點(diǎn)需要注意
"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)
即可寫(xiě)入一條數(shù)據(jù)
錯(cuò)誤的寫(xiě)入

正確的寫(xiě)入

寫(xiě)入多條數(shù)據(jù)
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" :"標(biāo)簽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" :"標(biāo)簽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)
這個(gè)方式是手動(dòng)指定了id,如果把”_id”這個(gè)參數(shù)去掉即可自動(dòng)生成id數(shù)據(jù).
如下:
action2 ={
"_index": "indes_test",
"_type": "doc_type_test",
"_source":{
"id": "1111122222",
"serial":"版本",
"tags.content" :"標(biāo)簽2",
"tags.dominant_color_name": "域名的顏色黃色",
"tags.skill":"分類信息",
"hasTag":"123",
"status":"11",
"createTime" :"2018-2-2",
"updateTime":"2018-2-3",
}
}
刪除一條數(shù)據(jù)
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
查詢一條數(shù)據(jù)
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
查詢所有數(shù)據(jù)
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'])
通過(guò)['hits']參數(shù),可以解析出查詢數(shù)據(jù)的詳細(xì)內(nèi)容
根據(jù)關(guān)鍵詞查找
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)
總結(jié)
所述是小編給大家介紹的python elasticsearch從創(chuàng)建索引到寫(xiě)入數(shù)據(jù)的全過(guò)程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Python比較文件夾比另一同名文件夾多出的文件并復(fù)制出來(lái)的方法
這篇文章主要介紹了Python比較文件夾比另一同名文件夾多出的文件并復(fù)制出來(lái)的方法,涉及Python針對(duì)文件與文件夾的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
python 常見(jiàn)的排序算法實(shí)現(xiàn)匯總
這篇文章主要介紹了python 常見(jiàn)的排序算法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-08-08
python 實(shí)現(xiàn)二叉搜索樹(shù)的四種方法
本文主要介紹了python 實(shí)現(xiàn)二叉搜索樹(shù)的四種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Django中的CACHE_BACKEND參數(shù)和站點(diǎn)級(jí)Cache設(shè)置
這篇文章主要介紹了Django中的CACHE_BACKEND參數(shù)和站點(diǎn)級(jí)Cache設(shè)置,Python是最具人氣的Python web框架,需要的朋友可以參考下2015-07-07
手把手教你使用TensorFlow2實(shí)現(xiàn)RNN
本文主要介紹了TensorFlow2實(shí)現(xiàn)RNN,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
一起解密Python中的*args和**kwargs無(wú)限可能的函數(shù)參數(shù)
這篇文章主要來(lái)跟大家一起解密Python中的*args和**kwargs無(wú)限可能的函數(shù)參數(shù)使用的靈活性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Python網(wǎng)絡(luò)編程之socket與socketserver
這篇文章介紹了Python網(wǎng)絡(luò)編程之socket與socketserver,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Python實(shí)現(xiàn)微信小程序自動(dòng)操作工具
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)微信小程序自動(dòng)化操作的小工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01

