Skip to content

Commit

Permalink
SearchCommands extensions updates to remove usage of search term para…
Browse files Browse the repository at this point in the history
…meter
  • Loading branch information
melittleman committed Feb 23, 2024
1 parent abb9828 commit 02b9079
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
36 changes: 14 additions & 22 deletions src/Querying/Extensions/SearchCommandsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,14 @@ public static async Task<bool> CreateIndexAsync(
public static async Task<IPagedList<T>> SearchAsync<T>(
this SearchCommands search,
string indexName,
string searchTerm,
SearchFilter filter,
params string[] highlightFields)
{
ArgumentNullException.ThrowIfNull(search);
ArgumentNullException.ThrowIfNull(indexName);
ArgumentNullException.ThrowIfNull(searchTerm);
ArgumentNullException.ThrowIfNull(filter);

Query query = GetPagedQuery(searchTerm, filter, false, highlightFields);
Query query = GetPagedQuery(filter, false, highlightFields);

try
{
Expand All @@ -84,17 +82,17 @@ public static async Task<IPagedList<T>> SearchAsync<T>(
}
}

public static async Task<T?> SearchSingleAsync<T>(this SearchCommands search, string indexName, string searchTerm)
public static async Task<T?> SearchSingleAsync<T>(this SearchCommands search, string indexName, string searchQuery)
{
ArgumentNullException.ThrowIfNull(search);
ArgumentNullException.ThrowIfNull(indexName);
ArgumentNullException.ThrowIfNull(searchTerm);
ArgumentNullException.ThrowIfNull(searchQuery);

SearchFilter filter = new(1, 1);
SearchFilter filter = new(page: 1, count: 1, query: searchQuery);

// TODO: try catch...

SearchResult result = await search.SearchAsync(indexName, GetPagedQuery(searchTerm, filter));
SearchResult result = await search.SearchAsync(indexName, GetPagedQuery(filter));

return result?.Documents is not null
? ConvertTo<T>(result.Documents).SingleOrDefault()
Expand Down Expand Up @@ -125,11 +123,11 @@ public static async Task<ICollection<T>> SearchAllAsync<T>(this SearchCommands s

// Provide a default value of starting at
// page 1, with 100 results per page.
filter ??= new SearchFilter(1, 100);
filter ??= new SearchFilter(page: 1, count: 100);

// TODO: try catch...

IPagedList<T> results = await search.SearchAsync<T>(indexName, "*", filter);
IPagedList<T> results = await search.SearchAsync<T>(indexName, filter);

// We must have managed to retrieve all results
// in the first page, return them as-is.
Expand All @@ -154,7 +152,7 @@ public static async Task<ICollection<T>> SearchAllAsync<T>(this SearchCommands s
// a transaction and execute as a batch?
// Not too worried at the moment as it's unlikely we'll
// have thousands of results right now at least.
results = await search.SearchAsync<T>(indexName, "*", filter);
results = await search.SearchAsync<T>(indexName, filter);

documents.AddRange(results);
}
Expand Down Expand Up @@ -187,57 +185,51 @@ private static async Task<bool> IndexExistsAsync(SearchCommands search, string i
}

private static Query GetPagedQuery(
string searchTerm,
SearchFilter filter,
bool summarize = false,
params string[] highlightFields)
{
ArgumentNullException.ThrowIfNull(searchTerm);
ArgumentNullException.ThrowIfNull(filter);

Query builder;

if (filter.OrderBy is not null)
{
builder = GetSortedQuery(searchTerm, filter, summarize, highlightFields);
builder = GetSortedQuery(filter, summarize, highlightFields);
}
else
{
builder = GetDefaultQuery(searchTerm, summarize, highlightFields);
builder = GetDefaultQuery(filter.Query, summarize, highlightFields);
}

return builder.Limit((filter.Page - 1) * filter.Count, filter.Count);
}

private static Query GetSortedQuery(
string searchTerm,
SearchFilter filter,
bool summarize = false,
params string[] highlights)
{
ArgumentNullException.ThrowIfNull(searchTerm);
ArgumentNullException.ThrowIfNull(filter);

Query builder = GetDefaultQuery(searchTerm, summarize, highlights);
Query builder = GetDefaultQuery(filter.Query, summarize, highlights);

return filter.OrderBy is not null
? builder.SetSortBy(filter.OrderBy, filter.SortBy is SortDirection.Ascending)
: builder;
}

private static Query GetDefaultQuery(
string searchTerm,
string? searchQuery,
bool summarize = false,
params string[] highlights)
{
ArgumentNullException.ThrowIfNull(searchTerm);

Query builder = new(searchTerm);
Query builder = new(searchQuery ?? "*");

if (highlights.Length > 0)
{
// https://oss.redislabs.com/redisearch/Highlight/#highlighting
builder.HighlightFields(new Query.HighlightTags("<span class=\"search-term-found\">", "</span>"), highlights);
builder.HighlightFields(new Query.HighlightTags("<span class=\"query-found\">", "</span>"), highlights);
}

// https://oss.redislabs.com/redisearch/Highlight/#summarization
Expand Down
23 changes: 21 additions & 2 deletions src/Querying/SearchFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public byte Count

public SearchFilter() { }

// TODO: Should these contructors also provide the default values?
public SearchFilter(byte count)
{
Count = count;
Expand All @@ -83,17 +84,35 @@ public SearchFilter(short page, byte count)
Count = count;
}

public SearchFilter(short page, byte count, string orderBy)
public SearchFilter(short page, byte count, string query)
{
Page = page;
Count = count;
Query = query;
}

public SearchFilter(
short page,
byte count,
string query,
string orderBy)
{
Page = page;
Count = count;
Query = query;
OrderBy = orderBy;
}

public SearchFilter(short page, byte count, string orderBy, SortDirection sortBy)
public SearchFilter(
short page,
byte count,
string query,
string orderBy,
SortDirection sortBy)
{
Page = page;
Count = count;
Query = query;
OrderBy = orderBy;
SortBy = sortBy;
}
Expand Down

0 comments on commit 02b9079

Please sign in to comment.