Skip to content

Commit

Permalink
FT.Search TotalResults
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Oct 22, 2024
1 parent e416537 commit 4321909
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 35 deletions.
25 changes: 25 additions & 0 deletions src/FreeRedis/FreeRedis.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeRedis", "FreeRedis.csproj", "{9C8F4D2B-B78E-43B1-BF70-1B6100C57BCA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9C8F4D2B-B78E-43B1-BF70-1B6100C57BCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C8F4D2B-B78E-43B1-BF70-1B6100C57BCA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C8F4D2B-B78E-43B1-BF70-1B6100C57BCA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C8F4D2B-B78E-43B1-BF70-1B6100C57BCA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0488E242-36D8-4669-8CFA-05A7F1D9C8EF}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public AggregationResult Execute()
private long _timeout = -1;
private List<object> _groupBy = new List<object>();
private List<AggregateReduce> _groupByReduces = new List<AggregateReduce>();
private List<object> _sortBy;
private List<object> _sortBy = new List<object>();
private int _sortByMax;
private List<object> _applies;
private List<object> _applies = new List<object>();
private long _limitOffset, _limitNum = 10;
private string _filter;
private bool _withCursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,14 @@ internal FtDocumentRepositorySearchBuilder(FtDocumentRepository<T> repository, s
_searchBuilder = new SearchBuilder(_repository._client, index, query);
}

