Ordering search results by date?

Jun 14, 2013 at 9:51 PM
It looks that search results are not ordered by date. How can i order them by date so the most recent is first? What do i need to mod in the search logic?
Jun 16, 2013 at 11:11 AM
Edited Jun 16, 2013 at 1:16 PM
The search results are ordered by relevance. The relevance is determined by a simple scoring system. The number of times the search term(or each word in the search term) appears is weighted and totalled. So for a post or page it's like this:

(number of title word matches x 20) +
(number of description word matches x 2) +
(number of body word matches)

The reason I mention this is because your search results might include matches where only one word has been matched(and that might be a body text match). The cautionary point being, if you then sort the results by date, they might not appear to be all that relevant.

That said, you could have a look at OnLoad in search.aspx.cs
protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        rep.ItemDataBound += new RepeaterItemEventHandler(rep_ItemDataBound);

        var term = Request.QueryString["q"];
        if (!Utils.StringIsNullOrWhitespace(term))
        {
            bool includeComments = (Request.QueryString["comment"] == "true");

            var encodedTerm = Server.HtmlEncode(term);
            Page.Title = Server.HtmlEncode(Resources.labels.searchResultsFor) + " '" + encodedTerm + "'";
            h1Headline.InnerHtml = Resources.labels.searchResultsFor + " '" + encodedTerm + "'";

            Uri url;
            if (!Uri.TryCreate(term, UriKind.Absolute, out url))
            {
                List<IPublishable> list = Search.Hits(term, includeComments);

/****************************** sort list by date *************************************************************/

                BindSearchResult(list);
            }
            else
            {
                SearchByApml(url);
            }
        }
        else
        {
            Page.Title = Resources.labels.search;
            h1Headline.InnerHtml = Resources.labels.search;
        }

    }
P.S. The actual search logic is in search.cs in BlogEngine.Core
Another P.S. If you decide to modify the core file, would you mind posting back the changes, it would be interesting to see what you come up with.
Jun 16, 2013 at 9:12 PM
What I was thinking was to have them still based on relevance but just sorted with the most recent first. I use my site as an personal archive so if I want to find something it helps to know the results are chronological so I know where to look.
Jun 16, 2013 at 11:26 PM
For what you are talking about, it would be easy enough to sort simply by most recent.

The above code snippet is from BE 2.8 OnLoad in search.aspx.cs
Versions vary slightly, main thing is to insert single line of code in your version as indicated below.
          
                List<IPublishable> list = Search.Hits(term, includeComments);

//Insert list sort here
                list.Sort(delegate(IPublishable r1, IPublishable r2) { return r2.DateCreated.CompareTo(r1.DateCreated); });

                BindSearchResult(list);
         
The results are still relevant, but since order of relevance is out, I'd be inclined to include a relevance score as part of the result - a bit more work though.