最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python elasticsearch從創(chuàng)建索引到寫入數(shù)據(jù)的全過(guò)程

 更新時(shí)間:2019年08月04日 14:35:01   作者:diyiday  
這篇文章主要介紹了python elasticsearch從創(chuàng)建索引到寫入數(shù)據(jù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

python elasticsearch從創(chuàng)建索引到寫入數(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索引

寫入一條數(shù)據(jù)

寫入數(shù)據(jù)需要根據(jù) 創(chuàng)建的es索引類型對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu)寫入:

from elasticsearch import Elasticsearch
es = Elasticsearch('192.168.1.1:9200')
action ={
       "id": "1111122222",
       "serial":"版本",
       #以下tags.content是錯(cuò)誤的寫法
       #"tags.content" :"標(biāo)簽2",
       #"tags.dominant_color_name": "域名的顏色黃色",
       #正確的寫法如下:
       "tags":{"content":"標(biāo)簽3","dominant_color_name": "域名的顏色黃色"},
       #按照字典的格式寫入,如果用上面的那種寫法,會(huì)直接寫成一個(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)

即可寫入一條數(shù)據(jù)

錯(cuò)誤的寫入

這里寫圖片描述

正確的寫入

這里寫圖片描述

寫入多條數(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)建索引到寫入數(shù)據(jù)的全過(guò)程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

  • Python比較文件夾比另一同名文件夾多出的文件并復(fù)制出來(lái)的方法

    Python比較文件夾比另一同名文件夾多出的文件并復(fù)制出來(lái)的方法

    這篇文章主要介紹了Python比較文件夾比另一同名文件夾多出的文件并復(fù)制出來(lái)的方法,涉及Python針對(duì)文件與文件夾的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • python 常見(jiàn)的排序算法實(shí)現(xiàn)匯總

    python 常見(jiàn)的排序算法實(shí)現(xiàn)匯總

    這篇文章主要介紹了python 常見(jiàn)的排序算法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-08-08
  • python 實(shí)現(xiàn)二叉搜索樹的四種方法

    python 實(shí)現(xiàn)二叉搜索樹的四種方法

    本文主要介紹了python 實(shí)現(xiàn)二叉搜索樹的四種方法,文中通過(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è)置

    這篇文章主要介紹了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

    本文主要介紹了TensorFlow2實(shí)現(xiàn)RNN,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • pip無(wú)法安裝osgeo失敗的問(wèn)題解決

    pip無(wú)法安裝osgeo失敗的問(wèn)題解決

    本文主要介紹了pip無(wú)法安裝osgeo失敗的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • 一起解密Python中的*args和**kwargs無(wú)限可能的函數(shù)參數(shù)

    一起解密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

    這篇文章介紹了Python網(wǎng)絡(luò)編程之socket與socketserver,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • Pytorch模型參數(shù)的保存和加載

    Pytorch模型參數(shù)的保存和加載

    pytorch中state_dict()和load_state_dict()函數(shù)配合使用可以實(shí)現(xiàn)狀態(tài)的獲取與重載,load()和save()函數(shù)配合使用可以實(shí)現(xiàn)參數(shù)的存儲(chǔ)與讀取,這篇文章主要介紹了Pytorch模型參數(shù)的保存和加載,需要的朋友可以參考下
    2023-03-03
  • Python實(shí)現(xiàn)微信小程序自動(dòng)操作工具

    Python實(shí)現(xiàn)微信小程序自動(dòng)操作工具

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)微信小程序自動(dòng)化操作的小工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-01-01

最新評(píng)論

长岭县| 界首市| 榆树市| 大余县| 黄龙县| 黑龙江省| 平谷区| 安西县| 东安县| 冀州市| 和平区| 息烽县| 丘北县| 隆昌县| 砚山县| 鲁山县| 嘉禾县| 佛山市| 科技| 墨江| 盘锦市| 大庆市| 阜新市| 云阳县| 花垣县| 桐城市| 尉犁县| 三台县| 灌南县| 泸溪县| 泸州市| 广安市| 来宾市| 响水县| 咸宁市| 白城市| 铁岭县| 南汇区| 诸城市| 金川县| 平塘县|