# Can we use a field in script score which is excluded in \_source

**URL:** <https://discuss.elastic.co/t/can-we-use-a-field-in-script-score-which-is-excluded-in-source/359742>\
**Category:** Elastic Search\
**Tags:** painless\
**Created:** [May 17, 2024, 10:32pm UTC](https://discuss.elastic.co/t/can-we-use-a-field-in-script-score-which-is-excluded-in-source/359742 "2024-05-17T22:32:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Sen1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_sen1/32/134604_2.png) [@Abhishek\_Sen1](https://discuss.elastic.co/u/Abhishek_Sen1)\
**Post date:** [May 17, 2024, 10:32pm UTC](https://discuss.elastic.co/t/can-we-use-a-field-in-script-score-which-is-excluded-in-source/359742/1 "2024-05-17T22:32:57Z")

</div>

Index mapping:

```auto
{
    "index":
    {
        "mappings":
        {
            "dynamic": "strict",
            "_source":
            {
                "excludes":
                [
                    "patientClaims"
                ]
            },
            "properties":
            {
                "patientClaims":
                {
                    "properties":
                    {
                      "diagnosisIcdCode": {
                        "type": "keyword"
                      },
                        "diversity":
                        {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
}

```

Query:

```auto
{
  "from": 0,
  "size": 20,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "function_score": {
                "query": {
                  "has_child": {
                    "type": "claim",
                    "score_mode": "sum", 
                    "query": {
                      "function_score": {
                        "query": {
                          "bool": {
                            "filter": [
                              {
                                "terms": {
                                  "patientClaims.diagnosisIcdCode": [
                                    "E7021"
                                  ]
                                }
                              }
                            ]
                          }
                        },
                        "functions": [
                          {
                            "script_score": {
                              "script": "doc['patientClaims.diversity'].size()==0 ? 0 : (doc['patientClaims.diversity'].value == 'White Non-Hispanic' ? 0 : 1)"
                            }
                          }
                        ]
                      }
                    },
                    "inner_hits":
                    {
                        "name": "patient_claims_matching_count",
                        "size": 0,
                        "_source": false
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

Top score is zero, nothing matches. Can we not use those fields which are excluded from `_source` in scrip score?

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [May 20, 2024, 12:28pm UTC](https://discuss.elastic.co/t/can-we-use-a-field-in-script-score-which-is-excluded-in-source/359742/2 "2024-05-20T12:28:18Z")

</div>

From our [scripting docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#_search_and_aggregation_scripts):

Field values can be accessed from a script using [doc-values](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#modules-scripting-doc-vals), [the `_source` field](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#modules-scripting-source), or [stored fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#modules-scripting-stored).

---

<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 17, 2024, 12:28pm UTC](https://discuss.elastic.co/t/can-we-use-a-field-in-script-score-which-is-excluded-in-source/359742/3 "2024-06-17T12:28:34Z")

</div>

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