繁体   English   中英

将 RediSearch 与 NRediSearch (C#) 一起使用,如何在两个数字字段上应用 OR 子句,例如 i.CountyId == id || i.CityId == id

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

我正在使用 RediSearch 开发.Net Core Web 应用程序。 之前我使用了应用内内存缓存,但我必须将其更改为分布式缓存。 问题是,Web 应用程序具有非常复杂的搜索,必须从缓存中处理。 我虽然只是将缓存移动到Redis并且必须在Linq过滤将保持与应用程序内内存缓存相同,但是如果您在 Redis 中有 500K+ 条目,则在同一主机上使用 docker 映像大约需要 4 秒将这些条目加载到变量中,因此这不是一个选项。

我现在RediSearch使用NRediSearch实现了RediSearch ,它似乎可以很好地处理负载。 我现在遇到的问题是,我搜索了多个位置字段。

例子

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;}
}

我需要将以下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))
}

我已经阅读了RediSearch文档,但还没有找到任何答案。 由于排序,我必须在一次搜索中完成此操作。

我找到了他的答案。

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

可能如果NRediSearch可以在新查询中处理此或运算符,如果不能,则必须直接通过其客户端实现

您可以使用我在 nredisearch 上构建的库。 但它适用于简单的 linq 查询。 它不适用于嵌套对象。 如果你为它的发展做出贡献,我会很高兴。

如果我们谈论您的示例,您必须为每个位置复制数据,如下所示:

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;}
}

你可以用我的图书馆搜索这个 daha。

RedisworkCore

包管理器:

Install-Package RedisworkCore

点网 CLI:

dotnet add package RedisworkCore

您可以创建类似实体框架的上下文:

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) { }
}

那么你可以使用 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();
  }
  
}

笔记:

  • 您应该使用 redisworkcore 保存您的数据,否则它将无法工作。
  • 没有进行性能测试。 如果我可以作为开源获得支持,它可能会更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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