Try our conversational search powered by Generative AI!

Using projections with HitId and HitType


As I describe in this blog post it is possible to enable click tracking for more than unified search.

Now I have run into problem with that when trying to get HitId and HitType when using projections.

I am trying to minimize what are returned by the server together with doing a multisearch and for that I am using projections.

The problem is that when I use these functions:

                        HitId = _searchClient.Conventions.IdConvention.GetId(h),
                        HitType = _searchClient.Conventions.TypeNameConvention.GetTypeName(h.GetType())

I get an error like this:

An exception of type JsonSerializationException was thrown while deserializing object.

With the inner exception:

"Error setting value to 'IsPendingPublish' on '[pagetype]'."

If I comment out these two lines it works great with the projection.

Anyone know how to get this to work or to work around it to get projections to work?

May 22, 2015 19:51

Henrik Lindström helped me out with the answer.

This is because it triggers Find to referens and trying to serielize the whole PageData object and this wount work.

The way to solve it is to use IncludeField.

Start by creating extension methods to PageData like this:

        public static string HitId(this PageData page)
            return SearchClient.Instance.Conventions.IdConvention.GetId(page);

        public static string HitType(this PageData page)
            return SearchClient.Instance.Conventions.TypeNameConvention.GetTypeName(page.GetType());

Then in your class that defines what to or not to index (if you have one of those) add this:

            SearchClient.Instance.Conventions.ForInstancesOf<PageData>().IncludeField(p => p.HitId());
            SearchClient.Instance.Conventions.ForInstancesOf<PageData>().IncludeField(p => p.HitType());

Reindex all and then use the new property like this:

HitId = h.HitId(),
HitType = h.HitType()

It works great!

Thanks again Henrik!

May 22, 2015 20:51
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.