1

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?

@{ 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> }

1 Answers1

1

You can add an event to the indexing command to concatenate all of the fields into one large field at indexing time, and just search that one field.

To hook into the event, in your OnApplicationStarting event handler, do the following:

ExamineManager.Instance.IndexProviderCollection["YOUR INDER NAME HERE"].GatheringNodeData += SetSiteSearchFields;

And then for the function, you can do something like this, combining all of the fields int a single field:

void SetSiteSearchFields(object sender, IndexingNodeDataEventArgs e)
    {
        //grab some fields
        var combined = e.Fields["field1"] + " " + e.Fields["field2"];

        //add as new field
        e.Fields.Add("searchField", combined);
    }

This would then give you a field called "searchField" that you could search, making your search much simpler.

Tim
  • 4,217
  • 1
  • 15
  • 21