0

I went through many posts on SO. Key-Pattern-Search Get-All-Hashesh

I am having some different scenario. I have redis hash for my project. The structure is as follow:

Redis: 
Key - H_SD_C_India 
Field - Ameya_Deshpande_India
Value - JSON

What I need is to search all the documents which contains specific value in field. something like this

Where Field.Contains("Ameya*)

we are using StackExchange.Redis in .Net and their methods to store and get data from redis cache.

There are few options suggested by members like HMSET or HSCAN or SCAN 0 TYPE hash but these are inside CLI.

I tried below method to find data: but didn't get what I expected.

HashGetAsync("H_SD_C_India", "Ameya*");

Please suggest how to do wildcard search from .Net application using StackExchange.Redis

Ameya Deshpande
  • 3,580
  • 4
  • 30
  • 46

1 Answers1

1

It got resolved. I didn't find any direct method in stockExchange.Redis, What we did is executed the command from CLI in ExecuteAsync method SDK.

Code looks like this:

public async Task<List<T>> HashFieldSearch<T>(string hashKey, string HashFieldfilterValue, bool isWildCardSearch)
        {
            var redisData = new List<T>();
            #region With HSCAN and MATCH
            int nextCursor = 0;
            do
            {
                RedisResult matchRedisResultFromCache = await dbCache.ExecuteAsync("HSCAN", new object[] { hashKey, nextCursor.ToString(), "MATCH", isWildCardSearch ? $"{HashFieldfilterValue}*" : HashFieldfilterValue, "COUNT", "1000" });
                var matchRecord = (RedisResult[])matchRedisResultFromCache;
                nextCursor = int.Parse((string)matchRecord[0]);
                List<string> cacheResultCount = ((string[])matchRecord[1]).ToList();
                // If data is present for that cursor then only deserialize it to type otherwise return resultset
                if (cacheResultCount.Count > 0)
                {
                    redisData.Add(JsonConvert.DeserializeObject<T>(CacheGZipHelper.Unzip(((byte[])((RedisValue[])matchRecord[1])[1]))));
                }
            }
            while (nextCursor != 0);
            #endregion With HSCAN and MATCH
            return redisData;
        }

Hope it will help to someone in future.

Ameya Deshpande
  • 3,580
  • 4
  • 30
  • 46
  • Anyway you are always passing 'Hash_Key' to function then how this scan is helpful? If we have 100000 keys and I want to get the key which has Field as 'Ameya' then do I need to pass 100000 keys to function in loop? – Jaydeep Suryawanshi Mar 29 '23 at 12:08