# How to apply filters in elasticsearch NEST functionscore function

**URL:** <https://discuss.elastic.co/t/how-to-apply-filters-in-elasticsearch-nest-functionscore-function/245839>\
**Category:** Elasticsearch\
**Created:** [August 21, 2020, 2:18am UTC](https://discuss.elastic.co/t/how-to-apply-filters-in-elasticsearch-nest-functionscore-function/245839 "2020-08-21T02:18:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![learning21](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@learning21](https://discuss.elastic.co/u/learning21)\
**Post date:** [August 21, 2020, 2:18am UTC](https://discuss.elastic.co/t/how-to-apply-filters-in-elasticsearch-nest-functionscore-function/245839/1 "2020-08-21T02:18:21Z")

</div>

To control the score of documents based on a field value, I am using filter with function\_score in my DSL query and this gets the results ordered as I expect.

However on implementing this in NEST, the results are different; the score is not applied to the filter value. From further investigation I find that some versions of C# NEST Filter is not supported with ScoreFunctionsDescriptor. Is this still the case? Can you please assist with a working option to implement this with NEST? (I am new to Elastic Search and C# so please excuse if its a noob question).

I am currently using Elasticsearch v7.6, and NEST v7.5.1.

Thanks!

DSL query

```auto
GET /help/_search
{
  "query":{
   "function_score": {
     "query": {
     "bool": {
      "must": [
        {
          "multi_match": {
            "query": "AGC",
            "fields": [
              "title^2",
              "description^1"
            ],
            "type":"most_fields",
            "fuzziness": "AUTO:4,8",
            "prefix_length": 2,
            "boost": 5            
          }
        }
        ]
  }
  },
  **"functions": [**
 **{**
 **"filter": {**
 **"term":{**
 **"product":"A"**
 **}**
 **},**
 **"weight": 45**
 **},**
 **{**
 **"filter": {**
 **"term":{**
 **"product":"B"**
 **}**
 **},**
 **"weight": 20**
 **},**
 **{**
 **"filter": {**
 **"term":{**
 **"product":"C"**
 **}**
 **},**
 **"weight": 10**
 **}**
**],**
    "score_mode": "max",
    "boost_mode": "multiply"    
}
}
}

```

---

<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:** [August 21, 2020, 7:15am UTC](https://discuss.elastic.co/t/how-to-apply-filters-in-elasticsearch-nest-functionscore-function/245839/2 "2020-08-21T07:15:30Z")

</div>

[I answered your same question on Stack Overflow](https://stackoverflow.com/a/63515810/1831); will add here for completeness.

> [@learning21](#):
>
> From further investigation I find that some versions of C# NEST Filter is not supported with ScoreFunctionsDescriptor. Is this still the case?

[The function you're looking for is a weight function](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-weight) with a filter applied. This has been supported for a _long_ time in the client.

The client equivalent would be something like

```auto
var response = client.Search<object>(s => s
	.Query(q => q
		.FunctionScore(fs => fs
			.Query(fsq => fsq
				.Bool(b => b
					.Must(mu => mu
						.MultiMatch(mm => mm
							.Query("AGC")
							.Fields(new[] { "title^2", "description^1" })
							.Type(TextQueryType.MostFields)
							.Fuzziness(Fuzziness.AutoLength(4,8))
							.PrefixLength(2)
							.Boost(5)
						)
					)
				)
			)
			.Functions(fu => fu
				.Weight(w => w
					.Filter(f => f
						.Term("product", "A")
					)
					.Weight(45)
				)
				.Weight(w => w
					.Filter(f => f
						.Term("product", "B")
					)
					.Weight(20)
				)
				.Weight(w => w
					.Filter(f => f
						.Term("product", "C")
					)
					.Weight(10)
				)
			)
			.ScoreMode(FunctionScoreMode.Max)
			.BoostMode(FunctionBoostMode.Multiply)
		)
	)
);

```

There's a couple of things that you may want to look at:

1. `description^1` can be replaced with simply `description` as the default boost is 1
2. This may be better expressed as a `bool` query with `should` clauses for each of the queries in each weight function, with a boost applied similar to how weight is being used. Something like

```auto
var response = client.Search<object>(s => s
	.Query(q => q
		.Bool(b => b
			.Must(mu => mu
				.MultiMatch(mm => mm
					.Query("AGC")
					.Fields(new[] { "title^2", "description^1" })
					.Type(TextQueryType.MostFields)
					.Fuzziness(Fuzziness.AutoLength(4, 8))
					.PrefixLength(2)
					.Boost(5)
				)
			)
			.Should(
				sh => sh.Term(t => t
					.Field("product")
					.Value("A")
					.Boost(45)
				),
				sh => sh.Term(t => t
					.Field("product")
					.Value("B")
					.Boost(20)
				),
				sh => sh.Term(t => t
					.Field("product")
					.Value("C")
					.Boost(10)
				)
			)
		)
	)
);

```

---

<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:** [September 18, 2020, 7:15am UTC](https://discuss.elastic.co/t/how-to-apply-filters-in-elasticsearch-nest-functionscore-function/245839/3 "2020-09-18T07:15:35Z")

</div>

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