我有一个带博客的应用程序,我使用 redis 作为数据库,使用 redis-om 来管理它,应用程序中的博客模型是: 博客类扩展实体{}
const model_blog = new Schema(Blog,{
title : {type : "string"},
description : {type : "string"},
body : {type : "text"},
image : {type : "string"},
}, {
dataStructure: 'JSON'
})
我可以插入数据,但我可以从中获取所有数据 插入代码:
const clinet = new Client()
const create_blog = async (data)=>{
clinet.open('redis://127.0.0.1:6379')
const blog_repository = clinet.fetchRepository(model_blog)
const blog = await blog_repository.createAndSave(data)
return blog
}
获取所有代码:
const clinet = new Client()
const get_all_blogs = async ()=>{
clinet.open('redis://127.0.0.1:6379')
const blog_repository = clinet.fetchRepository(model_blog)
const blog =await blog_repository.search().return.all()
return blog
}
这是文本错误:
error - { name: 'Error', source: 'server', message: 'Blog:index: no such index' }
我想在调用get_all_blogs函数时获取我redis数据库中所有的Blog键
看起来您没有为 RediSearch 创建索引以供使用。在存储库上,您需要通过调用恰当命名的
.createIndex
创建索引。 ;)
await blog_repository.createIndex()
这里是链接了解更多详情。
此外,您实际上只需要获取存储库一次。重用它会稍微更有效率。而且,作为奖励,您可以在创建它的位置调用
.createIndex()
,因为您当然只想在应用程序启动时调用 .createIndex()
一次。