Copied to clipboard

Flag this post as spam?

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


  • zedna 7 posts 87 karma points
    Oct 26, 2015 @ 14:45
    zedna
    0

    Search in all fields

    Hi i have search on Umbraco 7 and it works OK, but i have to add a lot of search fields to index and it´s not practic. How can i search through all fields instead of adding them manually?

    @{
        string searchQuery = Request["query"];
        if (String.IsNullOrWhiteSpace(searchQuery))
        {
            searchQuery = "";
        }
    
        var searcher = ExamineManager.Instance;
        var searchCriteria = searcher.CreateSearchCriteria();
    
        var query = searchCriteria.GroupedOr(new[] { 
        "nodeName", 
        //"packSizes",
        "name", 
        "title", 
        "bodyText", 
        "body",
        "field1",
        "field2",
        "field3",
        "field4",
        "field5",
        "field6" 
         }, searchQuery).Compile();
        var SearchResults = searcher.Search(query).Where(x => x["__IndexType"] == "content").ToList();
    }
    @if (SearchResults.Any())
    {
        <ul class="search-results-box">
            @foreach (var result in SearchResults)
            {
                var node = Umbraco.TypedContent(result.Id);
                var pathIds = result["__Path"].Split(',');
                var path = Umbraco.TypedContent(pathIds).Where(p => p != null).Select(p => new { p.Name }).ToList();
    
                if (node != null)
                {
                    <li><a href="@node.Url">@node.Name</a></li>
                }
            }
        </ul>
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft