我是Redis的新手,找不到任何关于查询数据库中特定值然后排序的好文档。我正在尝试从数据库中获取数据,我正在使用NextJs和Redis-om
我尝试在我的/lib/redis.js
中执行此操作
import { Client, Entity, Repository, Schema } from "redis-om";
const client = new Client();
const connect = async () => {
if (!client.isOpen()) {
await client.open(process.env.NEXT_PUBLIC_REDIS_URL);
}
};
class Score extends Entity {}
const schema = new Schema(
Score,
{
address: { type: "string" },
score: { type: "string", textSearch: true },
},
{ dataStructure: "JSON" }
);
export const createScore = async (data) => {
await connect();
const repository = client?.fetchRepository(schema, client);
const score = repository.createEntity();
score.score = data.score;
score.address = data.address;
const id = await repository.save(score);
return id;
};
export const createIndex = async () => {
await connect();
const repository = client.fetchRepository(schema, client);
await repository.createIndex();
};
export const searchScore = async (query) => {
await connect();
const repository = client.fetchRepository(schema, client);
const scores = await repository.search().where("address").matches(query);
return scores;
};
并将数据提取为
const router = useRouter();
const data = router.query;
const query = Object.keys(data)[0];
const fetchScores = async () => {
const res = await fetch("/api/search?" + query);
const results = await res.json();
// console.log(results);
// setScore(results[])
};
fetchScores();
为了简化这个过程,我传递了一个地址,该地址用于检查数据库,如果发现任何地址与该查询地址匹配,则返回该地址。
1条答案
按热度按时间7hiiyaii1#
问题是您对地址的建模不正确。
当您设定:
address: { type: "string" }
您在RediSearch中将address
设置为TAG
字段类型,这不允许全文搜索,而您希望将地址设置为text
类型address: { type: "text" }
,这将支持RediSearch的全文搜索功能。