Copied to clipboard

Flag this post as spam?

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


  • André Nøbbe Christiansen 10 posts 71 karma points
    Feb 28, 2015 @ 17:23
    André Nøbbe Christiansen
    0

    Umbraco search is wired. Dynamic searchQuery dosen't work but static does?

    Hello. I'm adjusting / exploring my searchfunction on a page, and I stumbled upon a strange problem.

    When I make my SearchQuery dynamic and shearchs for results, it says for some keywords that there are no results, but I know that there should be some.

    StringBuilder luceneString = new StringBuilder();

            luceneString.Append("+((");

            if (isSenetens){

                luceneString.Append("nodeName:(+" + searchTerm.Replace(" ", " +") + ")^5 ");

            }

            luceneString.Append("nodeName:" + searchTerm + "~0.4)^2");

     

            if (isSenetens){

                luceneString.Append(" name:(+" + searchTerm.Replace(" ", " +") + ")^5");

            }

            luceneString.Append(" name:" + searchTerm + "~0.4");

     

            if (isSenetens){

                luceneString.Append(" title:(+" + searchTerm.Replace(" ", " +") + ")^5");

            }

            luceneString.Append(" title:" + searchTerm + "~0.4");

            if (isSenetens){

                luceneString.Append(" bodyText:(+" + searchTerm.Replace(" ", " +") + ")^5");

            }

            luceneString.Append(" bodyText:" + searchTerm + "~0.4");

     

            if (isSenetens){

                luceneString.Append(" seo:(+" + searchTerm.Replace(" ", " +") + ")^5");

            }

            luceneString.Append(" seo:" + searchTerm + "~0.4)");

    SearchQuery = luceneString.ToString(); //+((nodeName:keyword~0.4)^2 name:keyword~0.4 title:keyword~0.4 bodyText:keyword~0.4 seo:keyword~0.4)


    But when I insert the same SearchQuery static there are now two results?

    SearchQuery = "+((nodeName:keyword~0.4)^2 name:keyword~0.4 title:keyword~0.4 bodyText:keyword~0.4 seo:keyword~0.4)";

    Does anyone know how this can be?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Mar 05, 2015 @ 17:48
    Alex Skrypnyk
    0

    Hi André ,

    Try this code snippet :

                    var searchFields = new[] { "nodeName", "name", "title", "bodyText", "seo" };
    
                    var criteria = _searchService.CreateSearchCriteria();
    
                    var query2 = new StringBuilder();
                    query2.AppendFormat("-{0}:1 ", "umbracoNaviHide");
    
                    // Set search path
                    var contentPathFilter = string.Format("__IndexType:{0} -template:0", UmbracoExamine.IndexTypes.Content);
                    query2.AppendFormat("+({0}) ", contentPathFilter);
    
                    // Ensure page contains all search terms in some way
                    var searchTems = Tokenize(model);
                    foreach (var term in searchTems)
                    {
                        var groupedOr = new StringBuilder();
                        foreach (var searchField in searchFields)
                        {
                            groupedOr.AppendFormat("{0}:{1}* ", searchField, term);
                        }
                        query2.Append("+(" + groupedOr + ") ");
                    }
    
                    // Rank content based on positon of search terms in fields
                    for (var i = 0; i < searchFields.Count(); i++)
                    {
                        foreach (var term in searchTems)
                        {
                            query2.AppendFormat("{0}:{1}*^{2} ", searchFields[i], term, searchFields.Count() - i);
                        }
                    }
    
                    var criteria2 = criteria.RawQuery(query2.ToString());
    
                    var results = _searchService.Search(criteria2).TakeWhile(x => x.Score > 0.05f).OrderByDescending(x => x.Score).ToList();
    
Please Sign in or register to post replies

Write your reply to:

Draft