亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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不會進行分詞,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)

通過以上代碼即可創(chuàng)建es索引

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

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

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添加數(shù)據(jù),這點需要注意
       "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ù)

錯誤的寫入

這里寫圖片描述

正確的寫入

這里寫圖片描述

寫入多條數(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" :"標簽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”這個參數(shù)去掉即可自動生成id數(shù)據(jù).
如下:

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",
          }
        }

刪除一條數(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'])

通過['hits']參數(shù),可以解析出查詢數(shù)據(jù)的詳細內(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ù)的全過程,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

相關(guān)文章

  • Python實現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度)

    Python實現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度)

    這篇文章主要介紹了Python實現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 怎樣用cmd命令行運行Python文件

    怎樣用cmd命令行運行Python文件

    這篇文章主要介紹了怎樣用cmd命令行運行Python文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 使用Python進行自動化部署詳解

    使用Python進行自動化部署詳解

    在軟件開發(fā)和運維領(lǐng)域,自動化部署是一個至關(guān)重要的環(huán)節(jié),本文將介紹如何使用Python進行自動化部署,并提供代碼實例來說明,希望對大家有所幫助
    2024-04-04
  • python字符串拼接的7種方法及性能比較詳解

    python字符串拼接的7種方法及性能比較詳解

    這篇文章主要介紹了python字符串拼接的7種方法詳解,需要的朋友可以參考下
    2021-04-04
  • Django websocket原理及功能實現(xiàn)代碼

    Django websocket原理及功能實現(xiàn)代碼

    這篇文章主要介紹了Django websocket原理及功能實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Python檢查和同步本地時間(北京時間)的實現(xiàn)方法

    Python檢查和同步本地時間(北京時間)的實現(xiàn)方法

    這篇文章主要介紹了Python檢查和同步本地時間(北京時間)的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • python實現(xiàn)信號時域統(tǒng)計特征提取代碼

    python實現(xiàn)信號時域統(tǒng)計特征提取代碼

    今天小編就為大家分享一篇python實現(xiàn)信號時域統(tǒng)計特征提取代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • scrapy處理python爬蟲調(diào)度詳解

    scrapy處理python爬蟲調(diào)度詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于scrapy處理python爬蟲調(diào)度的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2020-11-11
  • 在Python中使用元類的教程

    在Python中使用元類的教程

    這篇文章主要介紹了在Python中使用元類的教程,是Python當中的基礎(chǔ)知識,代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • pycharm打包python項目為exe執(zhí)行文件的實例代碼

    pycharm打包python項目為exe執(zhí)行文件的實例代碼

    這篇文章主要介紹了pycharm打包python項目為exe執(zhí)行文件,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07

最新評論