Skip to content

Commit

Permalink
Add order by asc/desc
Browse files Browse the repository at this point in the history
  • Loading branch information
mentordigital committed Mar 25, 2020
1 parent e01e4f2 commit 812446e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 10 deletions.
Binary file added LINQToExamine.1.5.1.nupkg
Binary file not shown.
2 changes: 1 addition & 1 deletion LINQToExamine.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>LINQToExamine</id>
<title>Linq To Examine for Umbraco</title>
<version>1.5</version>
<version>1.5.1</version>
<authors>Craig Noble</authors>
<description>This project allows you to query the Lucene indexes using LINQ based on your own classes.</description>
<language>en-US</language>
Expand Down
2 changes: 1 addition & 1 deletion Umbraco.Examine.Linq.Tests/TestSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Umbraco.Examine.Linq.Tests
public class TestSearcher : ISearcher
{

public IEnumerable<SearchResult> Search(string query, int skip, int take)
public IEnumerable<SearchResult> Search(string query, int skip, int take, string orderByField, bool orderByAsc)
{
return new List<SearchResult>();
}
Expand Down
2 changes: 1 addition & 1 deletion Umbraco.Examine.Linq.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void DoSomething()
var results = index.Where(r =>
!(r.Name.Contains("content page")).Fuzzy(0.2).Boost(10)
&& !(r.Name.ContainsAll("something", "else", "hello").Boost(10) || r.Name != "home boo")
).Skip(2).Take(20).ToList();
).Skip(2).Take(20).OrderBy(r => r.CreatedDate).ToList();

//IEnumerable<Result> results = (from r in index.AsQueryable()
// where r.Name.Contains("boo").Boost(10)
Expand Down
3 changes: 2 additions & 1 deletion Umbraco.Examine.Linq/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public IEnumerable<T> ExecuteCollection<T>(QueryModel queryModel)
{
var visitor = new QueryModelVisitor();
visitor.VisitQueryModel(queryModel);
var searchResults = Searcher.Search(string.Join(" AND ", visitor.queries), visitor.skip, visitor.take);

var searchResults = Searcher.Search(string.Join(" AND ", visitor.queries), visitor.skip, visitor.take, visitor.orderByField, visitor.orderByAsc);

return (IEnumerable<T>)Mapper.Map(searchResults);
}
Expand Down
2 changes: 1 addition & 1 deletion Umbraco.Examine.Linq/ISearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace Umbraco.Examine.Linq
{
public interface ISearcher
{
IEnumerable<SearchResult> Search(string query, int skip, int take);
IEnumerable<SearchResult> Search(string query, int skip, int take, string orderByField, bool orderByAsc);
}
}
22 changes: 20 additions & 2 deletions Umbraco.Examine.Linq/QueryModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public class QueryModelVisitor : QueryModelVisitorBase
public List<StringBuilder> queries = new List<StringBuilder>();
public int take = -1;
public int skip = 0;
public string orderByField = "";
public bool orderByAsc = true;

public override void VisitWhereClause(Remotion.Linq.Clauses.WhereClause whereClause, QueryModel queryModel, int index)
{
bool alreadyHasQuery = false;

var query = new StringBuilder();
queries.Add(query);

Expand Down Expand Up @@ -60,5 +60,23 @@ public override void VisitQueryModel(QueryModel queryModel)
}
base.VisitQueryModel(queryModel);
}

public override void VisitOrderByClause(OrderByClause orderByClause, QueryModel queryModel, int index)
{
var expression = orderByClause.Orderings[0].Expression as MemberExpression;
var attr = expression.Member.GetCustomAttributes(typeof(Umbraco.Examine.Linq.Attributes.FieldAttribute), false);

string fieldName = "";

if (attr.Length > 0)
fieldName = ((FieldAttribute)attr[0]).Name;
else
{
fieldName = orderByClause.Orderings[0].Expression.Type.Name;
}

orderByAsc = orderByClause.Orderings[0].OrderingDirection == OrderingDirection.Asc;
orderByField = fieldName;
}
}
}
7 changes: 6 additions & 1 deletion Umbraco.Examine.Linq/SearchProviders/ExamineSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ExamineSearch(string indexName)
IndexName = indexName;
}

public IEnumerable<SearchResult> Search(string query, int skip, int take)
public IEnumerable<SearchResult> Search(string query, int skip, int take, string orderByField, bool orderByAsc)
{
ISearchCriteria criteria = null;
var searcher = ExamineManager.Instance.SearchProviderCollection[IndexName];
Expand All @@ -38,13 +38,18 @@ public IEnumerable<SearchResult> Search(string query, int skip, int take)
{
criteria = searcher.CreateSearchCriteria();
criteria = criteria.RawQuery(query);

if (orderByField != "")
criteria = orderByAsc ? criteria.OrderBy(new[] { orderByField }).Compile() : criteria.OrderByDescending(new[] { orderByField }).Compile();

searchQueryCache.Add(query, criteria);
}

var results = searcher.Search(criteria).Skip(skip);

if (take > -1)
results = results.Take(take);

return results.ToList();
}
}
Expand Down
8 changes: 6 additions & 2 deletions Umbraco.Examine.Linq/SearchProviders/LuceneSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ public LuceneSearch(string indexName)
IndexName = indexName;
}

public IEnumerable<SearchResult> Search(string query, int skip, int take)
public IEnumerable<SearchResult> Search(string query, int skip, int take, string orderByField, bool orderByAsc)
{
LuceneSearcher searcher = ExamineManager.Instance.SearchProviderCollection[IndexName] as LuceneSearcher;
var searchCriteria = searcher.CreateSearchCriteria().RawQuery(query);

var results = searcher.Search(searcher.CreateSearchCriteria().RawQuery(query)).Skip(skip);
if (orderByField != "")
searchCriteria = orderByAsc ? searchCriteria.OrderBy(new[] { orderByField }).Compile() : searchCriteria.OrderByDescending(new[] { orderByField }).Compile();

var results = searcher.Search(searchCriteria).Skip(skip);

if (take > -1)
results = results.Take(take);
Expand Down
1 change: 1 addition & 0 deletions Umbraco.Examine.Linq/Umbraco.Examine.Linq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.4.0.30506.0\lib\net40\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.Helpers.dll</HintPath>
Expand Down

0 comments on commit 812446e

Please sign in to comment.