# How to avoid field\_value\_factor on 'must' clause

**URL:** https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449
**Category:** Elasticsearch
**Created:** [September 6, 2023, 1:33pm UTC](https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449 "2023-09-06T13:33:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![blinker1](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@blinker1](https://discuss.elastic.co/u/blinker1)
#### Post date: [September 6, 2023, 1:33pm UTC](https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449/1 "2023-09-06T13:33:35Z")

</div>

Hi, running the ELK stack on the Elastic cloud, I've defined a search template that has the following query:

```auto
"query": {
  "function_score": {
    "functions": [
      {
        "field_value_factor": {
          "field": "marketcap",
          "missing": 1,
          "modifier": "log1p"
        }
      }
    ],
    "query": {
      "bool": {
        "should": [
          {
            "multi_match": {
              "fields": [
                "name",
                "symbol^3"
              ],
              "query": "{{query_string}}",
              "type": "phrase_prefix"
            }
          },
          {
            "multi_match": {
              "fields": [
                "name"
              ],
              "fuzziness": "auto",
              "query": "{{query_string}}"
            }
          },
          {
            "match_phrase": {
              "symbol": {
                "boost": 3,
                "query": "{{query_string}}"
              }
            }
          },
        ]
      }
    }
  }
}

```

which scores the `name` and `symbol` fields of the document, and then adds score according to the field `marketcap`, in the form of math's log function.

I want to add a filter on field `isEnabled` so only documents with value `true` gets returned.

I tried adding it to the `bool` clause, under `must`, but then, the `function_score` gets applied to all of the documents, and not only to the results in the `should` clause (which makes sense).

how can get the desired result, which is filtering out all documents with `isEnabled` set to `false` but only use the `marketcap` score on the results of the `should` clause?

thanks!

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [September 6, 2023, 5:01pm UTC](https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449/2 "2023-09-06T17:01:41Z")

</div>

Hi @blinker1

Did you apply the filter using the bool-filter? Show your query using the filter.

---

<div class="post-metadata">

### Author: ![blinker1](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@blinker1](https://discuss.elastic.co/u/blinker1)
#### Post date: [September 6, 2023, 8:38pm UTC](https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449/3 "2023-09-06T20:38:31Z")

</div>

using the filter (added a `must` in the `bool` section):

```auto
"query": {
  "function_score": {
    "functions": [
      {
        "field_value_factor": {
          "field": "marketcap",
          "missing": 1,
          "modifier": "log1p"
        }
      }
    ],
    "query": {
      "bool": {
        "must": {
          "match": {
              "isEnabled": true
          }
        },
        "should": [
          {
            "multi_match": {
              "fields": [
                "name",
                "symbol^3"
              ],
              "query": "{{query_string}}",
              "type": "phrase_prefix"
            }
          },
          {
            "multi_match": {
              "fields": [
                "name"
              ],
              "fuzziness": "auto",
              "query": "{{query_string}}"
            }
          },
          {
            "match_phrase": {
              "symbol": {
                "boost": 3,
                "query": "{{query_string}}"
              }
            }
          },
        ]
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [September 6, 2023, 8:54pm UTC](https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449/4 "2023-09-06T20:54:04Z")

</div>

Try this:

If you filter the fields to keyword type it is always good to use [filter context](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html#filter-context).

```auto
{
  "query": {
  "function_score": {
    "functions": [
      {
        "field_value_factor": {
          "field": "marketcap",
          "missing": 1,
          "modifier": "log1p"
        }
      }
    ],
    "query": {
      "bool": {
        "filter": {
          "term": {
            "isEnabled": true
          }
        },
        "should": [
          {
            "multi_match": {
              "fields": [
                "name",
                "symbol^3"
              ],
              "query": "{{query_string}}",
              "type": "phrase_prefix"
            }
          },
          {
            "multi_match": {
              "fields": [
                "name"
              ],
              "fuzziness": "auto",
              "query": "{{query_string}}"
            }
          },
          {
            "match_phrase": {
              "symbol": {
                "boost": 3,
                "query": "{{query_string}}"
              }
            }
          }
        ]
      }
    }
  }
}
}

```

---

<div class="post-metadata">

### Author: ![blinker1](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@blinker1](https://discuss.elastic.co/u/blinker1)
#### Post date: [September 7, 2023, 4:40am UTC](https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449/5 "2023-09-07T04:40:23Z")

</div>

with `filter` and `term` now, documents that didn't match the `should` clause are returned with score 0.  
essentially, the whole index is returned. (with score 0). previously, using only the `should` clause returned only matching documents.

```auto
    "hits": {
        "total": {
            "value": 9306,
            "relation": "eq"
        },
        "max_score": 0.0,
        ...
     }

```

---

<div class="post-metadata">

### Author: ![blinker1](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@blinker1](https://discuss.elastic.co/u/blinker1)
#### Post date: [September 7, 2023, 6:28pm UTC](https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449/6 "2023-09-07T18:28:26Z")

</div>

any clues to what can be done next?

---

<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: [October 5, 2023, 6:28pm UTC](https://discuss.elastic.co/t/how-to-avoid-field-value-factor-on-must-clause/342449/7 "2023-10-05T18:28:49Z")

</div>

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