Question

I have a problem to get highlighting working. I use a ASP.Net MVC Application with a WCF Service. The WCF Service return the results to the view. My code is:

public IEnumerable<Dictionary<string, string>> SolrQuery(string searchString)
    {
        SolrInitialSetup(); 

        var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrPDFDocument>>();

        var results = solr.Query(new SolrQueryByField("author", searchString),
            new QueryOptions
            {
                Highlight = new HighlightingParameters
                {
                    Fields = new[] { "search_snippet" },
                }
            });

        var returnedResults = new List<Dictionary<string, string>>();

        if (results.Count > 0)
        {
            foreach (var pdfDoc in results)
            {
                var docBuffer = new Dictionary<string, string>();
                docBuffer.Add("Id", ""+pdfDoc.Id.GetHashCode());
                docBuffer.Add("Content", "" + pdfDoc.Content.ElementAt(0));
                docBuffer.Add("Version", "" + pdfDoc.Version);

                foreach (var h in results.Highlights[results[0].Id])
                {
                    docBuffer.Add("search_snippet", String.Format("{0}", string.Join(", ", h.Value.ToArray())));
                    Debug.WriteLine("search_snippet", String.Format("{0}", string.Join(", ", h.Value.ToArray())));
                }
                returnedResults.Add(docBuffer);
            }
        }
        return returnedResults;
    }

@ Line foreach (var h in results.Highlights[results[0].Id]) I get an error in the browser The given key was not present in the dictionary. I have no idea whats wrong.

My schema.xml:

    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
    <field name="content" type="text_general" indexed="true" stored="true" multiValued="true" termVectors="true" termPositions="true" termOffsets="true"/>
    <field name="author" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>
    <field name="_version_" type="long" indexed="true" stored="true"/>
    <field name="search_snippet" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true" />

My SolrPDFDocument.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolrNet.Attributes;

namespace WcfSearchService
{
    class SolrPDFDocument
    {
        [SolrUniqueKey("id")]
        public string Id { get; set; }

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

        [SolrField("content")]
        public ICollection<string> Content { get; set; }

        [SolrField("search_snippet")]
        public ICollection<string> SearchSnippet { get; set; }

        [SolrField("_version_")]
        public long Version { get; set; }
    }
}

My solrconfig.xml

<requestHandler name="/select" class="solr.SearchHandler">
    <!-- default values for query parameters can be specified, these
         will be overridden by parameters in the request
      -->
     <lst name="defaults">
       <str name="echoParams">explicit</str>
       <int name="rows">10</int>
       <str name="df">text</str>

     <!-- Highlighting defaults -->
       <str name="hl">on</str>
       <str name="hl.fl">search_snippet author content</str>
     </lst>
</requestHandler>
Was it helpful?

Solution

OK i have solved my problem.

Instead of using the sample code like that:

var results = solr.Query(new SolrQueryByField("author", searchString),
        new QueryOptions
        {
            Highlight = new HighlightingParameters
            {
                Fields = new[] { "author" },
            }
        });

I set the Parameter of the new Fields like this:

var results = solr.Query(new SolrQueryByField("text", searchString),
        new QueryOptions
        {
            Highlight = new HighlightingParameters
            {
                Fields = new[] { "*" },
            }
        });

So I'm getting all the snippets to all results. Thanks for your help!!!!

OTHER TIPS

You might not be getting any highlight results at all. Can you debug your code and validate that results.Highlights is populated. Also check your highlighting defaults settings as I believe you need to set hl=true not on.

 <str name="hl">true</str>

This is interesting. For me, this wasn't working (I wasn't getting any results):

var results = solr.Query(new SolrQueryByField("text", searchString),
        new QueryOptions
        {
            Highlight = new HighlightingParameters
            {
                Fields = new[] { "*" },
            }
        });

But, this works for me:

 var results = solr.Query(string.Format("{0}:{1}", "text", searchString),
            new QueryOptions
            {
                Highlight = new HighlightingParameters
                {
                    Fields = new[] { "*" },
                }
            });

I am using solr 4.6.0 and SolrNet build #173 (16 Oct 13 00:32)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top