RedisError: Cannot perform full-text search operations like .match on field of type 'string'

Viewed 28

I'm new to Redis and can't find any good docs on querying database for specific value and then sort it. I'm trying to get data from a database, I'm using NextJs & Redis-om

I tried doing this in my /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;
};

And fetching data as

  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();

To simplify this, I'm passing an address that was meant to check database and if any address is found matching to that query address, return it.

1 Answers

The issue is that you've modeled your address incorrectly.

When you set: address: { type: "string" } you set address up as a TAG field type in RediSearch, which does not allow full-text searches, instead you want to set address to a text type address: { type: "text" } which will support the full-text search capabilities of RediSearch.

Related