成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

python中elasticsearch如何創(chuàng)建索引并寫入數(shù)據(jù)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)python中elasticsearch如何創(chuàng)建索引并寫入數(shù)據(jù),小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)專注于尼金平企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設公司,電子商務商城網(wǎng)站建設。尼金平網(wǎng)站建設公司,為尼金平等地區(qū)提供建站服務。全流程按需搭建網(wǎng)站,專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

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索引類型對應的數(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ù)

錯誤的寫入

python中elasticsearch如何創(chuàng)建索引并寫入數(shù)據(jù)

正確的寫入

python中elasticsearch如何創(chuàng)建索引并寫入數(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)

關(guān)于“python中elasticsearch如何創(chuàng)建索引并寫入數(shù)據(jù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前名稱:python中elasticsearch如何創(chuàng)建索引并寫入數(shù)據(jù)-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://jinyejixie.com/article18/jsedp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設、App設計、虛擬主機網(wǎng)站建設、建站公司全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設網(wǎng)站維護公司
桐城市| 万盛区| 吴旗县| 宁城县| 小金县| 济阳县| 秦皇岛市| 徐水县| 锦屏县| 无棣县| 措美县| 武隆县| 新竹县| 光山县| 灌阳县| 遵化市| 凭祥市| 静宁县| 定陶县| 噶尔县| 伊宁县| 漳州市| 黎平县| 吐鲁番市| 新泰市| 宁陕县| 奇台县| 马公市| 墨竹工卡县| 京山县| 太康县| 明光市| 阜南县| 财经| 江西省| 平原县| 方正县| 象州县| 博野县| 大姚县| 沈丘县|