#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020-07-02 21:46
# @Author  : zhangyunfei
# @File    : create-es-index.py
# @Software: PyCharm
from elasticsearch import Elasticsearch


def create_index():
    '''
     创建索引,创建索引名称为tx_ic_bigdata_business_heming_index_01，es7没有类型
    '''
    # 创建映射
    mappings = {
        "settings": {
            "number_of_shards": 5,
            "number_of_replicas": 1
        },
        "mappings": {
            "properties": {
                "id": {
                    "type": "integer"
                },
                "company_name": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    },
                    "analyzer": "ik_max_word"
                },
                "company_name_standard": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    },
                    "analyzer": "standard"
                }
            }
        }
    }

    es = Elasticsearch(['9.223.8.84'], http_auth=("elastic", "Brg2020!"), port=9200)

    if es.indices.exists(index="tx_ic_bigdata_business_heming_index_01"):
        print("索引已存在，正在删除----", )
        es.indices.delete(index="tx_ic_bigdata_business_heming_index_01")
    if es.indices.exists(index="tx_ic_bigdata_business_heming_index_01") is not True:
        print("创建索引------")
        res = es.indices.create(index='tx_ic_bigdata_business_heming_index_01', body=mappings)
        print("成功创建索引------", res)

create_index()
