0

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

我现在已经实现RediSearchNRediSearch似乎可以很好地处理负载的方法。我现在遇到的问题是,我搜索了多个位置字段。

例子

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文档,但尚未找到任何答案。由于排序,我必须在一次搜索中执行此操作。

4

2 回答 2

1

我找到了他的答案。

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

可能 ifNRediSearch可以在新查询中处理这个 or 运算符,如果不能,则必须直接通过它的客户端实现

于 2020-08-30T15:06:45.887 回答
0

你可以使用我在 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

点网命令行:

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 保存您的数据,否则它将无法正常工作。
  • 没有进行性能测试。如果我可以作为开源获得支持,它可能会更好。
于 2020-09-05T22:15:45.837 回答