python自动生成pg数据库表对应的es索引

项目需要用到Es进行查询。手动创建Es索引太麻烦,写了个脚本。

首先需要安装两个库

复制代码
pip install psycopg2

我使用的es版本是7.10的安装对应版本的elasticsearch库

复制代码
pip install elasticsearch==7

以下是生成索引代码

复制代码
import psycopg2  # 导入psycopg2驱动程序
from elasticsearch import Elasticsearch

# 连接到Elasticsearch
es = Elasticsearch(["http://192.168.1.2:9210"])
index_mapping = {
    'mappings': {
        'properties': {}
    }
}


def getcolumns(table):
    # 创建数据库连接
    cnx = psycopg2.connect(
        host='192.168.1.26',
        port='5432',
        database='test',
        user='postgres',
        password='******'
    )

    # 创建游标
    cursor = cnx.cursor()
    # 执行查询语句
    query = f"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{table}'"
    cursor.execute(query)

    # 获取结果
    result = cursor.fetchall()

    # 关闭游标和连接
    cursor.close()
    return result


def get_es_type(data_type):
    if data_type == 'integer':
        return 'integer'
    elif data_type == 'bigint':
        return 'long'
    elif data_type == 'numeric':
        return 'float'
    elif data_type == 'character varying' or data_type == 'text':
        return 'text'
    elif data_type == 'boolean':
        return 'boolean'
    elif data_type == 'timestamp with time zone' or data_type == 'timestamp without time zone':
        return 'date'
    elif data_type == 'bytea':
        return 'binary'
    else:
        return 'keyword'  # 默认使用keyword类型


# 创建Es索引
def create_index(table_name, index_name):
    result = getcolumns(table_name)

    # 添加字段映射
    for column_name, data_type in result:
        es_type = get_es_type(data_type)
        index_mapping['mappings']['properties'][column_name] = {'type': es_type}
        if es_type == 'text' or es_type == 'keyword':
            index_mapping['mappings']['properties'][column_name] = {
                'type': es_type,
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
    # 使用indices.exists()方法判断Index是否存在
    if not es.indices.exists(index=index_name):
        es.indices.create(index=index_name, body=index_mapping)
        print(f'索引{index_name}创建成功。')
    else:
        print(f'索引{index_name}已存在,无需创建。')


# 需要创建索引的表
indexlist = [
    {
        'table_name': 'pg_table1',
        'index_name': 'es_index1'
    },
    {
        'table_name': 'pg_table2',
        'index_name': 'es_index2'
    },
    {
        'table_name': 'pg_table3',
        'index_name': 'es_index3'
    },
    {
        'table_name': 'pg_table4',
        'index_name': 'es_index4'
    }
]

for indexinfo in indexlist:
    table_name = indexinfo['table_name']
    index_name = indexinfo['index_name']
    create_index(table_name, index_name)
相关推荐
huihui45013 分钟前
一天一道Sql题(day01)
数据库
~尼卡~14 分钟前
软考(软件设计师)数据库原理:事务管理,备份恢复,并发控制
数据库·软件设计师-软考
八九燕来22 分钟前
Django双下划线查询
数据库·django·sqlite
ahead~1 小时前
【大模型入门】访问GPT_API实战案例
人工智能·python·gpt·大语言模型llm
眠りたいです1 小时前
Mysql常用内置函数,复合查询及内外连接
linux·数据库·c++·mysql
paopaokaka_luck1 小时前
智能推荐社交分享小程序(websocket即时通讯、协同过滤算法、时间衰减因子模型、热度得分算法)
数据库·vue.js·spring boot·后端·websocket·小程序
大模型真好玩2 小时前
准确率飙升!GraphRAG如何利用知识图谱提升RAG答案质量(额外篇)——大规模文本数据下GraphRAG实战
人工智能·python·mcp
19892 小时前
【零基础学AI】第30讲:生成对抗网络(GAN)实战 - 手写数字生成
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·近邻算法
applebomb2 小时前
没合适的组合wheel包,就自行编译flash_attn吧
python·ubuntu·attention·flash
He.ZaoCha2 小时前
函数-1-字符串函数
数据库·sql·mysql