# Function\_score with several functions

**URL:** https://discuss.elastic.co/t/function-score-with-several-functions/343786
**Category:** Elasticsearch
**Tags:** painless
**Created:** [September 25, 2023, 3:13pm UTC](https://discuss.elastic.co/t/function-score-with-several-functions/343786 "2023-09-25T15:13:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![MikeT1234](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MikeT1234](https://discuss.elastic.co/u/MikeT1234)
#### Post date: [September 25, 2023, 3:13pm UTC](https://discuss.elastic.co/t/function-score-with-several-functions/343786/1 "2023-09-25T15:13:04Z")

</div>

Could somebody point me to the right direction? 🤓

I'm trying to search items and categorize / score them based on several criteria.  
`function_score` seems to do all the right things except I it just returns total sum of scores instead of scores for each functions.

```auto
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {
            "match": {
              "industry.group.code": "A"
            }
          },
          "weight": 70
        },
        {
          "filter": {
            "match": {
              "name": "Apple"
            }
          },
          "weight": 30
        }
      ],
      "score_mode": "sum",
      "boost_mode": "sum",
      "min_score": 42
    }
  }
}

```

I've been trying to add missing scores fields by using `script_score` but failed so far.

```auto
{
  "filter": {
    "match": {
      "industry.group.code": "A"
    }
  },
  "script_score": {
    "script": {
      "lang": "painless",
      "source": "ctx._source.score_industry == _score // <-- THIS DOESNT WORK; return _score;"
    }
  },
  "weight": 70
}

```

I found this topic but it seemed to be quite old

> <https://github.com/elastic/elasticsearch/issues/20068>
>
> \<!--
> GitHub is reserved for bug reports and feature requests. The best place
> to …ask a general question is at the Elastic Discourse forums at
> https://discuss.elastic.co. If you are in fact posting a bug report or
> a feature request, please include one and only one of the below blocks
> in your new issue. Note that whether you're filing a bug report or a
> feature request, ensure that your submission is for an
> \[OS that we support\](https://www.elastic.co/support/matrix#show\_os).
> Bug reports on an OS that we do not support or feature requests
> specific to an OS that we do not support will be closed.
> \--\>
> 
> \<!--
> If you are filing a bug report, please remove the below feature
> request block and provide responses for all of the below items.
> \--\>
> 
> \*\*Elasticsearch version\*\*: master
> 
> \*\*Plugins installed\*\*: none
> 
> \*\*JVM version\*\*: 1.8.0\_60
> 
> \*\*OS version\*\*: OSX El Capitan
> 
> \*\*Description of the problem including expected versus actual behavior\*\*:
> 
> According to \[the docs\](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-request-script-fields.html) \`script\_fields\` can access the document \_source via a \`\_source\` keyword. When attempting this with a Painless script, I get a \`Variable \[\_source\] is not defined.\` error.
> 
> \*\*Steps to reproduce\*\*:
> 1. Execute a query with a painless script\_field that attempts to access \_source, something like this:
> 
> \`\`\`
> "script\_fields": {
> "scriptip": {
> "script": {
> "inline": "return \_source.ip;",
> "lang": "painless"
> }
> }
> }
> \`\`\`
> 
> \*\*Provide logs (if relevant)\*\*:
> 
> \`\`\`
> {
> "error": {
> "root\_cause": \[
> {
> "type": "script\_exception",
> "reason": "compile error",
> "script\_stack": \[
> "return \_source.ip;",
> " ^---- HERE"
> \],
> "script": "return \_source.ip;",
> "lang": "painless"
> }
> \],
> "type": "search\_phase\_execution\_exception",
> "reason": "all shards failed",
> "phase": "query\_fetch",
> "grouped": true,
> "failed\_shards": \[
> {
> "shard": 0,
> "index": "logstash-0",
> "node": "00s71MPnSRenvz6mflLQeQ",
> "reason": {
> "type": "script\_exception",
> "reason": "compile error",
> "caused\_by": {
> "type": "illegal\_argument\_exception",
> "reason": "Variable \[\_source\] is not defined."
> },
> "script\_stack": \[
> "return \_source.ip;",
> " ^---- HERE"
> \],
> "script": "return \_source.ip;",
> "lang": "painless"
> }
> }
> \],
> "caused\_by": {
> "type": "script\_exception",
> "reason": "compile error",
> "caused\_by": {
> "type": "illegal\_argument\_exception",
> "reason": "Variable \[\_source\] is not defined."
> },
> "script\_stack": \[
> "return \_source.ip;",
> " ^---- HERE"
> \],
> "script": "return \_source.ip;",
> "lang": "painless"
> }
> },
> "status": 500
> }
> \`\`\`
> 
> \*\*Describe the feature\*\*:

**So I'm wondering if it's possible to add new fields under script\_score or are there any alternative way to do this?**

---

<div class="post-metadata">

### Author: ![MikeT1234](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MikeT1234](https://discuss.elastic.co/u/MikeT1234)
#### Post date: [September 27, 2023, 6:46am UTC](https://discuss.elastic.co/t/function-score-with-several-functions/343786/2 "2023-09-27T06:46:32Z")

</div>

I would appreciate your comments about this 😜

---

<div class="post-metadata">

### Author: ![MikeT1234](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MikeT1234](https://discuss.elastic.co/u/MikeT1234)
#### Post date: [October 1, 2023, 1:55pm UTC](https://discuss.elastic.co/t/function-score-with-several-functions/343786/3 "2023-10-01T13:55:23Z")

</div>

Can anyone push me to the right direction? 😬

---

<div class="post-metadata">

### Author: ![MikeT1234](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MikeT1234](https://discuss.elastic.co/u/MikeT1234)
#### Post date: [October 9, 2023, 1:36pm UTC](https://discuss.elastic.co/t/function-score-with-several-functions/343786/4 "2023-10-09T13:36:03Z")

</div>

Anyone?

---

<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: [November 6, 2023, 1:36pm UTC](https://discuss.elastic.co/t/function-score-with-several-functions/343786/5 "2023-11-06T13:36:55Z")

</div>

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