From 987e0c8e5f398ffffdad5ebdc597f51b99f1e82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Kottal?= Date: Wed, 17 Apr 2024 15:50:51 +0200 Subject: [PATCH] Adds ability to set ordering of search --- .../Interfaces/ISearch.cs | 5 ++++- .../Models/OrderDirection.cs | 7 +++++++ .../Models/Search.cs | 13 +++++++++++- .../Services/SearchService.cs | 20 ++++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/Our.Umbraco.FullTextSearch/Models/OrderDirection.cs diff --git a/src/Our.Umbraco.FullTextSearch/Interfaces/ISearch.cs b/src/Our.Umbraco.FullTextSearch/Interfaces/ISearch.cs index ce2d61e..e06e726 100644 --- a/src/Our.Umbraco.FullTextSearch/Interfaces/ISearch.cs +++ b/src/Our.Umbraco.FullTextSearch/Interfaces/ISearch.cs @@ -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 @@ -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); diff --git a/src/Our.Umbraco.FullTextSearch/Models/OrderDirection.cs b/src/Our.Umbraco.FullTextSearch/Models/OrderDirection.cs new file mode 100644 index 0000000..21a24b5 --- /dev/null +++ b/src/Our.Umbraco.FullTextSearch/Models/OrderDirection.cs @@ -0,0 +1,7 @@ +namespace Our.Umbraco.FullTextSearch.Models; + +public enum OrderDirection +{ + Ascending, + Descending +} diff --git a/src/Our.Umbraco.FullTextSearch/Models/Search.cs b/src/Our.Umbraco.FullTextSearch/Models/Search.cs index 47dbe79..97ef9c4 100644 --- a/src/Our.Umbraco.FullTextSearch/Models/Search.cs +++ b/src/Our.Umbraco.FullTextSearch/Models/Search.cs @@ -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; @@ -53,6 +54,9 @@ public Search(string searchTerm) public ICollection SearchTermSplit => new List { QueryParser.Escape(SearchTerm) }; + public SortableField[] OrderByFields { get; set; } + public OrderDirection OrderDirection { get; set; } = OrderDirection.Descending; + public Search SetSearchType(SearchType searchType) { SearchType = searchType; @@ -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; + } } } diff --git a/src/Our.Umbraco.FullTextSearch/Services/SearchService.cs b/src/Our.Umbraco.FullTextSearch/Services/SearchService.cs index 5202fd6..0709869 100644 --- a/src/Our.Umbraco.FullTextSearch/Services/SearchService.cs +++ b/src/Our.Umbraco.FullTextSearch/Services/SearchService.cs @@ -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;