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.

@Sugafree how does your index looks like? Did you split it to 2 indexes, Locations and Items?

@meirsh no I did not. Locations are not stored in Redis, I only use the forgein keys which are stored in the item objects. For this simple example, my index would look like

public override bool CreateIndex()
{
  Schema sch = new Schema();
  sch.AddSortableNumericField("Id");
  sch.AddNumericField("CountyId");
  sch.AddNumericField("CityId");

  return Client.CreateIndex(sch, new Client.ConfiguredIndexOptions(Client.IndexOptions.Default));
}

Hello @sugafree

Do you mind showing the data itself, and not the code, this could help me to understand better. I do not see where the “isCounty” is coming from.

However if you need a OR an a numeric equality you can use the following FT.SEARCH syntax (this may help you)

FT.SEARCH idx  "@county_id:[1001 1001] | @county_id:[1003 1003]"

But once again, it will be better if we can see the “real data” and the query you want to build.

Regards
Tug

Hi,

I think this is exactly what I need. Sorry for being a noob when it comes to RediSearch. From what I can tell, this will return any documents that has a countyId: 1001 or has a cityId: 1003. If this is the case, I am super glad. Thanks @tgrall