1
votes

We are using SolrNet API to Index and Search a set of documents. The corresponding POCO representing the Solr document is

internal class IndexDocument
{
    [SolrField("DocId")]
    public long DocId { get; set; }

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

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

We can index data and successfully return the search results. The requirement is to return the relevance score of the result items along with the solr document attributes. We can return the score data by adding "score" as a field in the search request to solr. But the issue is how do we accept the score data in the solr results since this is not a part of the solr document class. We use the following snippet to execute the search:

SolrQueryResults<IndexDocument> results = SolrInstance.Query(query, options);

We can get the score data returned when we execute the query from the solr admin interface but how do we return the score data in the results object since the search results are returned only in terms of the IndexDocument class. The other parameters of the SolrQueryResults class like Grouping, Higlights, SimilarResults, Terms, etc., which are not typically related to the Solr document class, are also not suitable to return the score data.

3

3 Answers

2
votes

The answers from Maurizio and HungryPipo gave me some hints, but here is the code that worked for me (on a sitecore project):

[IndexField("score")]
public decimal Score { get; set; }

Also, I had to add this to the query options:

Fields = {"*", "score"}
1
votes

You can accomplished this with two POCO classes. One for mapping the result retrieved and one for indexing. Make sure you add the score field in this way:

[SolrField("score")]
public double Score { get; set; }

only in the first class used for retrieving the data.

1
votes

If you make your POCO class have the Score as nullable you can use the same object for indexing and results

[SolrField("score")]
public double? Score { get; set; }