SolrNet / SolrNet

Solr client for .Net
https://github.com/SolrNet/SolrNet?tab=readme-ov-file#documentation-index
Apache License 2.0
941 stars 797 forks source link

SolrNet Highlight #243

Closed ksivanandan closed 8 years ago

ksivanandan commented 8 years ago

Hello

I have implemented the SolrNet sample App and I am looking to do the Highlights. I configured solr and I see now I want to integrate it with my app and show it on the results page. I am display Title, Url. I would like to display snippet (where the keyword appears) and highlight the words on the title, url and Snippet. Are there any examples?

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 = "", AfterTerm = "", Snippets = 1 }

            };

Index @foreach (var item in Model.EntireSiteResults) {

  • @item.Title

    @item.PageUrl

    @item.Description

  •     }

    Any help is greatly appreciated

    Thanks Kalpana

    xmorera commented 8 years ago

    Example is in the documentation: https://github.com/mausch/SolrNet/blob/master/Documentation/Highlighting.md

    Additionally you can see this thread in SO for configuration: http://stackoverflow.com/questions/20097314/highlighting-in-solrnet

    ksivanandan commented 8 years ago

    Thank you for your quick response.

    I did see both the thread but not sure where I should set this up? At the Controller after I get the result set? Should I work on the results? Also, I would have to show the highlight on Title, Url, Description and Snippet. var results = _searchResults.Query(BuildQuery(parameters), queryOption); view = new SearchView { EntireSiteResults = results, Search = parameters, TotalCount = results.NumFound, DidYouMean = GetSpellCheckingResult(results) };

    Or at Index @foreach (var item in Model.EntireSiteResults) { @item.Title @item.PageUrl @item.Description }

    Thanks

    xmorera commented 8 years ago

    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:

    ksivanandan commented 8 years ago

    Thank you! Will try that out.

    ksivanandan commented 8 years ago

    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 = "", AfterTerm = "", Snippets = 1 }

                };

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

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

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

    Title, fullpath, 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

    Thanks From: xaviermorera [mailto:notifications@github.com] Sent: Friday, June 17, 2016 5:21 PM To: mausch/SolrNet SolrNet@noreply.github.com Cc: Kalpana Sivanandan ksivanandan@portalsolutions.net; Author author@noreply.github.com Subject: Re: [mausch/SolrNet] SolrNet Highlight (#243)

    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:

    — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/mausch/SolrNet/issues/243#issuecomment-226884481, or mute the threadhttps://github.com/notifications/unsubscribe/AQZGRs67mr_xLVfnAERD9lpHYAOOVrl9ks5qMw_FgaJpZM4I4s_G.

    xmorera commented 8 years ago

    Yes you have to iterate, please see that the key on Highlights matches the document id so use a foreach to go through them. Then on each one of the objects, the field name is also the key to get them from the list and extract the snippet with highlights.

    I believe that it is better for this kind of question to be posted to StackOverflow as iteration over an object is something that can be explained rather quickly by the many answerers of SO.

    ksivanandan commented 8 years ago

    I can use the foreach to iterate through the object, although I don't have Id as part of my SearchViewModel and hence foreach (var h in results.Highlights[results[0].Id]) throws an error

    results.Highlights[results[0].Id] error CS1061: 'SearchResultsViewModel' does not contain a definition for 'Id' and no extension method 'Id' accepting a first argument of type 'SearchResultsViewModel' could be found (are you missing a using directive or an assembly reference?)

    Thanks

    ksivanandan commented 8 years ago

    Although when I type in results.Highlights["9105"] I get the object back with Keys, Snippets and Values.

    xmorera commented 8 years ago

    Unique key should be in results if you add it to your model. And again, StackOverflow for support :)

    ksivanandan commented 8 years ago

    Yes thanks, I have posted the question there as well. Should I be using results.Highlights.Snippets?

    From: xaviermorera [mailto:notifications@github.com] Sent: Monday, June 20, 2016 12:49 PM To: mausch/SolrNet SolrNet@noreply.github.com Cc: Kalpana Sivanandan ksivanandan@portalsolutions.net; Author author@noreply.github.com Subject: Re: [mausch/SolrNet] SolrNet Highlight (#243)

    Unique key should be in results if you add it to your model. And again, StackOverflow for support :)

    — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/mausch/SolrNet/issues/243#issuecomment-227200059, or mute the threadhttps://github.com/notifications/unsubscribe/AQZGRr1y8v13SvwuSzXY9j88_H0PBHeWks5qNsR8gaJpZM4I4s_G.

    ksivanandan commented 8 years ago

    Also, will I be able to use the same viewmodel and swap the values for title, PageUrl, description and health_content with the ones from Highlight? Do you have any examples?

    Thanks so much for all your help

    ksivanandan commented 8 years ago

    I was able to get this working! Thank you