If you want to sort the search results been rendered. Then I think that you should take a look at the line 211. In there you can add the OrderBy. In the example down here I sort by nodeName.
@foreach (var result in model.PagedResults.OrderBy(r => r.Fields["nodeName"])){ .... }
You can sort this way however you are using linq2objects and therefore stuff is being loaded into memory if you do not have too much content then it should be fine.
Ideally you want lucene to do the sorting for you, although that involves a little bit more work. The createDate and updateDate fields which are standard umbraco fields are in lucene index already as sortable fields so you can sort on them however in ezSearch you will need to drill into the code and at the point where the query is built and the sortby criteria there.
I just talked to the creator of the ezSeach package on Twitter, and here what he said. "it's possible but you'd need to modify the lucene query. It's built to order by most relevant which is what most people would need."
I am not a .NET developer myself, I am working on the frontend :-)
var criteria2 = criteria.RawQuery(query.ToString());
Just before this line you could do
criteria.OrderBy("fieldToOrderBy");
The thing to note however as I pointed out earlier is that if the field you want to order on is standard umbraco field then it should already be in the index as sortable. If you want to use your own custom field like alias "date" then you have 2 options, see http://thecogworks.co.uk/blog/posts/2013/april/examiness-hints-and-tips-from-the-trenches-part-10-document-writing-redux/ there is easy way which is to update ExamineIndex.config and rebuild the index or use code to inject in sortable field. Can you paste the contents of your ExamineIndex.config we can then see from there is the config way is the way to go.
I have the same issue tried various different things but no luck I have essentially adapted the ezSearch for my purpose and all that is remaining is the order by please help:
Query builder:
// Perform the search
var searcher = ExamineManager.Instance.CreateSearchCriteria();
var query = searcher.NodeTypeAlias("umbPropertyDetails");
query.Not().Field("umbracoNaviHide", "1");
// Set search path
var contentPathFilter = model.RootContentNodeId > 0
? string.Format("__IndexType:{0} +searchPath:{1} -template:0", UmbracoExamine.IndexTypes.Content, model.RootContentNodeId)
: string.Format("__IndexType:{0} -template:0", UmbracoExamine.IndexTypes.Content);
var mediaPathFilter = model.RootMediaNodeId > 0
? string.Format("__IndexType:{0} +searchPath:{1}", UmbracoExamine.IndexTypes.Media, model.RootMediaNodeId)
: string.Format("__IndexType:{0}", UmbracoExamine.IndexTypes.Media);
// Ensure page contains all search terms in some way
if (model.Location != "any")
{
query.And().Field("regionID", model.Location);
}
if (model.MinPrice >= 0 && model.MaxPrice > 0)
{
var paddedLower = model.MinPrice.ToString("D6");
var paddedHigher = model.MaxPrice.ToString("D6");
//groupedOr.AppendFormat(String.Format(searchField + ":[{0} TO {1}]", model.MinPrice, model.MaxPrice));
query.And().Range("price", paddedLower, paddedHigher, true, true);
}
if (model.MinBedrooms > 0)
{
query.And().Field("propertyBedrooms", model.MinBedrooms.ToString());
}
var results = ExamineManager.Instance.Search(query.Compile())
.Where(x => (
!Umbraco.IsProtected(int.Parse(x.Fields["id"]), x.Fields["path"]) ||
(
Umbraco.IsProtected(int.Parse(x.Fields["id"]), x.Fields["path"]) &&
Umbraco.MemberHasAccess(int.Parse(x.Fields["id"]), x.Fields["path"])
)) && (
(x.Fields["__IndexType"] == UmbracoExamine.IndexTypes.Content && Umbraco.TypedContent(int.Parse(x.Fields["id"])) != null) ||
(x.Fields["__IndexType"] == UmbracoExamine.IndexTypes.Media && Umbraco.TypedMedia(int.Parse(x.Fields["id"])) != null)
))
.ToList();
model.AllResults = results;
model.TotalResults = results.Count;
model.TotalPages = (int)Math.Ceiling((decimal)model.TotalResults / model.PageSize);
model.CurrentPage = Math.Max(1, Math.Min(model.TotalPages, model.CurrentPage));
// Page the results
model.PagedResults = model.AllResults.Skip(model.PageSize * (model.CurrentPage - 1)).Take(model.PageSize).OrderBy(r => r.Fields["price"]);
LogHelper.Debug<string>("[ezSearch] Searching Lucene with the following query: " + query.ToString());
You are sorting by linq not examine. In fact after your main search you are additionally filtering with linq. The protected stuff you could inject that into your indexing using gathering node then you could use examine to query filter if doc is protected or not.
Additionally the price field that you are ordering by you need to inject that into the index as well then you can order using examine.
Ezsearch - sorting
Hello,
I am using ezsearch package on a websire and I want to do a sorting in the results. Can someone please advise on how to proceed.
Thanks in advance,
kusum
Hi Kusum
What kind of sorting do you want to do? And is it when the search results have been rendered initially or is it before the search has been executed?
And what exact version of Umbraco are you using? (It's just nice to know).
/Jan
Hi Kusum,
If you want to sort the search results been rendered. Then I think that you should take a look at the line 211. In there you can add the OrderBy. In the example down here I sort by nodeName.
Hope this helps,
/Dennis
Hi Dennis ,
Thanks for the help. But I want to sort by latest date. I am trying CreateDate or UpdateDate but it's not working. If you could advise.
Thanks,
kusum
Hello jan,
I am using umbraco7 and ezsearch 1.2
Thanks,
kusum
Hi Kusum
Ok, what does your code look like? Could you please share?
/Jan
Hi Kusum,
Then I think that you can do something like this,
For using the create date.
For using the update date.
Hope this helps,
/Dennis
Guys,
You can sort this way however you are using linq2objects and therefore stuff is being loaded into memory if you do not have too much content then it should be fine.
Ideally you want lucene to do the sorting for you, although that involves a little bit more work. The createDate and updateDate fields which are standard umbraco fields are in lucene index already as sortable fields so you can sort on them however in ezSearch you will need to drill into the code and at the point where the query is built and the sortby criteria there.
If you want to sort by fields that are not injected in as sortable you then have 2 options to inject those fields as sortable, the easy way which is to modify the examine config file and for the field add sortable=true or using document.writing event see http://thecogworks.co.uk/blog/posts/2013/april/examiness-hints-and-tips-from-the-trenches-part-10-document-writing-redux/ for information.
Regards
Ismail
Hi Dennis,
It worked thanks a lot. And if i want to sort by an alias exemple "date". I have tried many options but not working. If you please help.
Thanks,
kusum
Hi kusum,
I just talked to the creator of the ezSeach package on Twitter, and here what he said. "it's possible but you'd need to modify the lucene query. It's built to order by most relevant which is what most people would need."
I am not a .NET developer myself, I am working on the frontend :-)
Hope this can help you further.
/Dennis
Kusum,
If you look at the source code of the macro partial https://github.com/mattbrailsford/ezSearch/blob/master/Src/Our.Umbraco.ezSearch/Web/UI/Views/MacroPartials/ezSearch.cshtml line 135. You have
Just before this line you could do
criteria.OrderBy("fieldToOrderBy");
The thing to note however as I pointed out earlier is that if the field you want to order on is standard umbraco field then it should already be in the index as sortable. If you want to use your own custom field like alias "date" then you have 2 options, see http://thecogworks.co.uk/blog/posts/2013/april/examiness-hints-and-tips-from-the-trenches-part-10-document-writing-redux/ there is easy way which is to update ExamineIndex.config and rebuild the index or use code to inject in sortable field. Can you paste the contents of your ExamineIndex.config we can then see from there is the config way is the way to go.
Regards
Ismail
I have the same issue tried various different things but no luck I have essentially adapted the ezSearch for my purpose and all that is remaining is the order by please help:
Query builder:
Andrew,
You are sorting by linq not examine. In fact after your main search you are additionally filtering with linq. The protected stuff you could inject that into your indexing using gathering node then you could use examine to query filter if doc is protected or not.
Additionally the price field that you are ordering by you need to inject that into the index as well then you can order using examine.
Regards
Ismail
Sorry just seen this Im sorry this went over my head a bit can you advise what needs amending please so this re-orders by price.
Many Thanks
is working on a reply...