Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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?
I did check on the backend and ExternalIndex has all the values that I am looking for
contentPage is the Content Type that I want to search but I would also be OK searching all content types if possible
hello,
try this:
var criteria = searcher.CreateQuery("content").NodeTypeAlias("contentPage") .And().GroupedOr(new[] { "nodeName", "mainContent" }, query.MultipleCharacterWildcard());
Can you explain what does adding ".MultipleCharacterWildcard()" do?
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
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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
but it is not returning any results, Can anyone tell me what I am missing?
I did check on the backend and ExternalIndex has all the values that I am looking for
contentPage is the Content Type that I want to search but I would also be OK searching all content types if possible
hello,
try this:
Can you explain what does adding ".MultipleCharacterWildcard()" do?
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
is working on a reply...