# Elasticsearch技术调研演示 **Repository Path**: 6feel/ElasticsearchDemo ## Basic Information - **Project Name**: Elasticsearch技术调研演示 - **Description**: Elasticsearch 技术调研演示 - 客户检索 展示 ES 全文搜索、聚合分析、实时索引、中文分词、数据同步等核心能力 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-23 - **Last Updated**: 2026-05-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Elasticsearch 技术调研演示 展示 Elasticsearch 8.x 的核心能力:全文搜索、聚合分析、实时索引、中文分词、MySQL 数据同步。 ## 技术栈 - **Elasticsearch 8.12.0** - 分布式搜索引擎 - **Kibana 8.12.0** - ES 可视化工具 - **Logstash 8.12.0** - 数据同步管道 - **Node.js + Express** - RESTful API 后端 - **IK Analyzer** - 中文分词器 - **MySQL 5.7** - 源数据库 ## 快速开始 ### 1. 启动服务 ```bash # 启动所有服务 (ES, Kibana, Logstash) docker-compose up -d # 安装后端依赖 cd backend && npm install # 启动后端服务 npm start ``` ### 2. 访问地址 | 服务 | 地址 | |------|------| | API 服务 | http://localhost:3000 | | Kibana | http://localhost:5601 | | Elasticsearch | http://localhost:9200 | | 前端测试页面 | http://localhost:3000/frontend/index.html | ### 3. 健康检查 ```bash curl http://localhost:3000/health curl http://localhost:9200/_cluster/health ``` ## 功能演示 ### 创建索引 ```bash curl -X POST http://localhost:3000/api/indices \ -H "Content-Type: application/json" \ -d '{ "indexName": "products", "mappings": { "properties": { "name": { "type": "text", "analyzer": "ik_max_word" }, "description": { "type": "text", "analyzer": "ik_max_word" }, "price": { "type": "float" }, "category": { "type": "keyword" } } } }' ``` ### 添加文档 ```bash curl -X POST http://localhost:3000/api/docs/products \ -H "Content-Type: application/json" \ -d '{ "id": "1", "body": { "name": "iPhone 15 Pro", "description": "苹果最新款智能手机", "price": 8999.00, "category": "电子产品" } }' ``` ### 全文搜索 ```bash curl -X POST http://localhost:3000/api/search/products \ -H "Content-Type: application/json" \ -d '{ "query": { "match": { "name": "苹果" } }, "highlight": { "fields": { "name": {} } } }' ``` ### 布尔查询 ```bash curl -X POST http://localhost:3000/api/search/products \ -H "Content-Type: application/json" \ -d '{ "query": { "bool": { "must": [{ "match": { "name": "手机" } }], "filter": [{ "range": { "price": { "gte": 1000, "lte": 10000 } } }] } } }' ``` ### 聚合分析 ```bash curl -X POST http://localhost:3000/api/agg/products \ -H "Content-Type: application/json" \ -d '{ "aggs": { "category_stats": { "terms": { "field": "category", "size": 10 }, "aggs": { "avg_price": { "avg": { "field": "price" } }, "max_price": { "max": { "field": "price" } } } } } }' ``` ## API 列表 ### 索引管理 - `POST /api/indices` - 创建索引 - `GET /api/indices` - 列出所有索引 - `GET /api/indices/:name` - 获取索引详情 - `DELETE /api/indices/:name` - 删除索引 - `PUT /api/indices/:name/settings` - 更新设置 ### 文档操作 - `POST /api/docs/:index` - 添加文档 - `GET /api/docs/:index/:id` - 获取文档 - `PUT /api/docs/:index/:id` - 更新文档 - `DELETE /api/docs/:index/:id` - 删除文档 - `POST /api/docs/:index/_bulk` - 批量操作 - `POST /api/docs/:index/_bulk-index` - 批量添加 ### 搜索功能 - `POST /api/search/:index` - 全文搜索 - `POST /api/search/:index/explain/:id` - 评分解释 ### 聚合分析 - `POST /api/agg/:index` - 聚合查询 - `POST /api/agg/:index/terms` - 词项聚合 - `POST /api/agg/:index/range` - 范围聚合 - `POST /api/agg/:index/stats` - 统计聚合 ### MySQL - `GET /api/mysql/tables` - 列出表 - `POST /api/mysql/query/:table` - 查询表数据 ## 数据同步配置 Logstash 配置位于 `logstash/pipeline/` 目录: ### 增量同步 - 配置文件: `mysql-to-es.conf` - 同步周期: 每 5 分钟 - 追踪字段: `update_time` ### 全量同步 - 配置文件: `products-full-sync.conf` - 同步周期: 每天凌晨 2 点 ### MySQL 连接信息 > **注意**:请在 `.env` 文件中配置实际数据库连接信息 ```yaml host: your_mysql_host port: 3306 user: your_username password: your_password database: elasticsearch_demo ``` ## 前端测试页面 打开 `frontend/index.html` 即可通过图形界面测试所有功能。 功能包括: - 索引管理界面 - 文档 CRUD 操作 - 多种搜索类型演示 - 聚合分析演示 - 中文分词测试 - MySQL 数据查询 - 同步状态监控 ## 环境变量 复制 `.env.sample` 为 `.env` 并配置实际数据库连接: ```bash cp .env.sample .env ``` ```bash # MySQL DB_HOST=your_mysql_host DB_PORT=3306 DB_USER=your_username DB_PASSWORD=your_password # Elasticsearch ES_HOST=localhost ES_PORT=9200 # API API_PORT=3000 ``` ## 停止服务 ```bash # 停止所有容器 docker-compose down # 删除数据卷 docker-compose down -v ```