gpt4 book ai didi

graphql-compose-mongoose - graphql-compose-mongoose 中的全文 mongodb $text 搜索查询

转载 作者:行者123 更新时间:2023-12-02 19:36:16 26 4
gpt4 key购买 nike

我无法弄清楚如何构建 graphql 查询来使用文本索引执行 mongodb 全文搜索。 https://docs.mongodb.com/manual/text-search/

我已经在 mongoose 架构中的字符串上创建了一个文本索引,但在 grapqhl Playground 中显示的架构中没有看到任何内容。

最佳答案

有点晚了,尽管我能够像这样实现

const FacilitySchema: Schema = new Schema(
{
name: { type: String, required: true, maxlength: 50, text: true },
short_description: { type: String, required: true, maxlength: 150, text: true },
description: { type: String, maxlength: 1000 },
location: { type: LocationSchema, required: true },
},
{
timestamps: true,
}
);

FacilitySchema.index(
{
name: 'text',
short_description: 'text',
'category.name': 'text',
'location.address': 'text',
'location.city': 'text',
'location.state': 'text',
'location.country': 'text',
},
{
name: 'FacilitiesTextIndex',
default_language: 'english',
weights: {
name: 10,
short_description: 5,
// rest fields get weight equals to 1
},
}
);

为模型创建 ObjectTypeComposer 后,添加此

const paginationResolver = FacilityTC.getResolver('pagination').addFilterArg({
name: 'search',
type: 'String',
query: (query, value, resolveParams) => {
resolveParams.args.sort = {
score: { $meta: 'textScore' },
};
query.$text = { $search: value, $language: 'en' };
resolveParams.projection.score = { $meta: 'textScore' };
},
});

FacilityTC.setResolver('pagination', paginationResolver);

然后你可以像这样分配


const schemaComposer = new SchemaComposer();

schemaComposer.Query.addFields({
// ...
facilities: Facility.getResolver('pagination')
// ...
});

在您的客户端,像这样执行查询

{
facilities(filter: { search: "akure" }) {
count
items {
name
}
}
}

关于graphql-compose-mongoose - graphql-compose-mongoose 中的全文 mongodb $text 搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61017389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com