1

I have a requirement that I needs to search in Redis Cache based on 3 hashKey fields - (clientId - String, securityId - String and transactionDate - Date). I did a basic POC where I implement a simple enough method to find by Id.

Code:

private RedisTemplate<String, EquityFeeds> redisTemplate;

private HashOperations hashOperations;

public EquityFeedsRedisRepositoryImpl(RedisTemplate<String, EquityFeeds> redisTemplate) {
   this.redisTemplate = redisTemplate;
   this.hashOperations = redisTemplate.opsForHash();
}

public EquityFeeds findById(String id) {
    return (EquityFeeds) hashOperations.get("KEY", id);
}

Now how do I search in RedisCache based on the above 3 hashKeys - (clientId - String, securityId - String and transactionDate - Date).

I have a very basic and simple save operation:

Code:

@Override
public void save(EquityFeeds equityFeeds) {
   hashOperations.put("EQUITY", equityFeeds.getId(), equityFeeds);
}

I found only one article of relevance on RediSearch for Java - JRediSearch - RediSearch Java Client https://oss.redislabs.com/redisearch/java_client.html

When I ran a program sample using this site: Code:

public static void main(String[] args) {

        Client client = new Client("EQUITY", "localhost",6379);

        Schema sc = new Schema()
                        .addTagField("clientId")
                        .addTagField("securityId")
                        .addTagField("transactionDate");

        client.createIndex(sc, Client.IndexOptions.defaultOptions());

        Map<String, Object> fields = new HashMap<String, Object>();
        fields.put("clientId", "hello world");
        fields.put("securityId", "lorem ipsum");
        fields.put("transactionDate", "1337");

        client.addDocument("doc1", fields);

        Query q = new Query("hello world");

        SearchResult res = client.search(q);

        System.out.println("Result: "+res.totalResults);                        

    }

I am getting an exception as below:

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'FT.CREATE'

in the line

client.createIndex(sc, Client.IndexOptions.defaultOptions());

I am not getting any resolution for this on the net.

Guy Korland
  • 9,139
  • 14
  • 59
  • 106
sidd
  • 195
  • 3
  • 20
  • Did you consider using Redis module RediSearch? – Guy Korland Mar 30 '20 at 20:03
  • @Guy: No. To be honest I haven't heard about RediSearch. Could you please give me directions on how to use it. Some sample code, or website link perhaps which could help me? I only did a very simple POC involving basic CRUD operations as shown in my code above. – sidd Mar 31 '20 at 01:56
  • https://redisearch.io – Guy Korland Mar 31 '20 at 05:35
  • @Guy: I have edited my post above. I had used JRediSearch - RediSearch Java Client using the link https://oss.redislabs.com/redisearch/java_client.html. But I am getting an exception as mentioned in my post above and not able to get any resolution for the exception in google. I had RedisServer running on my machine as a service. I have windows machine. – sidd Mar 31 '20 at 07:47

1 Answers1

0

It seems like you didn't load the RediSearch module to your Redis server.

See: Quick Start Guide for RediSearch

You can easily run a docker container of Redis with RediSearch pre-loaded:

docker run -p 6379:6379 redislabs/redisearch:latest
Guy Korland
  • 9,139
  • 14
  • 59
  • 106