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

**URL:** <https://discuss.elastic.co/t/nest-create-a-custom-function-score-to-use-a-custom-plugin/294935>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 20, 2022, 10:52am UTC](https://discuss.elastic.co/t/nest-create-a-custom-function-score-to-use-a-custom-plugin/294935 "2022-01-20T10:52:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimNightmare](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simnightmare/32/100549_2.png) [@SimNightmare](https://discuss.elastic.co/u/SimNightmare)\
**Post date:** [January 20, 2022, 10:52am UTC](https://discuss.elastic.co/t/nest-create-a-custom-function-score-to-use-a-custom-plugin/294935/1 "2022-01-20T10:52:49Z")

</div>

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.

```auto
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

```auto
{
    "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:

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 17, 2022, 10:53am UTC](https://discuss.elastic.co/t/nest-create-a-custom-function-score-to-use-a-custom-plugin/294935/2 "2022-02-17T10:53:23Z")

</div>

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