Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lucy Foster 10 posts 82 karma points
    Apr 26, 2018 @ 16:45
    Lucy Foster
    0

    Hi All,

    I'm having some issues with the examine indexes order by. I have a search which looks for multiple document types, however these document types have different alias's needed for the publish date.

    All news articles and video articles in the site use a date picker called publishDate all other document types searched need to use the createDate alias. This can't be changed.

    Is there a way with examine index to create an if statement (or similar) to say if it has these alias orderby publishDate else orderby createDate? I haven't done much beyond basic searches before with examine.

    Below is my current code:

         var Searcher = Examine.ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
        //Setup search criteria
        var searchCriteria = Searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.And);
        //build the query
        var query = searchCriteria.GroupedOr(new string[] {"__NodeTypeAlias"}, "newsArticle", "videoArticle", "project", "expedition", "volunteering", "fundraising");
        if (!String.IsNullOrWhiteSpace(Request.QueryString["keywords"]))
        {
            query = query.And().GroupedOr(new string[] { "nodeName", "articleText", "Name", "artIntroduction" }, Request.QueryString["keywords"]);
        }
        var SearchResults = Searcher.Search(query.And().OrderBy("createDate").Or().NodeTypeAlias("newsArticle").And().OrderByDescending("publishDate").Compile());
    

    Thanks, Lucy

  • Mila Pandurska 43 posts 190 karma points
    Apr 26, 2018 @ 22:09
    Mila Pandurska
    0

    Hi, Lucy, You can run your query without order. Your result will be of type ISearchResult. Then you can check if the items have publishedDate. Someting like this:

    var SearchResults = Searcher.Search(query.Compile());
     var orderedItems = SearchResults .OrderByDescending(a => a.Fields["publishDate"]).ThenBy(a => a.Fields["createDate"]);
    

    Another option which I recommend is to use GatheringNodeData event. You can create search field sortDate in your IndexSet and do this check:

    private void OnExamineGatheringNodeData(IndexWriter indexWriter, object sender, IndexingNodeDataEventArgs e)
        {
            var content = _contentService.GetPublishedVersion(e.NodeId);
            if (content != null)
            {
                try
                {
    
                    switch (content.ContentType.Alias)
                    {
                        case "video": //or news articles
                               e.Fields.Add("sortDate ", publishDate );
                      case "other doc types":
                               e.Fields.Add("sortDate ", createDate);
    

    Then in your search method you can make

     var SearchResults = Searcher.Search(query.And().OrderBy("sortDate").Compile());
    

    Mila

  • Lucy Foster 10 posts 82 karma points
    Apr 27, 2018 @ 08:54
    Lucy Foster
    0

    Hi Mila,

    Thank you so much for your reply, I have tried the first one which didn't work so I'm looking at your section option.

    I haven't done much with extending umbraco before, would you be able to point me in the right direction as to where the GatheringNodeData event code needs to go?

    Thank you! Lucy

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Apr 27, 2018 @ 09:01
    Nik
    0

    Hi Lucy,

    Okay, so the External Index is a bit of a pain for sorting. The problem I've found is that it is inputting all fields as strings. If you had your own custom index, you can specified nodes that should be sortable and what type they are.

    Examine help docs can be found here: https://github.com/Shazwazza/Examine/wiki

    But, the reason for linking them is this: https://github.com/Shazwazza/Examine/wiki/IndexSet#field-types--sorting

    What you might be able to do (and I've not tried this), is manually add your date based fields to the External Index set as sortable and specify their type as well. This might allow your searching to sort correctly on the lucene/examine side of things rather than once you get your results back.

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft