Customize search

Topics: Customizing Orchard
Mar 2, 2015 at 12:50 AM
Edited Mar 2, 2015 at 12:53 AM

I am new to orchard and MVC. I want to use orderby clause in SearchController for custom module field. Can anyone please guide me on this.

searchHits = _searchService.Query(q, pager.Page, pager.PageSize,
                                               // true,
                                              searchHit => searchHit);
Above is the code from SearchController. What I want is to use order by at

searchSettingPart.SearchedFields.Orderby(....... have to code here .........)

It is asking for some func<string, TKey> key selector which i am not able to use.
Mar 2, 2015 at 1:16 AM
Not tested, but try this
searchSettingPart.SearchedFields.OrderBy(x => x).ToArray()
Mar 2, 2015 at 4:37 AM
I want to use order by for a specific field for eg. Date, Rating, Source. These all are fields of custom module.
Mar 2, 2015 at 5:20 AM
You have to use an IComparer, something like that
class SortByFieldName : IComparer<string> {
    private string _fields = "Date, Rating, Source";

    public int Compare(string x, string y) {
        if (_fields.Contains(x) && !_fields.Contains(y))
            return 1;
        else if (!_fields.Contains(x) && _fields.Contains(y))
            return -1;
            return string.Compare(x, y);
And use it
searchSettingPart.SearchedFields.OrderBy(x => x, new SortByFieldName()).ToArray()
Mar 2, 2015 at 10:46 AM
Not Working :(
Mar 2, 2015 at 11:04 AM
Requirement is : There would be options for user for sorting on different field.

It can be either Date, Rating or Source.
Mar 2, 2015 at 2:20 PM
Sorry, I misunderstood your needs. I first though you only need to re-order this string array

So, I assume you have a custom module that uses the search service, and you want search field options in the front end. To do that, you need to use a template view as "Search.SearchForm.cshtml" (see also in SearchFormPartDriver.cs) and add to the form a "select" html tag populated with the SearchedFields. Then, in your controller you have to use these selected fields with the search service