From 03fde2be2b34257d4a75a18cb12e20974e87467a Mon Sep 17 00:00:00 2001 From: justin3go Date: Mon, 22 Jan 2024 15:08:00 +0800 Subject: [PATCH] fix: use elasticsearch client instead of nuxt-elasticsearch --- nuxt.config.ts | 15 +------------ package.json | 7 +++--- pnpm-lock.yaml | 40 ++++++++++++++--------------------- server/api/search/index.ts | 5 +++-- server/api/search/suggest.ts | 5 +++-- server/elasticsearch/index.ts | 9 ++++++++ 6 files changed, 36 insertions(+), 45 deletions(-) create mode 100644 server/elasticsearch/index.ts diff --git a/nuxt.config.ts b/nuxt.config.ts index d509486..99aa6c3 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -11,7 +11,6 @@ export default defineNuxtConfig({ } }, modules: [ - 'nuxt-elasticsearch', (_options, nuxt) => { nuxt.hooks.hook('vite:extendConfig', (config) => { // @ts-expect-error @@ -19,17 +18,8 @@ export default defineNuxtConfig({ }) }, ], - nuxtElasticsearch: { - clientOpts: { - node: process.env.ES_URL, - auth: { - username: process.env.ES_AUTH_USERNAME || '', - password: process.env.ES_AUTH_PASSWORD || '', - } - } - }, build: { - transpile: ['vuetify', 'lodash'], + transpile: ['vuetify'], }, vite: { vue: { @@ -37,8 +27,5 @@ export default defineNuxtConfig({ transformAssetUrls, }, }, - }, - nitro: { - minify: false } }) diff --git a/package.json b/package.json index 1b24c11..18938a9 100644 --- a/package.json +++ b/package.json @@ -12,10 +12,9 @@ "type-check": "tsc --noEmit" }, "devDependencies": { + "@types/elasticsearch": "^5.0.43", "@types/lodash": "^4.14.202", - "lodash": "^4.17.21", "nuxt": "^3.9.1", - "nuxt-elasticsearch": "^1.0.2", "sass": "^1.69.7", "sass-loader": "^14.0.0", "typescript": "^5.3.3", @@ -26,7 +25,9 @@ "vuetify": "^3.4.10" }, "dependencies": { + "@elastic/elasticsearch": "^7.17.13", "@mdi/font": "^7.4.47", - "cross-env": "^7.0.3" + "cross-env": "^7.0.3", + "lodash": "^4.17.21" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ff839f0..74edef0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,26 +5,29 @@ settings: excludeLinksFromLockfile: false dependencies: + '@elastic/elasticsearch': + specifier: ^7.17.13 + version: 7.17.13 '@mdi/font': specifier: ^7.4.47 version: 7.4.47 cross-env: specifier: ^7.0.3 version: 7.0.3 + lodash: + specifier: ^4.17.21 + version: 4.17.21 devDependencies: + '@types/elasticsearch': + specifier: ^5.0.43 + version: 5.0.43 '@types/lodash': specifier: ^4.14.202 version: 4.14.202 - lodash: - specifier: ^4.17.21 - version: 4.17.21 nuxt: specifier: ^3.9.1 version: 3.9.1(sass@1.69.7)(typescript@5.3.3)(vite@5.0.11) - nuxt-elasticsearch: - specifier: ^1.0.2 - version: 1.0.2 sass: specifier: ^1.69.7 version: 1.69.7 @@ -413,7 +416,7 @@ packages: secure-json-parse: 2.7.0 transitivePeerDependencies: - supports-color - dev: true + dev: false /@esbuild/aix-ppc64@0.19.11: resolution: {integrity: sha512-FnzU0LyE3ySQk7UntJO4+qIiQgI7KoODnZg5xzXIrFJlKd2P2gwHsHY4927xj9y5PJmJSzULiUCWmv7iWnNa7g==} @@ -1519,6 +1522,10 @@ packages: minimatch: 9.0.3 dev: true + /@types/elasticsearch@5.0.43: + resolution: {integrity: sha512-N+MpzURpDCWd7zaJ7CE1aU+nBSeAABLhDE0lGodQ0LLftx7ku6hjTXLr9OAFZLSXiWL3Xxx8jts485ynrcm5NA==} + dev: true + /@types/eslint-scope@3.7.7: resolution: {integrity: sha512-MzMFlSLBqNF2gcHWO0G1vP/YQyfvrxZ0bF+u7mzUdZ1/xK4A4sru+nraZz5i3iEIk1l1uyicaDVTB4QbbEkAYg==} dependencies: @@ -2604,7 +2611,6 @@ packages: optional: true dependencies: ms: 2.1.2 - dev: true /deepmerge@4.3.1: resolution: {integrity: sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==} @@ -3252,7 +3258,7 @@ packages: /hpagent@0.1.2: resolution: {integrity: sha512-ePqFXHtSQWAFXYmj+JtOTHr84iNrII4/QRlAAPPE+zqnKy4xJo7Ie1Y4kC7AdB+LxLxSTTzBMASsEcy0q8YyvQ==} - dev: true + dev: false /html-tags@3.3.1: resolution: {integrity: sha512-ztqyC3kLto0e9WbNp0aeP+M3kTt+nbaIveGmUxAtZa+8iFgKLUOD4YKM5j+f3QD89bra7UeumolZHKuOXnTmeQ==} @@ -3743,7 +3749,6 @@ packages: /lodash@4.17.21: resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==} - dev: true /lru-cache@10.1.0: resolution: {integrity: sha512-/1clY/ui8CzjKFyjdvwPWJUYKiFVXG2I2cY0ssG7h4+hwk+XOIX7ZSG9Q7TW8TW3Kp3BUSqgFWBLgL4PJ+Blag==} @@ -3992,11 +3997,9 @@ packages: /ms@2.1.2: resolution: {integrity: sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==} - dev: true /ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} - dev: true /nanoid@3.3.7: resolution: {integrity: sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==} @@ -4297,17 +4300,6 @@ packages: fsevents: 2.3.3 dev: true - /nuxt-elasticsearch@1.0.2: - resolution: {integrity: sha512-SUlJMx8tznGCqBqVHPxvRR5P8Jb2LXbwUSrQPFt6ZmK/GK+DW1Q+f0N4zL0Dowo6m6gyHIVlFcSFaDWNYhDIhw==} - dependencies: - '@elastic/elasticsearch': 7.17.13 - '@nuxt/kit': 3.9.1 - defu: 6.1.4 - transitivePeerDependencies: - - rollup - - supports-color - dev: true - /nuxt@3.9.1(sass@1.69.7)(typescript@5.3.3)(vite@5.0.11): resolution: {integrity: sha512-jyD9E74bx8cdDc3nmYMNsJR0dIOrpcDH/mBlo1FmpB2zeXXMwupOD2tm033MJpHIEBbhQGHYFQlRyd7wHg2DyA==} engines: {node: ^14.18.0 || >=16.10.0} @@ -5210,7 +5202,7 @@ packages: /secure-json-parse@2.7.0: resolution: {integrity: sha512-6aU+Rwsezw7VR8/nyvKTx8QpWH9FrcYiXXlqC4z5d5XQBDRqtbfsRjnwGyqbi3gddNtWHuEk9OANUotL26qKUw==} - dev: true + dev: false /semver@6.3.1: resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==} diff --git a/server/api/search/index.ts b/server/api/search/index.ts index 3dd88fd..f621637 100644 --- a/server/api/search/index.ts +++ b/server/api/search/index.ts @@ -1,3 +1,5 @@ +import { client } from "~/server/elasticsearch"; + interface ISearchQuery { pageNo: number; pageSize: number; @@ -5,10 +7,9 @@ interface ISearchQuery { } export default defineEventHandler(async (event) => { - const { serverElasticsearchClient } = event.context; const { pageNo = 1, pageSize = 10, query }: ISearchQuery = getQuery(event); - const esRes = await serverElasticsearchClient.search({ + const esRes = await client.search({ index: process.env.ES_INDEX, body: { from: (pageNo - 1) * pageSize, // 从哪里开始 diff --git a/server/api/search/suggest.ts b/server/api/search/suggest.ts index 7063bd4..ccd0ae6 100644 --- a/server/api/search/suggest.ts +++ b/server/api/search/suggest.ts @@ -1,12 +1,13 @@ +import { client } from "~/server/elasticsearch"; + interface ISuggestQuery { input: string; } export default defineEventHandler(async (event) => { - const { serverElasticsearchClient } = event.context; const { input }: ISuggestQuery = getQuery(event); - const esRes = await serverElasticsearchClient.search({ + const esRes = await client.search({ index: process.env.ES_INDEX, body: { suggest: { diff --git a/server/elasticsearch/index.ts b/server/elasticsearch/index.ts new file mode 100644 index 0000000..011089f --- /dev/null +++ b/server/elasticsearch/index.ts @@ -0,0 +1,9 @@ +import { Client } from '@elastic/elasticsearch'; + +export const client = new Client({ + node: process.env.ES_URL, + auth: { + username: process.env.ES_AUTH_USERNAME || '', + password: process.env.ES_AUTH_PASSWORD || '' + } +}); \ No newline at end of file