Skip to content

Commit

Permalink
Adds ability to set ordering of search
Browse files Browse the repository at this point in the history
  • Loading branch information
skttl committed Apr 17, 2024
1 parent 3a81d93 commit 987e0c8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Our.Umbraco.FullTextSearch/Interfaces/ISearch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Our.Umbraco.FullTextSearch.Models;
using Examine.Search;
using Our.Umbraco.FullTextSearch.Models;
using System.Collections.Generic;

namespace Our.Umbraco.FullTextSearch.Interfaces
Expand Down Expand Up @@ -27,6 +28,8 @@ public interface ISearch
string CustomQuery { get; set; }
string Searcher { get; set; }
string Index { get; set; }
SortableField[] OrderByFields { get; set; }
OrderDirection OrderDirection { get; set; }

Search SetIndex(string index);
Search SetSearcher(string searcher);
Expand Down
7 changes: 7 additions & 0 deletions src/Our.Umbraco.FullTextSearch/Models/OrderDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Our.Umbraco.FullTextSearch.Models;

public enum OrderDirection
{
Ascending,
Descending
}
13 changes: 12 additions & 1 deletion src/Our.Umbraco.FullTextSearch/Models/Search.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.QueryParsers.Classic;
using Examine.Search;
using Lucene.Net.QueryParsers.Classic;
using Our.Umbraco.FullTextSearch.Interfaces;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -53,6 +54,9 @@ public Search(string searchTerm)

public ICollection<string> SearchTermSplit => new List<string> { QueryParser.Escape(SearchTerm) };

public SortableField[] OrderByFields { get; set; }
public OrderDirection OrderDirection { get; set; } = OrderDirection.Descending;

public Search SetSearchType(SearchType searchType)
{
SearchType = searchType;
Expand Down Expand Up @@ -324,5 +328,12 @@ public Search SetCustomQuery(string customQuery)
CustomQuery = customQuery;
return this;
}

public Search OrderBy(SortableField[] orderBy, OrderDirection direction = OrderDirection.Ascending)
{
OrderByFields = orderBy;
OrderDirection = direction;
return this;
}
}
}
20 changes: 19 additions & 1 deletion src/Our.Umbraco.FullTextSearch/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,25 @@ private ISearchResults GetResults()
if (searcher != null)
{
_logger.LogDebug("Trying to search for {query}", query.ToString());
return searcher.CreateQuery().NativeQuery(query.ToString()).Execute(new Examine.Search.QueryOptions(_search.PageLength * (_currentPage - 1), _search.PageLength));

var searchQuery = searcher.CreateQuery().NativeQuery(query.ToString());
var queryOptions = new Examine.Search.QueryOptions(_search.PageLength * (_currentPage - 1), _search.PageLength);

if (_search.OrderByFields?.Length > 0)
{
if (_search.OrderDirection is OrderDirection.Descending)
{
return searchQuery.OrderByDescending(_search.OrderByFields).Execute(queryOptions);
}
else
{
return searchQuery.OrderBy(_search.OrderByFields).Execute(queryOptions);
}
}
else
{
return searchQuery.Execute(queryOptions);
}
}

return null;
Expand Down

0 comments on commit 987e0c8

Please sign in to comment.