Skip to content

Commit

Permalink
fix: use elasticsearch client instead of nuxt-elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin3go committed Jan 22, 2024
1 parent 9407a1c commit 03fde2b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 45 deletions.
15 changes: 1 addition & 14 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,21 @@ export default defineNuxtConfig({
}
},
modules: [
'nuxt-elasticsearch',
(_options, nuxt) => {
nuxt.hooks.hook('vite:extendConfig', (config) => {
// @ts-expect-error
config.plugins.push(vuetify({ autoImport: true }))
})
},
],
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: {
template: {
transformAssetUrls,
},
},
},
nitro: {
minify: false
}
})
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
40 changes: 16 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions server/api/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { client } from "~/server/elasticsearch";

interface ISearchQuery {
pageNo: number;
pageSize: number;
query: string;
}

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, // 从哪里开始
Expand Down
5 changes: 3 additions & 2 deletions server/api/search/suggest.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
9 changes: 9 additions & 0 deletions server/elasticsearch/index.ts
Original file line number Diff line number Diff line change
@@ -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 || ''
}
});

0 comments on commit 03fde2b

Please sign in to comment.