-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a4c3ef
commit 5b095f5
Showing
6 changed files
with
140 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
EXPRESS_PORT=9001 | ||
EXPRESS_PORT=9001 | ||
EXPRESS_NAME=msc | ||
|
||
ELASTICSEARCH_ISOPEN=true | ||
ELASTICSEARCH_URL=http://localhost:9200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Base from "../base"; | ||
import Es from "../../library/es/index" | ||
const es = new Es(); | ||
|
||
class EsCreateIndex extends Base { | ||
static get signature() { | ||
return ` | ||
Es:Create:Index | ||
{key:创建索引key} | ||
`; | ||
} | ||
|
||
static get description() { | ||
return "Elasticsearch 创建索引"; | ||
} | ||
|
||
/** | ||
* ES 存储方便查询更快 | ||
* @param {*} args | ||
* @param {*} options | ||
*/ | ||
async execute(args, options) { | ||
let { key } = args; | ||
console.log(es.createIndex(key)) | ||
} | ||
} | ||
|
||
export default EsCreateIndex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Base from "../base"; | ||
import Es from "../../library/es/index" | ||
const es = new Es(); | ||
|
||
class EsIndexList extends Base { | ||
static get signature() { | ||
return ` | ||
Es:Index:List | ||
`; | ||
} | ||
|
||
static get description() { | ||
return "Elasticsearch 获取所有索引列表"; | ||
} | ||
|
||
/** | ||
* ES 存储方便查询更快 | ||
* @param {*} args | ||
* @param {*} options | ||
*/ | ||
async execute(args, options) { | ||
const indexs = await es.indicesList() | ||
console.log(indexs) | ||
} | ||
} | ||
|
||
export default EsIndexList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import dotenv from "dotenv"; | ||
import { Client } from "elasticsearch"; | ||
const appConfig = dotenv.config().parsed; | ||
|
||
export default class Es { | ||
constructor() { | ||
if (!appConfig["ELASTICSEARCH_ISOPEN"]) { | ||
return; | ||
} | ||
// 创建 Elasticsearch 客户端实例 | ||
const client = new Client({ node: appConfig["ELASTICSEARCH_URL"] }); | ||
this.esClient = client; | ||
} | ||
|
||
async saveData(data) { | ||
return await this.esClient.bulk({ | ||
body: data, | ||
}); | ||
} | ||
|
||
/** | ||
* 创建索引 | ||
* @param {*} key | ||
*/ | ||
async createIndex(key) { | ||
return await this.esClient.indices.create({ | ||
index: key, | ||
}); | ||
} | ||
|
||
/** | ||
* 获取所有索引列表 | ||
* @returns | ||
*/ | ||
async indicesList() { | ||
const { body } = await this.esClient.cat.indices({ format: "json" }); | ||
if (body) { | ||
console.log(body) | ||
// 输出索引列表 | ||
const indices = body.map((index) => index.index); | ||
return indices; | ||
} else { | ||
return {} | ||
} | ||
} | ||
} |