[英]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.我正在使用 RediSearch 开发
.Net Core
Web 应用程序。 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.
问题是,Web 应用程序具有非常复杂的搜索,必须从缓存中处理。 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.我虽然只是将缓存移动到
Redis
并且必须在Linq
过滤将保持与应用程序内内存缓存相同,但是如果您在 Redis 中有 500K+ 条目,则在同一主机上使用 docker 映像大约需要 4 秒将这些条目加载到变量中,因此这不是一个选项。
I have now implemented RediSearch
with NRediSearch
which seem to handle the load pretty well.我现在
RediSearch
使用NRediSearch
实现了RediSearch
,它似乎可以很好地处理负载。 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
我需要将以下
Linq
命令翻译成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.我已经阅读了
RediSearch
文档,但还没有找到任何答案。 I have to do this in one search because of sorting.由于排序,我必须在一次搜索中完成此操作。
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可能如果
NRediSearch
可以在新查询中处理此或运算符,如果不能,则必须直接通过其客户端实现
you can use my library i build over nredisearch.您可以使用我在 nredisearch 上构建的库。 but it works for simple linq queries.
但它适用于简单的 linq 查询。 its not work on nested objects.
它不适用于嵌套对象。 I will be glad if you contribute to its development.
如果你为它的发展做出贡献,我会很高兴。
if we talk about your example you have to duplicate your data for each location like this:如果我们谈论您的示例,您必须为每个位置复制数据,如下所示:
class Item {
public int Id {get;set;}
public int CountyId {get;set;}
public int CityId {get;set;}
public int LocationId {get;set;}
public bool IsCounty {get;set;}
}
and you can search this daha with my library.你可以用我的图书馆搜索这个 daha。
PackageManager:包管理器:
Install-Package RedisworkCore
Dotnet CLI:点网 CLI:
dotnet add package RedisworkCore
and you can create context like entityframework:您可以创建类似实体框架的上下文:
public class Person
{
[RedisKey(0)]
public int Id { get; set; }
public string Name { get; set; }
public string Lastname { get; set; }
}
public class SimpleContext : RedisContext
{
public Rediset<Person> Persons { get; set; }
public SimpleContext(RedisContextOptions options) : base(options) { }
}
then you can use Find, Where, Any, All, SortBy, SortByDescending, Skip, Take similar LINQ.那么你可以使用 Find、Where、Any、All、SortBy、SortByDescending、Skip、Take 类似的 LINQ。
static async Task Main(string[] args)
{
RedisContextOptions options = new RedisContextOptions
{
HostAndPort = "localhost:6379"
};
using (var context = new SimpleContext(options))
{
var person = new Person
{
Id = 26,
Name = "Emre",
Lastname = "Hızlı"
};
context.Persons.Add(person);
await context.SaveChangesAsync();
}
using (var context = new SimpleContext(options))
{
Person person = await context.Persons.Find(26);
List<Person> persons = await context.Persons.Where(x => x.Id > 0).ToListAsync();
}
}
Notes:笔记:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.