日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 1801 0

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格式,訪問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是錯(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ò)誤的寫入

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

正確的寫入

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

寫入多條數(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'])


          

通過['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ù)的全過程,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 丽水市| 汕头市| 大庆市| 碌曲县| 大方县| 通辽市| 枞阳县| 滁州市| 新泰市| 明水县| 周至县| 龙口市| 肃北| 灵川县| 镶黄旗| 修文县| 贞丰县| 喀喇沁旗| 兴仁县| 康保县| 湄潭县| 石嘴山市| 莆田市| 灌阳县| 海南省| 阿尔山市| 水城县| 自贡市| 专栏| 乌拉特前旗| 宝兴县| 丹寨县| 米林县| 图木舒克市| 甘南县| 玉屏| 大关县| 平阳县| 中西区| 临朐县| 桃园市|