3 votes

Search Extensions

Extensions for performing searches in Umbraco.

This package is supported on Umbraco 8.1+.

Querying

There are several short-hand extension methods for querying Umbraco content in an index – checking if an item is published, is visible, or has a template.

Querying only published content items can be done like this: query.And().IsPublished()

Similarly, querying all content where the umbracoNaviHide property is not set can be done like this: query.And().IsVisible()

It is possible to query content with a specific template ID set. If 0 or no value is passed to the method, the query will match content with any templatee ID set. query.And().HasTemplate(int templateId)

Finally, it is possible to query for content that has any one of the specified content type aliases. Out of the box Umbraco supports querying for a single content alias. query.And().NodeTypeAlias(string[] aliases)

Cultures

Umbraco properties that have been set to "vary by culture" are indexed with a specific alias: {culture}_{fieldName}. For example, if the "pageTitle" field varies by culture and has 2 languages, English and Spanish, the index would contain 2 fields: "en_pageTitle" and "es_pageTitle".

A culture can be passed to Field and NodeName queries like this:

query.And().Field(string field, string culture)

query.And().NodeName(string nodeName, string culture)

It even works with grouped queries such as GroupedAnd, GroupedOr, and GroupedNot, where multiple fields can be specified: query.And().GroupedOr(string[] fields, string culture)

Searching

The SearchHelper class contains logic for commonly performed actions when searching, particularly helpful for creating paged search functionality.

The Get<T> method gets all results for a query cast to a given type, including IPublishedContent.

var query = _searcher.CreatePublishedQuery();

var results = _searchHelper.Get<T>(query, out int totalResults);

The Page<T> method efficiently gets a given number of items (perPage) at a specific position (page) in the results for a query An optional type constraint can be added to also return paged results cast to IPublishedContent.

var query = _searcher.CreatePublishedQuery();

var results = _searchHelper.Page<T>(query, int page, int perPage, out int totalResults);

All helper methods provide the total number of results found as an out parameter.

Results

For more specific cases where the SearchHelper is not appropriate, the same features for accessing strongly typed results are available as extension methods.

An entire results collection can be cast to a type like this:

var results = query.Execute().GetResults<T>();

Specific fields from an individual search result can be accessed via the .Value<T>() extension method like this:

foreach (var result in query.Execute())
{
    var value = result.Value<T>(string field);
}

Advanced fields

Search Extensions introduces several new field types into Examine – JSON, list, UDI and picker – to ensure Umbraco data is correctly indexed and queryable. Defining which fields in the index use which types is done through the IExamineManager:

if (_examineManager.TryGetIndex("ExternalIndex", out IIndex index))
{
    index.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("fieldName", "fieldType"));
}

The picker field type adds search-friendly aliases for the picked items into the index.

A picker with a selected a content item called "Example Page" can be queried like this:

query.Field("somePicker", "example-page");

Umbraco's "path" field is automatically indexed as a list and so a content item with the path "-1,1050,1100" can be queried like this:

query.Field("path", "1100");

Umbraco's "createDate" and "updateDate" fields are automatically indexed as date values, whereas they would be regularly indexed as string values.

Screenshots

Archived files

Documentation

Source code

 Download package
version 1.4.1

NuGet install instructions for Umbraco 8.1.0-8.17.1

Install-Package Our.Umbraco.Extensions.Search

Package owner

Callum Whyte

Callum Whyte

Callum has 270 karma points

Package Compatibility

This package is compatible with the following versions as reported by community members who have downloaded this package:
Untested or doesn't work on Umbraco Cloud
Version 10.0.x (untested)
Version 9.1.x (untested)
Version 9.0.x (untested)
Version 8.18.x (untested)
Version 8.17.x (untested)
Version 8.16.x (100%)
Version 8.15.x (100%)
Version 8.14.x (100%)
Version 8.13.x (100%)
Version 8.12.x (100%)
Version 8.11.x (100%)
Version 8.10.x (100%)
Version 8.9.x (100%)
Version 8.8.x (100%)
Version 8.7.x (100%)
Version 8.6.x (100%)
Version 8.5.x (100%)
Version 8.4.x (100%)
Version 8.3.x (100%)
Version 8.2.x (100%)
Version 8.1.x (100%)
Version 8.0.x (untested)
Version 7.15.x (untested)
Version 7.14.x (untested)
Version 7.13.x (untested)
Version 7.12.x (untested)
Version 7.11.x (untested)
Version 7.10.x (untested)
Version 7.9.x (untested)
Version 7.8.x (untested)
Version 7.7.x (untested)
Version 7.6.x (untested)
Version 7.5.x (untested)
Version 7.4.x (untested)
Version 7.3.x (untested)
Version 7.2.x (untested)
Version 7.1.x (untested)
Version 7.0.x (untested)

You must login before you can report on package compatibility.

Package Information

  • Package owner: Callum Whyte
  • Created: 23/01/2021
  • Current version 1.4.1
  • .NET version 4.7.2
  • License MIT
  • Downloads on Our: 192
  • Downloads on NuGet: 3.8K
  • Total downloads : 4K

External resources