Skip to content

Commit 933c3fd

Browse files
authored
Add mongo index (#519)
1 parent f818260 commit 933c3fd

File tree

7 files changed

+61
-31
lines changed

7 files changed

+61
-31
lines changed

packages/service/common/mongo/init.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ export async function connectMongo({
2626
bufferCommands: true,
2727
maxConnecting: Number(process.env.DB_MAX_LINK || 5),
2828
maxPoolSize: Number(process.env.DB_MAX_LINK || 5),
29-
minPoolSize: 2,
30-
connectTimeoutMS: 20000,
31-
waitQueueTimeoutMS: 20000
29+
minPoolSize: Number(process.env.DB_MAX_LINK || 10) * 0.5,
30+
connectTimeoutMS: 60000,
31+
waitQueueTimeoutMS: 60000
3232
});
3333

3434
console.log('mongo connected');

packages/service/core/dataset/collection/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const DatasetCollectionSchema = new Schema({
6969

7070
try {
7171
DatasetCollectionSchema.index({ datasetId: 1 });
72+
DatasetCollectionSchema.index({ datasetId: 1, parentId: 1 });
7273
DatasetCollectionSchema.index({ updateTime: -1 });
7374
} catch (error) {
7475
console.log(error);

packages/service/core/dataset/data/schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ try {
7878
DatasetDataSchema.index({ datasetId: 1 });
7979
DatasetDataSchema.index({ collectionId: 1 });
8080
// full text index
81-
DatasetDataSchema.index({ fullTextToken: 'text' });
81+
DatasetDataSchema.index({ datasetId: 1, fullTextToken: 'text' });
82+
DatasetDataSchema.index({ fullTextToken: 1 });
8283
} catch (error) {
8384
console.log(error);
8485
}

projects/app/src/pages/api/core/app/form2Modules/fastgpt-universal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,21 +283,21 @@ function datasetTemplate(formData: AppSimpleEditFormType): ModuleItemType[] {
283283
value: formData.dataset.datasets,
284284
type: FlowNodeInputTypeEnum.custom,
285285
label: '关联的知识库',
286-
connected: true
286+
connected: false
287287
},
288288
{
289289
key: 'similarity',
290290
value: formData.dataset.similarity,
291291
type: FlowNodeInputTypeEnum.slider,
292292
label: '相似度',
293-
connected: true
293+
connected: false
294294
},
295295
{
296296
key: 'limit',
297297
value: formData.dataset.limit,
298298
type: FlowNodeInputTypeEnum.slider,
299299
label: '单次搜索上限',
300-
connected: true
300+
connected: false
301301
},
302302
{
303303
key: 'switch',
@@ -317,7 +317,7 @@ function datasetTemplate(formData: AppSimpleEditFormType): ModuleItemType[] {
317317
label: '结果重排',
318318
description: '将召回的结果进行进一步重排,可增加召回率',
319319
plusField: true,
320-
connected: true,
320+
connected: false,
321321
value: formData.dataset.rerank
322322
}
323323
],

projects/app/src/pages/app/detail/components/SimpleEdit/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ function ConfigForm({
392392
})
393393
}
394394
>
395-
<Image alt={''} src={item.avatar} w={'18px'} mr={1} />
395+
<Avatar src={item.avatar} w={'18px'} mr={1} />
396396
<Box flex={'1 0 0'} w={0} className={'textEllipsis'} fontSize={'sm'}>
397397
{item.name}
398398
</Box>

projects/app/src/service/core/dataset/data/pg.ts

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PgDatasetTableName } from '@fastgpt/global/core/dataset/constant';
22
import type {
3-
DatasetDataWithCollectionType,
3+
DatasetDataSchemaType,
44
SearchDataResponseItemType
55
} from '@fastgpt/global/core/dataset/type.d';
66
import { PgClient } from '@fastgpt/service/common/pg';
@@ -298,30 +298,58 @@ export async function fullTextRecall({
298298
};
299299
}
300300

301-
const result = (await MongoDatasetData.find(
301+
let searchResults = (
302+
await Promise.all(
303+
datasetIds.map((id) =>
304+
MongoDatasetData.find(
305+
{
306+
datasetId: id,
307+
$text: { $search: jiebaSplit({ text }) }
308+
},
309+
{
310+
score: { $meta: 'textScore' },
311+
_id: 1,
312+
datasetId: 1,
313+
collectionId: 1,
314+
q: 1,
315+
a: 1,
316+
indexes: 1
317+
}
318+
)
319+
.sort({ score: { $meta: 'textScore' } })
320+
.limit(limit)
321+
.lean()
322+
)
323+
)
324+
).flat() as (DatasetDataSchemaType & { score: number })[];
325+
326+
// resort
327+
searchResults.sort((a, b) => b.score - a.score);
328+
searchResults.slice(0, limit);
329+
330+
const collections = await MongoDatasetCollection.find(
302331
{
303-
datasetId: { $in: datasetIds.map((item) => item) },
304-
$text: { $search: jiebaSplit({ text }) }
332+
_id: { $in: searchResults.map((item) => item.collectionId) }
305333
},
306-
{ score: { $meta: 'textScore' } }
307-
)
308-
.sort({ score: { $meta: 'textScore' } })
309-
.limit(limit)
310-
.populate('collectionId')
311-
.lean()) as DatasetDataWithCollectionType[];
334+
'_id name metadata'
335+
);
312336

313337
return {
314-
fullTextRecallResults: result.map((item) => ({
315-
id: String(item._id),
316-
datasetId: String(item.datasetId),
317-
collectionId: String(item.collectionId._id),
318-
sourceName: item.collectionId.name || '',
319-
sourceId: item.collectionId.metadata?.fileId || item.collectionId.metadata?.rawLink,
320-
q: item.q,
321-
a: item.a,
322-
indexes: item.indexes,
323-
score: 1
324-
})),
338+
fullTextRecallResults: searchResults.map((item) => {
339+
const collection = collections.find((col) => String(col._id) === String(item.collectionId));
340+
return {
341+
id: String(item._id),
342+
datasetId: String(item.datasetId),
343+
collectionId: String(item.collectionId),
344+
sourceName: collection?.name || '',
345+
sourceId: collection?.metadata?.fileId || collection?.metadata?.rawLink,
346+
q: item.q,
347+
a: item.a,
348+
indexes: item.indexes,
349+
// @ts-ignore
350+
score: item.score
351+
};
352+
}),
325353
tokenLen: 0
326354
};
327355
}

projects/app/src/web/core/app/templates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export const appTemplates: (AppItemType & {
225225
]
226226
},
227227
{
228-
id: 'simpleKbChat',
228+
id: 'simpleDatasetChat',
229229
avatar: '/imgs/module/db.png',
230230
name: '知识库 + 对话引导',
231231
intro: '每次提问时进行一次知识库搜索,将搜索结果注入 LLM 模型进行参考回答',

0 commit comments

Comments
 (0)