NEST - Create a custom function score to use a custom plugin

Hi,

We are migrating our application from NEST 6 to NEST 7.13.2 and now we can't call our plugin score function the way we do before.
So with NEST 6 we have a function extending FunctionScoreFunctionBase, and with that we call a custom plugin in our cluster to rescore the products.

public class CustomScoreFunction : FunctionScoreFunctionBase
    {
        public CustomScoreFunction(IDictionary<string, object> customFields, double weight)
        {
            this.CustomFields = customFields;
            this.Weight = weight;
        }

        [DataMember(Name = "custom-rescore")]
        public IDictionary<string, object> CustomFields { get; set; }
    }

below we have the expected query to call the custom plugin

{
    "query": {
        "bool": {
            "must": [
                {
                    "function_score": {
                        "functions": [
                            {
                                "custom-rescore": {
                                    "parameter_name1": "parameter_value1",
                                    "parameter_name2": "parameter_value2",
                                    "parameter_name3": "parameter_value3",
                                },
                                "weight": 1.0
                            }
                        ],
                        "score_mode": "max"
                    }
                }
            ]
        }
    }
}

Exception problem:

at Nest.ScoreFunctionJsonFormatter.Serialize(JsonWriter& writer, IScoreFunction value, IJsonFormatterResolver formatterResolver)
   at Elasticsearch.Net.Utf8Json.Formatters.CollectionFormatterBase`4.Serialize(JsonWriter& writer, TCollection value, IJsonFormatterResolver formatterResolver)
   at Elasticsearch.Net.Nest_IFunctionScoreQueryFormatter10.Serialize(JsonWriter& , IFunctionScoreQuery , IJsonFormatterResolver )
   at Elasticsearch.Net.Nest_IQueryContainerFormatter2.Serialize(JsonWriter& , IQueryContainer , IJsonFormatterResolver )
   at Nest.QueryContainerCollectionFormatter.Serialize(JsonWriter& writer, IEnumerable`1 value, IJsonFormatterResolver formatterResolver)
   at Elasticsearch.Net.Nest_IBoolQueryFormatter3.Serialize(JsonWriter& , IBoolQuery , IJsonFormatterResolver )
   at Elasticsearch.Net.Nest_IQueryContainerFormatter2.Serialize(JsonWriter& , IQueryContainer , IJsonFormatterResolver )
   at Elasticsearch.Net.Nest_ISearchRequestFormatter1.Serialize(JsonWriter& , ISearchRequest , IJsonFormatterResolver )
   at Elasticsearch.Net.Utf8Json.JsonSerializer.<SerializeAsync>d__11`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Elasticsearch.Net.SerializableData`1.<WriteAsync>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Elasticsearch.Net.HttpConnection.<SetContentAsync>d__24.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Elasticsearch.Net.HttpConnection.<RequestAsync>d__14`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Elasticsearch.Net.RequestPipeline.<CallElasticsearchAsync>d__67`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Elasticsearch.Net.Transport`1.<RequestAsync>d__15`1.MoveNext()

Exception Message Can not write function score json for CustomScoreFunction

Steps we already tried:

  • we already tried injecting a custom serializer but it's only affecting the SourceSerializer.

How we can call our custom plugin as function in this NEST 7 version?
Or how do we set a specific serializer for UTF8Json?

Thanks,
Simon

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.