public List<T> ToList()
public List<T> ToList() => ToList(out var _);
public List<T> ToList(out long total)
{
var prefix = _repository._schema.DocumentAttribute.Prefix;
var keyProperty = _repository._schema.KeyProperty;
var docs = _searchBuilder.Execute();
return docs.Select(doc => {
var result = _searchBuilder.Execute();
total = result.Total;
return result.Documents.Select(doc => {
var item = doc.Body.MapToClass<T>();
var itemId = doc.Id;
if (!string.IsNullOrEmpty(prefix))
Expand Down
23 changes: 16 additions & 7 deletions src/FreeRedis/RedisClient/Modules/RediSearch/SearchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

namespace FreeRedis.RediSearch
{
public class SearchResult
{
public long Total { get; }
public List<Document> Documents { get; }

public SearchResult(long total, List<Document> docs)
{
Total = total;
Documents = docs;
}
}
public class Document
{
public string Id { get; }
Expand Down Expand Up @@ -104,15 +115,13 @@ internal CommandPacket GetCommandPacket()
.InputIf(_dialect > 0, "DIALECT", _dialect);
return cmd;
}
public List<Document> Execute()
public SearchResult Execute()
{
var cmd = GetCommandPacket();
return _redis.Call(cmd, rt => rt.ThrowOrValue((a, _) =>
{
var docs = new List<Document>();
if (a.Any() != true) return docs;
var totalResults = a[0].ConvertTo<int>();
if (totalResults < 1) return docs;
var ret = new SearchResult(a[0].ConvertTo<long>(), new List<Document>());
if (a.Any() != true || ret.Total <= 0) return ret;
int step = 1;
int scoreOffset = 0;
Expand Down Expand Up @@ -145,9 +154,9 @@ public List<Document> Execute()
if (_withScores) score = a[x + scoreOffset].ConvertTo<double>();
if (_withPayloads) payload = a[x + payloadOffset].ConvertTo<byte[]>();
if (_noContent == false) fieldValues = a[x + contentOffset].ConvertTo<object[]>();
docs.Add(Document.Load(id, score, payload, fieldValues, scoreExplained));
ret.Documents.Add(Document.Load(id, score, payload, fieldValues, scoreExplained));
}
return docs;
return ret;
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RediSearchTests : TestBase

protected static ConnectionStringBuilder Connection = new ConnectionStringBuilder()
{
Host = "8.154.26.11",
Host = "8.154.26.119",
MaxPoolSize = 10,
Protocol = RedisProtocol.RESP2,
ClientName = "FreeRedis",
Expand Down Expand Up @@ -54,6 +54,8 @@ class TestDoc
[Fact]
public void FtDocumentRepository()
{
var connStr = Connection.ToString();

var repo = cli.FtDocumentRepository<TestDoc>();

try
Expand Down Expand Up @@ -187,28 +189,28 @@ public void FtAggregate()
cli.HSet("blog:post:2", "title", "prefix测试标题2", "content", "测试infix内容2", "author", "作者2,作者3", "created_date", "10001", "views", 201);
cli.HSet("blog:post:3", "title", "测试标题3 word", "content", "测试word内容3", "author", "作者2,作者5", "created_date", "10002", "views", 301);

var list = cli.FtSearch(idxName, "word").Execute();
list = cli.FtSearch(idxName, "@title:word").Execute();
list = cli.FtSearch(idxName, "prefix*").Execute();
list = cli.FtSearch(idxName, "@title:prefix*").Execute();
list = cli.FtSearch(idxName, "*suffix").Execute();
list = cli.FtSearch(idxName, "*infix*").Execute();
list = cli.FtSearch(idxName, "%word%").Execute();


list = cli.FtSearch(idxName, "@views:[200 300]").Execute();
list = cli.FtSearch(idxName, "@views:[-inf 2000]").SortBy("views").Limit(0, 5).Execute();
list = cli.FtSearch(idxName, "@views:[(200 (300]").Execute();
list = cli.FtSearch(idxName, "@views>=200").Dialect(4).Execute();
list = cli.FtSearch(idxName, "@views:[200 +inf]").Execute();
list = cli.FtSearch(idxName, "@views<=300").Dialect(4).Execute();
list = cli.FtSearch(idxName, "@views:[-inf 300]").Execute();
list = cli.FtSearch(idxName, "@views==200").Dialect(4).Execute();
list = cli.FtSearch(idxName, "@views:[200 200]").Execute();
list = cli.FtSearch(idxName, "@views!=200").Dialect(4).Execute();
list = cli.FtSearch(idxName, "-@views:[200 200]").Execute();
list = cli.FtSearch(idxName, "@views==200 | @views==300").Dialect(4).Execute();
list = cli.FtSearch(idxName, "*").Filter("views", 200, 300).Dialect(4).Execute();
var list = cli.FtAggregate(idxName, "word").Execute();
list = cli.FtAggregate(idxName, "@title:word").Execute();
list = cli.FtAggregate(idxName, "prefix*").Execute();
list = cli.FtAggregate(idxName, "@title:prefix*").Execute();
list = cli.FtAggregate(idxName, "*suffix").Execute();
list = cli.FtAggregate(idxName, "*infix*").Execute();
list = cli.FtAggregate(idxName, "%word%").Execute();


list = cli.FtAggregate(idxName, "@views:[200 300]").Execute();
list = cli.FtAggregate(idxName, "@views:[-inf 2000]").SortBy("views").Limit(0, 5).Execute();
list = cli.FtAggregate(idxName, "@views:[(200 (300]").Execute();
list = cli.FtAggregate(idxName, "@views>=200").Dialect(4).Execute();
list = cli.FtAggregate(idxName, "@views:[200 +inf]").Execute();
list = cli.FtAggregate(idxName, "@views<=300").Dialect(4).Execute();
list = cli.FtAggregate(idxName, "@views:[-inf 300]").Execute();
list = cli.FtAggregate(idxName, "@views==200").Dialect(4).Execute();
list = cli.FtAggregate(idxName, "@views:[200 200]").Execute();
list = cli.FtAggregate(idxName, "@views!=200").Dialect(4).Execute();
list = cli.FtAggregate(idxName, "-@views:[200 200]").Execute();
list = cli.FtAggregate(idxName, "@views==200 | @views==300").Dialect(4).Execute();
list = cli.FtAggregate(idxName, "*").Filter("views:[200 300]").Dialect(4).Execute();
}

[Fact]
Expand Down

0 comments on commit 4321909

Please sign in to comment.