---
title: "Add highlighting to a custom search type"
slug: "add-highlighting-to-a-custom-search-type"
abstract: "Describes how to add highlights to a custom search type."
---
# Add highlighting to a custom search type

Solr's highlighting feature lets you include fragments in the `highlighting` section of a query response.

You can add highlights to a custom search type by first enabling highlighting on a specific field, then adding the `Highlights` type property to the custom search type.

To add highlights:

1. In the `<SOLR_DIR>\server\solr\<SOLR_CORE_NAME>\conf` folder, in the `managed-schema` file, enable highlighting for the desired fields by setting the `stored` property of the fields to `true`, as shown in the following example:

    ```xml
    <field name="title_t" type="text_general" multiValued="true" indexed="true" stored="true"/>
    <field name="text" type="text_general" multiValued="true" indexed="true" stored="true"/>
    ```

2. Create a custom search type class and add public properties that return the `Highlights` type, then map each property to its appropriate index field, as shown in the following example:

    ```csharp
    public class SimpleHighlightResultItem
        {
           [IgnoreIndexField]
           public string Title { get; set; }

           [IndexField("title")]
           public Highlights HighlightsInTitle { get; set; }

           [IgnoreIndexField]
           public string Text { get; set; }

           [IndexField("text")]
           public Highlights HighlightsInText { get; set; }
        }
    ```

3. Use the custom search type in a LINQ query. For example:

    ```csharp
    var index = ContentSearchManager.GetIndex("sitecore_master_index");

        using (var context = index.CreateSearchContext())
        {
            var result = context.GetQueryable<SimpleHighlightResultItem>()
                .Where(it => it.Title == "Sitecore" && it.Text == "customer experience")
                .WithHighlights(hl => hl.HighlightOn(i => i.HighlightsInText));

            foreach (var resultItem in result)
            {
                string[] highlightSegments = resultItem.HighlightsInText.ToArray();
            }
        }
    ```

    The preceding LINQ query generates the following Solr query: `q=(title_t:("Sitecore") AND text:("customer experience")) ... &hl=true&hl.fl=text`

    The `highlightSegments` property contains the following segment:

    ```text
    " complexity that has previously held marketers back, the latest version of <em>Sitecore</em> makes <em>customer</em> <em>experience</em>"
    ```

<details>
<summary><h2>Highlight multiple fields in a LINQ query</h2></summary>

You can get highlighted matches for several index fields in a single LINQ query.

The following query returns highlights for both the `title` and `text` index fields:

```

var result = context.GetQueryable<SimpleHighlightResultItem>()
        .Where(it => it.Title == "Sitecore" && it.Text == "customer experience")
        .WithHighlights(hl => hl
            .HighlightOn(i => i.HighlightsInText)
            .HighlightOn(i => i.HighlightsInTitle)
        )
        .ToArray();

```

The preceding LINQ query generates the following Solr query: `q=(title_t:("Sitecore") AND text:("customer experience")) ... &hl=true&hl.fl=title_t,text`

The `HighlightsInText` property contains the following segment:

```text
" complexity that has previously held marketers back, the latest version of <em>Sitecore</em> makes <em>customer</em> <em>experience</em>"

```

The `HighlightsInTitle` property contains the following segment:

```text
"<em>Sitecore<em> Experience Platform"

```

</details>