Search code examples
c#redisredisearch

Using RediSearch with NRediSearch (C#), how to apply OR clause on two numeric fields like i.CountyId == id || i.CityId == id


I am developing a .Net Core web app with RediSearch. Previously I have used in-app memory cache, but I have to change it to distributed cache. The issue is, the web app has very complex searches which must be handled from cache. I though I just move the cache to Redis and must filter in Linq will stay the same just as with in-app memory cache, but if you have 500K+ entries in Redis, using a docker image on the same host took around 4 sec just to load these entries into a variable so this is not an option.

I have now implemented RediSearch with NRediSearch which seem to handle the load pretty well. The issue I have now, I have a search with multiple location fields.

Example

class Item{
    public int Id {get;set;}
    public int CountyId {get;set;}
    public int CityId {get;set;}
}
class Location{
    public int LocationId {get;set;}
    public bool IsCounty {get;set;}
}

I need to translate the following Linq command into RediSearch

public List<Item> Search(List<Location> location){
    var result = items.Where(i => location.Any(l => l.IsCounty && l.Id == i.CountyId) || location.Any(l => !l.IsCounty && i.CityId == l.Id))
}

I have read the RediSearch documentation, but have not found any answer yet. I have to do this in one search because of sorting.


Solution

  • I have found he answer.

    FT.SEARCH idx "@title|body:(hello world) @url|image:mydomain"
    

    Probably if NRediSearch can handle this or operator within a new query, if not, must be implemented straight through it's client