Merge branch 'typesense-search-engine' into fix-bugs
This commit is contained in:
@@ -2,11 +2,14 @@ const catchAsync = require('../../globalErrorHandler/catchAsync')
|
||||
const belongingService = require('./belongingService')
|
||||
|
||||
exports.searchBelongings = catchAsync(async (req, res, next) => {
|
||||
const {facets, belongings, totalCount, pagesCount} = await belongingService.searchBelongings()
|
||||
const { facets, belongings, totalCount, pagesCount } = await belongingService.searchBelongings(req.query)
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
facets, belongings, totalCount, pagesCount
|
||||
facets,
|
||||
belongings,
|
||||
totalCount,
|
||||
pagesCount,
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,11 +18,10 @@ exports.indexAllBelongings = catchAsync(async (req, res, next) => {
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
indexedCount
|
||||
indexedCount,
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
exports.getAllBelongings = catchAsync(async (req, res, next) => {
|
||||
const belongings = await belongingService.getAllBelongings()
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ router.get('/admin/get-all', perm.requires('getallbelongings'), belongingControl
|
||||
|
||||
router.post('/admin/create', perm.requires('createabelonging'), belongingController.createBelonging)
|
||||
|
||||
router.post('/admin/index-all', perm.requires('indexbelongings', belongingController.indexAllBelongings))
|
||||
router.post('/admin/index-all', perm.requires('indexbelongings'), belongingController.indexAllBelongings)
|
||||
|
||||
router.get('/admin/search', perm.requires('searchbelongings'), belongingController.searchBelongings)
|
||||
|
||||
router.get('/admin/get-one/:belongingId', perm.requires('getabelonging'), belongingController.getBelonging)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const Belonging = require('./belongingModel')
|
||||
const AppError = require('../../globalErrorHandler/AppError')
|
||||
const { client } = require('../../setup/typesense')
|
||||
const TsApiFeatures = require("../../utils/TsApiFeatures");
|
||||
const TsApiFeatures = require('../../utils/TsApiFeatures')
|
||||
|
||||
const getAllBelongings = async () => {
|
||||
return Belonging.find({}, null, null).populate('category')
|
||||
@@ -29,7 +29,6 @@ const getBelonging = async (belongingId) => {
|
||||
belonging.signedImageUrls = imageUrls
|
||||
belonging.signedMainImageUrls = mainImageUrls
|
||||
|
||||
|
||||
return belonging
|
||||
}
|
||||
|
||||
@@ -92,41 +91,46 @@ const indexAllBelongings = async () => {
|
||||
let documents = []
|
||||
|
||||
for (const belonging of belongings) {
|
||||
const categoryArray = Array.isArray(belonging.category) ? belonging.category : belonging.category ? [belonging.category] : []
|
||||
const tags = belonging.tags.join(', ')
|
||||
|
||||
const data = {
|
||||
_id: belonging._id,
|
||||
isActive: belonging.isActive,
|
||||
name: belonging.name,
|
||||
belongingCode: belonging.belongingCode,
|
||||
category: belonging.category.map( x=> x.englishName),
|
||||
nativeCategory: belonging.category.map( x=> x.nativeName),
|
||||
documents: belonging.documents,
|
||||
category: categoryArray.map((x) => x.englishName),
|
||||
nativeCategory: categoryArray.map((x) => x.nativeName),
|
||||
description: belonging.description,
|
||||
coordinates: belonging.location.coordinates
|
||||
unit: belonging.unit,
|
||||
tags: tags,
|
||||
coordinates: belonging.location.coordinates,
|
||||
createdAt: Math.floor(new Date(belonging.createdAt).getTime() / 1000), // Store as timestamp in seconds
|
||||
}
|
||||
|
||||
documents.push(data)
|
||||
}
|
||||
|
||||
await client.collections('belonging').documents().import(documents, {action: 'emplace'})
|
||||
await client.collections('belongings').documents().import(documents, { action: 'emplace' })
|
||||
}
|
||||
|
||||
return { totalCount: count }
|
||||
}
|
||||
|
||||
|
||||
const getTypeSenseSchema = () => {
|
||||
return {
|
||||
name: 'belongings',
|
||||
enable_nested_fields: true,
|
||||
fields: [
|
||||
{ name: '_id', type: 'string', facet: false, infix: false },
|
||||
{ name: 'isActive', type: 'bool', facet: false, infix: true },
|
||||
{ name: 'belongingCode', type: 'string', facet: true },
|
||||
{ name: 'category', type: 'string[]', facet: true },
|
||||
{ name: 'nativeCategory', type: 'string[]', facet: false },
|
||||
{ name: 'documents', type: 'string[]', facet: false },
|
||||
{ name: 'name', type: 'string', facet: false, infix: true },
|
||||
{ name: 'belongingCode', type: 'int64', facet: false },
|
||||
{ name: 'category', type: 'string[]', facet: true, infix: true },
|
||||
{ name: 'nativeCategory', type: 'string[]', facet: false, infix: true },
|
||||
{ name: 'description', type: 'string', facet: false },
|
||||
{ name: 'unit', type: 'string', facet: false, infix: true },
|
||||
{ name: 'tags', type: 'string', facet: false, infix: true },
|
||||
{ name: 'coordinates', type: 'geopoint' },
|
||||
{ name: 'createdAt', type: 'int64', facet: false },
|
||||
],
|
||||
default_sorting_field: 'createdAt',
|
||||
}
|
||||
|
||||
@@ -1,6 +1,27 @@
|
||||
const catchAsync = require('../../globalErrorHandler/catchAsync')
|
||||
const transactionService = require('./transactionService')
|
||||
|
||||
exports.searchTransactions = catchAsync(async (req, res, next) => {
|
||||
const { facets, transactions, totalCount, pagesCount } = await transactionService.searchTransactions(req.query)
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
facets,
|
||||
transactions,
|
||||
totalCount,
|
||||
pagesCount,
|
||||
})
|
||||
})
|
||||
|
||||
exports.indexAllTransactions = catchAsync(async (req, res, next) => {
|
||||
const { indexedCount } = await transactionService.indexAllTransactions()
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
indexedCount,
|
||||
})
|
||||
})
|
||||
|
||||
exports.createTransaction = catchAsync(async (req, res, next) => {
|
||||
const transactions = await transactionService.createTransaction(req.body)
|
||||
|
||||
|
||||
@@ -19,6 +19,18 @@ router.post(
|
||||
transactionController.createTransaction
|
||||
)
|
||||
|
||||
router.post(
|
||||
'/admin/index-all',
|
||||
// perm.requires('getallbelongings'),
|
||||
transactionController.indexAllTransactions
|
||||
)
|
||||
|
||||
router.get(
|
||||
'/admin/search',
|
||||
// perm.requires('getallbelongings'),
|
||||
transactionController.searchTransactions
|
||||
)
|
||||
|
||||
router.get(
|
||||
'/admin/get-stock-by-belonging/:belongingId',
|
||||
// perm.requires('getallbelongings'),
|
||||
|
||||
@@ -151,6 +151,81 @@ const deleteTransaction = async (transactionId) => {
|
||||
return null
|
||||
}
|
||||
|
||||
const ensureCollectionExists = async () => {
|
||||
const collections = await client.collections().retrieve()
|
||||
const collectionExists = collections.some((collection) => collection.name === 'transactions')
|
||||
|
||||
if (collectionExists) {
|
||||
await client.collections('transactions').delete()
|
||||
}
|
||||
|
||||
const schema = getTypeSenseSchema()
|
||||
|
||||
await client.collections().create(schema)
|
||||
console.log('Collection "transactions" created successfully')
|
||||
}
|
||||
|
||||
const searchTransactions = async (inputQuery) => {
|
||||
const tsFeatures = new TsApiFeatures(client, 'transactions', inputQuery).filter().facet().sort().limitFields().paginate().search()
|
||||
|
||||
let { facets, items, totalCount, pagesCount } = await tsFeatures.execute()
|
||||
|
||||
return { facets, transactions: items, totalCount, pagesCount }
|
||||
}
|
||||
|
||||
const indexAllTransactions = async () => {
|
||||
const batchSize = 20
|
||||
const count = await Transaction.countDocuments({}, null)
|
||||
const rounds = Math.ceil(count / batchSize)
|
||||
|
||||
await ensureCollectionExists()
|
||||
|
||||
for (let i = 1; i <= rounds; i++) {
|
||||
console.log(`Indexing ${batchSize} transactions, Round ${i}`)
|
||||
|
||||
const transactions = await Transaction.find({}, null, null)
|
||||
.skip((i - 1) * batchSize)
|
||||
.limit(batchSize)
|
||||
|
||||
let documents = []
|
||||
|
||||
for (const transaction of transactions) {
|
||||
const data = {
|
||||
_id: transaction._id,
|
||||
type: transaction.type,
|
||||
status: transaction.status,
|
||||
source: transaction.source,
|
||||
value: transaction.value,
|
||||
description: transaction.description,
|
||||
createdAt: Math.floor(new Date(transaction.createdAt).getTime() / 1000), // Store as timestamp in seconds
|
||||
}
|
||||
|
||||
documents.push(data)
|
||||
}
|
||||
|
||||
await client.collections('transactions').documents().import(documents, { action: 'emplace' })
|
||||
}
|
||||
|
||||
return { totalCount: count }
|
||||
}
|
||||
|
||||
const getTypeSenseSchema = () => {
|
||||
return {
|
||||
name: 'transactions',
|
||||
enable_nested_fields: true,
|
||||
fields: [
|
||||
{ name: '_id', type: 'string', facet: false, infix: false },
|
||||
{ name: 'type', type: 'string', facet: false },
|
||||
{ name: 'status', type: 'string', facet: false },
|
||||
{ name: 'source', type: 'string', facet: false },
|
||||
{ name: 'value', type: 'int64', facet: false },
|
||||
{ name: 'description', type: 'string', facet: false, infix: true },
|
||||
{ name: 'createdAt', type: 'int64', facet: false },
|
||||
],
|
||||
default_sorting_field: 'createdAt',
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createTransaction,
|
||||
getUserTransaction,
|
||||
@@ -158,4 +233,6 @@ module.exports = {
|
||||
updateTransaction,
|
||||
deleteTransaction,
|
||||
getTransactionByBelongingId,
|
||||
indexAllTransactions,
|
||||
searchTransactions,
|
||||
}
|
||||
|
||||
+2
-2
@@ -5,8 +5,8 @@ JWT_SECRET=Y@RJ$WT!@K0NS@MPL!
|
||||
REDIS_URL= redis://:GFis6zU4DXW9Q7NQ@services.irn12.chabokan.net:19794/0
|
||||
LOGIN_ATTEMPT_LIMIT=900
|
||||
SUPER_ADMIN_ROLE_ID=66a2da5b9b989a8909948848
|
||||
TYPE_SENSE_URL=services.irn9.chabokan.net
|
||||
TYPE_SENSE_API_KEY=zQzlsVKmSX5M0N09
|
||||
TYPE_SENSE_URL=bm-typesense.chbk.app
|
||||
TYPE_SENSE_API_KEY=rHV4vZePdfBYFC9q
|
||||
AWS_ENDPOINT=bm-minio.chbk.app
|
||||
AWS_ACCESS_KEY=EFYrmV0EiH4qwGERtbhJygNglaCZYtti
|
||||
AWS_SECRET_KEY=ArkI5GDTId6GsQ0WvKFSp54lrx78gSW1
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ let client = new TypeSense.Client({
|
||||
{
|
||||
host: process.env.TYPE_SENSE_URL,
|
||||
port: '443',
|
||||
protocol: 'httpS',
|
||||
protocol: 'https',
|
||||
},
|
||||
],
|
||||
apiKey: process.env.TYPE_SENSE_API_KEY,
|
||||
|
||||
Reference in New Issue
Block a user