iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 9
1
Elastic Stack on Cloud

Python&Elasticsearch 入門系列 第 9

IT鐵人第9天 Elasticsearch 使用python建立index(1)

  • 分享至 

  • twitterImage
  •  

今天要教大家如何用python操作es來建立第一個自己的index,在開始之前記得把前面幾篇的mapping教學看個透徹喔!

第1件事一定是先安裝套件,這邊使用python3做示範,大家可以自己選擇要不要用virtualenv切割虛擬環境,這邊就不對virtualenv多做教學,網路上有很多文章大家可以自行去google

1.安裝套件

pip3 install elasticsearch

2.建立es連接

from elasticsearch import Elasticsearch

# host跟port跟打上es的host跟port
es = Elasticsearch(hosts='192.168.1.59', port=9200)

這邊先用前面的mappings建立一個存放學生名單的index
3.定義mapping

{
    "mappings": {
        "properties": {
            "sid": {
                "type": "integer"
            },
            "name": {
                "type": "text"
            },
            "age": {
                "type": "integer"
            },
            "class": {
                "type": "keyword"
            }
        }
    }
}

4.設定settings

{
    "settings": {
        "index": {
            "number_of_shards": 3, #設定主分片
            "number_of_replicas": 1 #設定副本分片
        }
    }
}

ps.主分片只能在建立之前設定,副本分片之後還能再更改

5.打包body,整理成以下這樣,在python裡面直接用dict操作就可以了

{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 2
    },
    "mappings": {
        "properties": {
            "sid": {
                "type": "integer"
            },
            "name": {
                "type": "text"
            },
            "age": {
                "type": "integer"
            },
            "class": {
                "type": "keyword"
            }
        }
    }
}

操作es index時使用的是elasticsearch底下的indices這個class,建立index時使用的是indices底下create這個方法,看下面程式碼範例

#es是一開始建立的es連接
es.indices.create(index='index名稱', body=上面整理好的body)

都準備完了就可以執行來看看有沒有成功囉!

以下是完整的程式碼:

from elasticsearch import Elasticsearch
import json


def create_index(es):
    body = dict()
    body['settings'] = get_setting()
    body['mappings'] = get_mappings()
    print(json.dumps(body)) #可以用json.dumps輸出來看格式有沒又包錯
    es.indices.create(index='school_members', body=body)


def get_setting():
    settings = {
        "index": {
            "number_of_shards": 3,
            "number_of_replicas": 2
        }
    }

    return settings


def get_mappings():
    mappings = {
        "properties": {
            "sid": {
                "type": "integer"
            },
            "name": {
                "type": "text"
            },
            "age": {
                "type": "integer"
            },
            "class": {
                "type": "keyword"
            }
        }
    }

    return mappings


if __name__ == "__main__":
    es = Elasticsearch(hosts='192.168.1.59', port=9200)
    create_index(es)

下面再介紹一些常見方法

get: 回傳index信息

result = es.indices.get(index='school_members') #index指定要get哪個index的資訊
print(result)

https://ithelp.ithome.com.tw/upload/images/20200922/20129976YScgKgVQ5n.png

exists:查看index是否存在,回傳True/False

result = es.indices.exists(index='school_members')
print(result)

https://ithelp.ithome.com.tw/upload/images/20200922/20129976VkgeJR7jX8.png

今天的介紹到這邊結束,下篇會介紹如何重新put mappings跟index的別名機制


上一篇
IT鐵人第8天 Elasticsearch 認識Mapping(3) parent/child
下一篇
IT鐵人第10天 Elasticsearch 使用python建立index(2)
系列文
Python&Elasticsearch 入門30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言