# NEST V5 How to use FunctionScoreQuery/FunctionScoreFunction to always boost a particular field

**URL:** https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994
**Category:** Elasticsearch
**Created:** [May 9, 2017, 1:47am UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994 "2017-05-09T01:47:34Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![davey1990](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@davey1990](https://discuss.elastic.co/u/davey1990)
#### Post date: [May 9, 2017, 1:47am UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994/1 "2017-05-09T01:47:34Z")

</div>

Hi All,

I am really new to Elastic Search so please bear with me. I can see lots of examples in the Elasticsearch Reference guide for using the [Elasticsearch.NET](http://Elasticsearch.NET) low level client but not many examples on how to use the NEST high level client for FunctionScoreQuery/FunctionScoreFunction.

I am using NEST to build up a SearchRequest which will utilize a custom FunctionScoreQuery. I've set the Query property and am now trying to set the Functions property to a valid list of FunctionScoreFunctions. For simplicity, let's assume I have a simple Product object which has two properties, one a List called 'BoughtBefore' (which is a list of customer Ids) and one a float called 'Boost' which is the result of a custom boost calculation I have based on some of the properties in the Product object.

Firstly, I am trying to create a FunctionScoreFunction which would always apply a boost of 2 to the score if a customer has bought this product before.

This is the code I have for this FunctionScoreFunction:

```
var boughtBeforeBoostScore = new FunctionScoreFunction()
{
    Filter = new BoolQuery()
    {
        Must = new List<QueryContainer>() { new TermQuery() { Field = "boughtBefore", Value = customerId, Boost = 2.0 } }
    }
};

```

I have tried lots of things to no avail and at run time i get the exception: ' **Can not write function score json for FunctionScoreFunction**'.

Similarly, with the Product.Boost field, I am trying to create another FunctionScoreFunction which uses my Product.Boost field to **replace** the boost value calculated by Elasticsearch. Here is the code for this FunctionScoreFunction below:

```
var calculatedBoostScore = new FunctionScoreFunction()
{
    Filter = new FunctionScoreQuery() { Functions = new List<IScoreFunction> { new FieldValueFactorFunction() { Field = "boost" } }, BoostMode = FunctionBoostMode.Replace }
};

```

If someone could provide some guidance with these functions and how to properly implement them using NEST, that would be much appreciated.

Thanks

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [May 9, 2017, 7:03am UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994/2 "2017-05-09T07:03:12Z")

</div>

Hey @davey1990, [did you see the NEST example usage for `function_score` query](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/function-score-query-usage.html)? It demonstrates both the fluent API and object initializer syntax, both producing the example json shown.

---

<div class="post-metadata">

### Author: ![davey1990](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@davey1990](https://discuss.elastic.co/u/davey1990)
#### Post date: [May 9, 2017, 9:35pm UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994/3 "2017-05-09T21:35:16Z")

</div>

Hi @forloop, thank you so much for the link to that doc, I completely missed it in the documentation. I think I was mainly looking through the [Elasticsearch.NET](http://Elasticsearch.NET) doco (not NEST) instead.

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [May 10, 2017, 7:08am UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994/4 "2017-05-10T07:08:59Z")

</div>

> [@davey1990](#):
>
> thank you so much for the link to that doc, I completely missed it in the documentation.

No worries! If you have any further questions, feel free to ask them here 👍

---

<div class="post-metadata">

### Author: ![davey1990](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@davey1990](https://discuss.elastic.co/u/davey1990)
#### Post date: [May 12, 2017, 1:19am UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994/5 "2017-05-12T01:19:08Z")

</div>

Hi @forloop, I have another question 😃

I have got the FucntionScoreQuery working with the FieldValueFactorFunction on the 'boost' field on its own but I can't seem to combine it together with boosting on the 'boughtBefore' field if it matches the current customer Id performing the search.

I have this function for the 'boost' field:

```
        var calculatedBoostScore = new FieldValueFactorFunction()
        {
            Field = "boost",
            Factor = 1
        };

```

and this function for the 'boughtBefore' field but I don't think this is the correct way to do it?

```
        var boughtBefore = new FieldValueFactorFunction()
        {
            Field = "boughtBefore",
            Filter = new TermQuery() { Field = "boughtBefore", Boost = 3.5, Value = customerId }
        };

```

These two functions are then assigned to the one FunctionScoreQuery:

```
        var functionScoreQuery = new FunctionScoreQuery
        {
            Query = alreadyfilteredQuery,
            Functions = GetFunctionScoreFunctions(searchRequest.CustomerId)
        };

```

Should I actually have two separate FunctionScoreQueries?

Thanks for your help

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [May 14, 2017, 10:47pm UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994/6 "2017-05-14T22:47:15Z")

</div>

You can have multiple functions within one [`function_score` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html) and control how the scores computed for each function are combined together with `score_mode`, and then how they are combined with the score from the query using `boost_mode`.

Your query looks OK to me, assuming `GetFunctionScoreFunctions()` returns an `IEnumerable<IScoreFunction>`. You can get additional detail for how scores are calculated for each document with [`explain`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-explain.html) on the search request.

---

<div class="post-metadata">

### Author: ![davey1990](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@davey1990](https://discuss.elastic.co/u/davey1990)
#### Post date: [May 15, 2017, 9:43am UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994/7 "2017-05-15T09:43:04Z")

</div>

Thanks @forloop!

I've got it all working now! I also had to set a weight for my function score functions which I didn't have and it's now boosting the results as desired.

---

<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: [June 12, 2017, 9:58am UTC](https://discuss.elastic.co/t/nest-v5-how-to-use-functionscorequery-functionscorefunction-to-always-boost-a-particular-field/84994/8 "2017-06-12T09:58:17Z")

</div>

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