python實現(xiàn)創(chuàng)建elasticsearch索引和Type

環(huán)境:
python3.5
支持包:
pymysql
elasticsearch_dsl

安裝 elasticsearch_dsl

pip install elasticsearch_dsl

在elasticsearch中建立一個索引及type

(索引類似于:關(guān)系數(shù)據(jù)庫中的數(shù)據(jù)庫;type類似于:關(guān)系數(shù)據(jù)庫中的表table)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/18 下午9:06
# @Author  : lee
# @File    : es_types.py
# @Version : 1.0
# 說明: 在elasticsearch中建立一個索引及type
from datetime import datetime
from elasticsearch_dsl import DocType, Date, Keyword, Text, Integer


# 配置hosts ip
from elasticsearch_dsl.connections import connections
connections.create_connection(hosts=["localhost"])

class ZukerType(DocType):
    # 房屋的數(shù)據(jù)
    """
    'title' :'名稱',
    'price':'價格',
    'create_date':'時間',
    'desc':'介紹',
    'area':'位置',
    'longitude':'經(jīng)度',
    'latitude':'維度',
    'url': 'url',
    """

    # 建立 索引和doc
    title = Text(analyzer="ik_max_word")
    price = Integer()
    create_date = Date()
    desc = Text(analyzer="ik_max_word")
    area = Text(analyzer="ik_max_word")
    longitude = float()
    latitude = float()
    url = Keyword()

    # 類似于django
    class Meta:
        index = 'zuker' # 索引名稱
        doc_type = '58house_info' # type 類似數(shù)據(jù)庫中的表[table]

if __name__ == "__main__":
    ZukerType.init()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容