我正在尝试通过 Fastify 和 Mongoose 进行全文搜索。但它返回一个空数组。
网上没有关于这个的信息,所以我只是玩了一会儿。 在 db.ts 我设置了:
useCreateIndex: true
模型(Benefit.ts):
export class Benefit extends BaseModel {
_id: ObjectID;
@prop({required: true, unique: true, index: true, text: true}) <-- set index & text to true
name: string;
@prop({index: true, text: true})
details: string;
const BenefitModel = getModelForClass(Benefit);
BenefitModel.createIndexes(err => console.log('ERROR', err)); <-- this is what I tried // err=null
export default BenefitModel;
实际搜索(Benefit.controller.ts):
const searchResults = await benefitService.find(
{$text: {$search: textSearch}}, <-- textSearch is valid and comes as a string
{score: {$meta: 'textScore'}},
);
返回状态为 200:
{
"status": 1,
"results": []
}
我想我没有正确设置索引? 任何帮助将大大appriciated。 而且还会活很久,因为在任何地方都没有关于它的信息。
谢谢。
编辑: 当前路线设置
export class BenefitController {
protected routes: fastify.RouteOptions[];
constructor() {
this.routes = [];
this.setupRoutes();
}
public getRoutes(): fastify.RouteOptions[] {
return this.routes;
}
protected setupRoutes(): void {
this.routes.push({
method: 'POST',
url: '/searchBenefit',
handler: this.searchBenefit,
});
}
new BenefitController().getRoutes().forEach((route: fastify.RouteOptions) => {
fastify.route(route);
});
顺便说一句,当我执行 listIndexes() 时,我确实正确设置了索引 我得到:
{ v: 2, key: { _id: 1 }, name: '_id_', ns: 'some-address.db.benefits' },
{
v: 2,
unique: true,
key: { name: 1 },
name: 'name_1',
ns: 'some-address.db.benefits',
background: true
},
{
v: 2,
key: { details: 1 },
name: 'details_1',
ns: 'some-address.db.benefits',
background: true
},
{
v: 2,
key: { _fts: 'text', _ftsx: 1 },
name: 'details_text',
ns: 'some-address.db.benefits',
background: true,
weights: { details: 1 },
default_language: 'english',
language_override: 'language',
textIndexVersion: 3
}