Copied to clipboard

Flag this post as spam?

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


  • Jake Platford 5 posts 95 karma points
    Aug 20, 2020 @ 15:26
    Jake Platford
    0

    Search query not pulling through the right results

    I'm trying to set up a site search and a blog search. Currently for the site search I have:

    if (ExamineManager.Instance.TryGetIndex("ExternalIndex", out var index))
                    {
                        var searcher = index.GetSearcher();
                        var searchResults = searcher.Search(searchTerm);
                        if (searchResults.Any())
                        {
                            <ul>
                                @foreach (var result in searchResults)
                                {
                                    if (result.Id != null)
                                    {
                                        var node = Umbraco.Content(result.Id);
                                        <li>
                                            <a href="@node.Url">@node.Name</a>
                                        </li>
                                    }
                                }
                            </ul>
                        }
                        else
                        {
                            <p>No results found for query @searchTerm</p>
                        }
                    }
    

    (Where @searchTerm is coming from the form submit/query string)

    If I search for 'blog', then these blog posts come through (correct)

    • Bloggish
    • Testing my blog post

    Now, onto my blog search which is as below:

    if (ExamineManager.Instance.TryGetIndex("ExternalIndex", out var index))
            {
                var searcher = index.GetSearcher();
                var searchTerms = query.Split(' ').ToList().Select(s => s.Fuzzy()).ToArray();
    
                var results = searcher.CreateQuery("content").NodeTypeAlias("blogPost")
                    .And().ParentId(model.Content.Id)
                    .And().GroupedOr(new string[] { "nodeName", "title" }, searchTerms).Execute();
            }
    

    (This code is in a controller, query = the search term in the query string)

    If I search for 'blog' here, only the following appear:

    • Testing my blog post

    The post 'Bloggish' is missing from the results. How can I adjust the code above so that it would be more flexible and pull through the right results?

  • Amir Khan 1199 posts 2567 karma points
    Aug 20, 2020 @ 16:09
    Amir Khan
    0

    Sometimes when I have search issues I try to rebuild the Examine index.

  • Jake Platford 5 posts 95 karma points
    Aug 20, 2020 @ 16:20
    Jake Platford
    0

    Hi Amir,

    I don't believe this to be an index issue. Because, if I remove:

    .And().GroupedOr(new string[] { "nodeName", "title" }, searchTerms)
    

    leaving the rest of the query as is, it is able to pull through both "testing my blog post" and "bloggish".

  • Amir Khan 1199 posts 2567 karma points
    Aug 20, 2020 @ 18:26
    Amir Khan
    0

    Grabbed this from one of our blog searches, which I know works. Obvi some extra fields for our setup.

    Maybe give this a try and see if you get the desired results.

     var searcher = ExamineManager.Instance;
    var searchCriteria = searcher.CreateSearchCriteria();
    
    var query = searchCriteria.GroupedOr(new[] { "nodeName", "mainHeading", "postAbstract", "postGrid", "bodyCopyLegacy", "metaInformation"}, searchTerm).Not().Field("umbracoNaviHide", "1").And().Field("nodeTypeAlias", "BlogPostGeneral").Compile();
    
    var searchResults = searcher.Search(query).Where(r => r["__IndexType"] == "content").ToList();
    
  • Jake Platford 5 posts 95 karma points
    Aug 20, 2020 @ 21:39
    Jake Platford
    0

    Which version of Umbraco is this for?

    I'm on 8.6.3 and I'm unable to use:

    searcher.CreateSearchCriteria();
    

    Additionally, I have no option to use Compile() on the search query.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies