[英]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。
包管理器:
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();
}
}
筆記:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.