Kalpana
Kalpana

Reputation: 11

SolrNet Highlighting with Solr 5.4.1

Good Morning

I was able to keep a breakpoint and see the results. Highlights

My view Model

   [SolrField("health_content_t")]
    public string HealthContent { get; set; }

    [SolrField("description_t")]
    public string Description { get; set; }

    [SolrField("title_t")]
    public string Title { get; set; }

    [SolrField("_fullpath")]
    public string PageUrl { get; set; }

My Controller

var queryOption = new QueryOptions
            {
                FilterQueries = BuildFilterQueries(parameters),
                Rows = parameters.PageSize,
                Start = start,
                SpellCheck = new SpellCheckingParameters
                {
                    Collate = true
                },
                Highlight = new HighlightingParameters
                {
                    Fragsize = 250,
                    Fields = new[] { "*" },
                    BeforeTerm = "<b>",
                    AfterTerm = "</b>",
                    Snippets = 1
                }

            };

var results = _searchResults.Query(BuildQuery(parameters), queryOption);

view = new SearchView
{
      EntireSiteResults = results,
      Search = parameters,
      TotalCount = results.NumFound,
      DidYouMean = GetSpellCheckingResult(results)
};

My View

@foreach (var item in Model.EntireSiteResults)
    {
        <li>
            <h2><a href="@item.PageUrl">@item.Title</a></h2>
            <span class="sr-url"><a href="@item.PageUrl">@item.PageUrl</a></span>
            <p>@item.Description</p>
        </li>
    }

I am not sure how I iterate through the results. I would have to display

Title, PageUrl, with bolded search term, also show the snippet of the healthContent with bolded search term

Looks like I have to iterate through the results.Highlights for each of these fields?

Any guidance please

Upvotes: 1

Views: 422

Answers (1)

xmorera
xmorera

Reputation: 1961

[Note: this question was originally posted in GitHub but I suggested to take to SO for additional answers]

You have first to specify that you want to use highlighting and over which fields. The * is "good" but not great if you have many fields that you don't want to highlight - also depends on your qf. But in any case, you need to:

Set your highlighting parameters in QueryOptions and pass to .Query() And then when you get your results, you will get Title, PageUrl and Description without highlights. You need to iterate over highlight object in the response, which has the unique key as key and then extract the highlighted field that you are looking for. If this was confusing, put a breakpoint on the line immediately after you query (var results = ...) and then inspect results.Highlights.

Once you have the results you can iterate as indicated in the documentation page:

foreach (var h in results.Highlights[results[0].Id]) {
    Console.WriteLine("{0}: {1}", h.Key, string.Join(", ", h.Value.ToArray()));
}

While you iterate over highlights you can see for each result which is a snippet, the id will be the unique key. Then, in each h you will get a KeyValuePair where h.Key is the field that you are looking for, i.e. the title. The h.Value is a list of all snippets returned as you can have many. You should be fine if you get the first one.

See for reference https://github.com/mausch/SolrNet/blob/master/Documentation/Highlighting.md

Upvotes: 2

Related Questions