簡體   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