# IndexServer **Repository Path**: hongbin1/index-server ## Basic Information - **Project Name**: IndexServer - **Description**: 全文检索数据库,集群化方案使用gossip协议 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-14 - **Last Updated**: 2025-12-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # IndexServer - 分布式全文检索系统 IndexServer是一个基于Go语言开发的高性能分布式全文检索系统,提供了类似Elasticsearch的API接口,支持水平扩展和高可用性。 ## 功能特性 - **分布式架构**:基于Gossip协议的节点发现和通信机制 - **水平扩展**:支持动态添加/移除节点,自动重新分配数据 - **分片管理**:使用一致性哈希算法实现数据分片 - **多存储支持**:支持RAM、本地文件系统和S3存储 - **RESTful API**:提供类似Elasticsearch的API接口 - **实时监控**:内置监控指标和集群状态查看 - **容错机制**:支持主从副本,自动故障转移 ## 技术栈 - **Go 1.25.5**:主要开发语言 - **Gin**:Web框架 - **index-core**:自定义索引核心库 - **AWS S3**:可选的分布式存储后端 ## 系统架构 ``` ┌─────────────────────────────────────────────────────────────────────────┐ │ API层 │ │ ┌───────────────────────────────────────────────────────────────────┐ │ │ │ Gin Router (api/router.go) │ │ │ └───────────────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────────────────┘ │ ┌─────────────────────────────────────────────────────────────────────────┐ │ 核心层 │ │ ┌───────────────────────────────────────────────────────────────────┐ │ │ │ IndexManager (core/index_manager.go) │ │ │ │ MetadataManager (core/metadata.go) │ │ │ │ QueryParser (core/query_parser.go) │ │ │ └───────────────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────────────────┘ │ ┌─────────────────────────────────────────────────────────────────────────┐ │ 集群层 │ │ ┌────────────────────────┐ ┌────────────────────────┐ ┌──────────────┐ │ │ │ Gossip协议 │ │ 一致性哈希路由 │ │ 分片管理 │ │ │ │ (cluster/gossip) │ │ (cluster/routing) │ │ (cluster/sharding)│ │ │ └────────────────────────┘ └────────────────────────┘ └──────────────┘ │ └─────────────────────────────────────────────────────────────────────────┘ │ ┌─────────────────────────────────────────────────────────────────────────┐ │ 存储层 │ │ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ │ │ │ RAM存储 │ │ 本地文件存储 │ │ S3存储 │ │ │ └────────────────┘ └────────────────┘ └────────────────┘ │ └─────────────────────────────────────────────────────────────────────────┘ ``` ## 快速开始 ### 安装依赖 ```bash go mod download ``` ### 构建项目 ```bash go build -o indexserver ./cmd/main.go ``` ### 启动单节点服务器 ```bash ./indexserver --port 9201 ``` ### 启动集群 ```bash # 启动第一个节点(种子节点) ./indexserver --port 9201 # 启动第二个节点,加入集群 ./indexserver --port 9202 --seed-nodes localhost:9201 # 启动第三个节点,加入集群 ./indexserver --port 9203 --seed-nodes localhost:9201 ``` ## 命令行参数 | 参数名 | 默认值 | 描述 | |--------|--------|------| | `--node-id` | 自动生成 | 节点ID,不指定则自动生成 | | `--port` | 9201 | 服务端口 | | `--seed-nodes` | 空 | 种子节点列表,逗号分隔 | | `--storage-type` | ram | 存储类型:ram, local, s3 | | `--storage-path` | data/index | 本地存储路径 | | `--storage-bucket` | 空 | S3存储桶名称 | | `--storage-prefix` | 空 | S3存储前缀 | ## API接口 IndexServer提供了类似Elasticsearch的RESTful API接口,主要包括: ### 索引管理 - `PUT /:index_name` - 创建索引 - `DELETE /:index_name` - 删除索引 - `GET /:index_name` - 获取索引信息 - `GET /:index_name/_mapping` - 获取索引映射 - `PUT /:index_name/_mapping` - 更新索引映射 ### 文档管理 - `POST /:index_name/_doc` - 添加文档 - `PUT /:index_name/_doc/:id` - 更新文档 - `DELETE /:index_name/_doc/:id` - 删除文档 - `GET /:index_name/_doc/:id` - 获取文档 ### 搜索 - `POST /:index_name/_search` - 执行搜索 - `GET /:index_name/_search` - 简单查询字符串搜索 ### 集群管理 - `GET /_cluster/state` - 获取集群状态 - `GET /_nodes` - 获取节点列表 - `GET /_shards` - 获取分片信息 ### 监控 - `GET /_metrics` - 获取监控指标 - `GET /api/metrics/timeseries` - 获取时间序列指标 ## 使用示例 ### 创建索引 ```bash curl -X PUT "http://localhost:9201/test_index" -H 'Content-Type: application/json' -d ' { "settings": { "index": { "number_of_shards": 3, "number_of_replicas": 1 } }, "mappings": { "properties": { "title": { "type": "string", "store": true, "index": true, "analyzer": "standard" }, "content": { "type": "string", "store": true, "index": true, "analyzer": "standard" }, "timestamp": { "type": "string", "store": true, "index": true } } } } ' ``` ### 添加文档 ```bash curl -X POST "http://localhost:9201/test_index/_doc" -H 'Content-Type: application/json' -d ' { "title": "测试文档", "content": "这是一个测试文档,用于演示IndexServer的功能。", "timestamp": "2025-12-14T10:00:00Z" } ' ``` ### 搜索文档 ```bash curl -X POST "http://localhost:9201/test_index/_search" -H 'Content-Type: application/json' -d ' { "query": { "match": { "content": "测试" } } } ' ``` ## 监控界面 IndexServer提供了内置的监控界面,可以通过浏览器访问: ``` http://localhost:9201/ ``` ## 开发指南 ### 目录结构 ``` IndexServer/ ├── api/ # API层代码 ├── cluster/ # 集群管理相关代码 │ ├── gossip/ # Gossip协议实现 │ ├── routing/ # 路由和一致性哈希 │ └── sharding/ # 分片管理 ├── cmd/ # 命令行入口 ├── core/ # 核心功能实现 ├── data/ # 数据存储目录 ├── docs/ # 文档 ├── monitoring/ # 监控相关代码 ├── web/ # Web界面 ├── LICENSE # 许可证 ├── go.mod # Go模块文件 └── go.sum # Go依赖校验文件 ``` ### 运行测试 ```bash # 运行单元测试 go test ./... # 运行集成测试 python integration_test.py ``` ## 部署指南 ### 单机部署 ```bash ./indexserver --port 9201 --storage-type local --storage-path /data/indexserver ``` ### 集群部署 在多个服务器上分别运行: ```bash # 服务器1 ./indexserver --port 9201 --storage-type s3 --storage-bucket my-index-bucket --storage-prefix indexserver # 服务器2 ./indexserver --port 9201 --seed-nodes server1:9201 --storage-type s3 --storage-bucket my-index-bucket --storage-prefix indexserver # 服务器3 ./indexserver --port 9201 --seed-nodes server1:9201 --storage-type s3 --storage-bucket my-index-bucket --storage-prefix indexserver ``` ## 性能优化 1. **选择合适的存储类型**: - 开发环境:使用RAM存储提高性能 - 生产环境:使用S3存储保证数据持久化 2. **调整分片数量**: - 根据数据量和节点数量调整分片数 - 推荐每个分片大小在10GB-50GB之间 3. **启用缓存**: - IndexServer内置了查询缓存,可以提高重复查询的性能 ## 故障处理 ### 节点故障 当节点发生故障时: 1. Gossip协议会自动检测到节点故障 2. 集群会将故障节点上的主分片自动转移到其他节点 3. 副本分片会被提升为主分片 ### 数据恢复 如果使用S3存储,数据会自动持久化。当节点重启时: 1. 从S3读取元数据 2. 重建索引 3. 重新加入集群 ## 许可证 [MIT License](LICENSE) ## 贡献 欢迎提交Issue和Pull Request! ## 联系方式 如有问题或建议,请通过以下方式联系: - Email: your-email@example.com - GitHub: [IndexServer](https://github.com/yourusername/IndexServer) ## 更新日志 ### v1.0.0 (2025-12-14) - 初始版本发布 - 支持基本的索引管理和搜索功能 - 支持分布式集群 - 支持多种存储类型 - 提供实时监控界面