HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Customizing default projections

Describes how to customize projections of search results using the Unified Search functionality in Optimizely Search & Navigation.

Unified Search is an Optimizely Content Management System (CMS) integration that lets you index and query objects of existing classes without having to map them to an intermediate type in the index.

You can customize the projection of unified search results in several ways. The UnifiedSearchRegistry supports the options described in the following, see Unified Search.

ProjectTitleFrom

To register the Title for a specific document type.

client.Conventions.UnifiedSearchRegistry
  .ForInstanceOf<MySearchableClass>()
  .ProjectTitleFrom(x => x.MyTitleProperty);

ProjectTypeNameFrom

To register the TypeName for a specific document type.

client.Conventions.UnifiedSearchRegistry
  .ForInstanceOf<MySearchableClass>()
  .ProjectTypeNameFrom(x => x.MyTypeNameProperty);

ProjectUrlFrom

To register the URL for a specific document type.

client.Conventions.UnifiedSearchRegistry
  .ForInstanceOf<MySearchableClass>()
  .ProjectUrlFrom(x => x.MyUrlProperty);

ProjectImageUriFrom

To register the ImageUri for a specific document type.

client.Conventions.UnifiedSearchRegistry
  .ForInstanceOf<MySearchableClass>()
  .ProjectImageUriFrom(x => x.MyImageUriProperty);

UseHitType

To register the type of the result item for a specific document type.

client.Conventions.UnifiedSearchRegistry
  .ForInstanceOf<MySearchableClass>()
  .UseHitType<MySearchableHitClass>();

ProjectHighlightedExcerptUsing

To register the highlighted excerpt for a specific document type.

client.Conventions.UnifiedSearchRegistry
  .ForInstanceOf<MySearchableClass>()
  .ProjectHighlightedExcerptUsing<ISearchContent>(spec =>
    x => x.MyHighlightedProperty.AsHighlighted(new HighlightSpec 
      { 
        FragmentSize = spec.ExcerptLength, NumberOfFragments = 1 
      }));

AlwaysApplyFilter

To register a filter always applied for UnifiedSearch.

client.Conventions.UnifiedSearchRegistry
  .ForInstanceOf<MySearchableClass>()
  .AlwaysApplyFilter(f => f.BuildFilter<MyType>().And(x => !x.MyProperty.Match("Something")));

PublicSearchFilter

To register a filter applied for public Unified Search.

client.Conventions.UnifiedSearchRegistry
  .ForInstanceOf<MySearchableClass>()
  .PublicSearchFilter(f => f.BuildFilter<MySearchableClass>().And(x => !x.MyProperty.Match("Something")));

CustomizeProjection

If the other projection options are inadequate, the CustomizeProjection extension provides full access to the HitProjectionBuilder.

Highlight and encode unified search results

For unified search results, HTML encoding is enabled by default for "title" and "excerpt" fields. The text is encoded before adding the HTML tags that surround highlighted content.

var hitSpec = new HitSpecification
  {
    HighlightExcerpt = true
    // HighlightTitle = false by default,
    // EncodeExcerpt = true by default,
    // EncodeTitle = true by default
  };

var result = searchClient.UnifiedSearchFor("apples").GetResult(hitSpec);
// result.Hits.First().Document.Excerpt contains I like <em>apples</em>.

Disable HTML encoding

To turn off HTML encoding, set the EncodeTitle and EncodeExcerpt properties of the HitSpecification to "false." If you do, ensure that the implementation handles the encoding of results to ensure normal rendering and avoid script injections. You may also implement custom highlighting.

var hitSpec = new HitSpecification
  {
    HighlightExcerpt = true,
    EncodeTitle = false,
    EncodeExcerpt = false
  };

var result = searchClient.UnifiedSearchFor("apples").GetResult(hitSpec);
// result.Hits.First().Document.Excerpt contains I like <em>apples</em>.