Copied to clipboard

Flag this post as spam?

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


  • Yaco Zaragoza 87 posts 361 karma points
    Jun 04, 2024 @ 19:34
    Yaco Zaragoza
    0

    Adding a Content Search to my site

    I am trying to add a Search Box to my site that searches the content added to each page.

    This is the code that I have

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.SearchPage>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    
    @using Umbraco.Cms.Core.Models.PublishedContent;
    @using Umbraco.Cms.Core.Services;
    @using Umbraco.Cms.Core.Web;
    @using Examine;
    @using System.Collections.Generic;
    @using System.Linq;
    
    @inject IExamineManager ExamineManager
    
    @{
        Layout = "Master.cshtml";
        string query = Context.Request.Query["q"];
        var searchResults = new List<IPublishedContent>();
    
        if (!string.IsNullOrEmpty(query))
        {
            if (ExamineManager.TryGetSearcher("ExternalIndex", out var searcher))
            {
                var criteria = searcher.CreateQuery("content").NodeTypeAlias("contentPage")
                                       .And().GroupedOr(new[] { "nodeName", "mainContent" }, query);
    
                searchResults = criteria.Execute().Select(x => Umbraco.Content(x.Id)).ToList();
            }
        }
    }
    
    <div class="container-fluid">
        <div class="row">
            <div class="col">
    
                <p>Search Results for <strong>@query</strong></p>
    
                @if (searchResults.Any())
                {
                    <ul>
                        @foreach (var result in searchResults)
                        {
                            <li>
                                <a href="@result.Url()">@result.Name</a>
                            </li>
                        }
                    </ul>
                }
                else
                {
                    <p>No results found</p>
                }
            </div>
    
        </div>
    </div>
    

    but it is not returning any results, Can anyone tell me what I am missing?

    1. I did check on the backend and ExternalIndex has all the values that I am looking for

    2. contentPage is the Content Type that I want to search but I would also be OK searching all content types if possible

  • Marcio Goularte 388 posts 1360 karma points
    Jun 05, 2024 @ 13:25
    Marcio Goularte
    0

    hello,

    try this:

     var criteria = searcher.CreateQuery("content").NodeTypeAlias("contentPage")
                                       .And().GroupedOr(new[] { "nodeName", "mainContent" }, query.MultipleCharacterWildcard());
    
  • Yaco Zaragoza 87 posts 361 karma points
    Jun 05, 2024 @ 15:42
    Yaco Zaragoza
    0

    Can you explain what does adding ".MultipleCharacterWildcard()" do?

  • Marcio Goularte 388 posts 1360 karma points
    Jun 05, 2024 @ 17:51
    Marcio Goularte
    0

    The MultipleCharacterWildcard is an extension that adds * to the end of the string. Without this, your search will return exactly the term you are looking for. It's the way to tell Lucene to search for something that starts with the term you want

Please Sign in or register to post replies

Write your reply to:

Draft