graphql-compose-mongoose - graphql-compose-mongoose 中的全文 mongodb $text 搜索查询
问题描述
我无法弄清楚如何构造一个 graphql 查询来使用文本索引执行 mongodb 全文搜索。https://docs.mongodb.com/manual/text-search/
我已经在 mongoose 模式中的字符串上创建了一个文本索引,但是我在 grapqhl 游乐场中显示的模式中看不到任何内容。
解决方案
有点晚了,虽然我能够像这样实现它
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
}
}
}
推荐阅读
- bash - Unix 命令 Timeout 是否也会杀死该进程的任何子进程?
- javascript - 更新到网格后获取编辑的行单元格值。网格
- c - 为什么我在 OCaml 中的 while 循环不能按预期工作?
- javascript - 使用
我的 React 组件中有一个简单的 HTML 输入标签,定义如下:
<input type="text" onInput={(value) => { search(value); return value;
- javascript - Jquery Modal弹出窗口一遍又一遍地重新渲染
- r - 闪亮的仪表板 highchart 对两个输入做出反应,不起作用
- android - Android:进入多窗口模式时,片段的字段被清除
- symfony - ReflectionException Class 不存在 Symfony 4
- java - Java - 在 Windows 上使用进程 ID 触发正常关闭
- javascript - Mocha - 如何测试未解决的承诺?