简体   繁体   中英

wildcard search on field in StackExchange.Redis hash using C#

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM