技术栈

python 3.11.8
neo4j  5.23.1

Python 脚本

# encoding: utf8
# author: qbit
# date: 2024-09-23
# summary: 清空 neo4j 全部索引

from neo4j import Record, GraphDatabase, RoutingControl

URI = "neo4j://192.168.1.97:7687"
AUTH = ("neo4j", "xxx")

def DropAllIndex():
    r" 删除所有索引 "

    with GraphDatabase.driver(URI, auth=AUTH) as driver:
        gql = "SHOW INDEX;"
        print(f"gql: {gql}")

        records, summary, keys = driver.execute_query(
            gql, database_="neo4j", routing_=RoutingControl.READ,
        )
        indexList = list()
        for record in records:
            record: Record
            indexList.append(record['name'])
        print(f"indexList: {indexList}")
        for index in indexList:
            gql = f"DROP INDEX {index}"
            print(f"gql: {gql}")
            records, summary, keys = driver.execute_query(
                                        query_=gql, 
                                        database_="neo4j", 
                                        routing_=RoutingControl.WRITE
                                    )

if __name__ == '__main__':
    DropAllIndex()

后记

  • 如果代码运行卡住,检查数据库地址和账号密码。
本文出自 qbit snap

qbit
268 声望279 粉丝