# Script field along with all the other fields using Painless

**URL:** <https://discuss.elastic.co/t/script-field-along-with-all-the-other-fields-using-painless/296384>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 6, 2022, 10:23am UTC](https://discuss.elastic.co/t/script-field-along-with-all-the-other-fields-using-painless/296384 "2022-02-06T10:23:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saurab](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@saurab](https://discuss.elastic.co/u/saurab)\
**Post date:** [February 6, 2022, 10:23am UTC](https://discuss.elastic.co/t/script-field-along-with-all-the-other-fields-using-painless/296384/1 "2022-02-06T10:23:06Z")

</div>

I have data indexed as follows:

```auto
{"index":{"_id":1}}
{"first":"johnny","last":"gaudreau","goals":[9,27,1],"born":"1993/08/13"}
{"index":{"_id":2}}
{"first":"sean","last":"monohan","goals":[7,54,26],"born":"1994/10/12"}

```

What I want is to calculate the total goals for each player, so I use scripted fields as follows:

```auto
GET hockey/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "total_goals": {
      "script": {
        "lang": "painless",
        "source": """
          int total = 0;
          for (int i = 0; i < doc['goals'].length; ++i) {
            total += doc['goals'][i];
          }
          return total;
        """
      }
    }
  }
}

```

the result:

```auto
"hits" : [
      {
        "_index" : "hockey",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "fields" : {
          "total_goals" : [
            37
          ]
        }
      },
      {
        "_index" : "hockey",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "fields" : {
          "total_goals" : [
            87
          ]
        }
      }

```

This works fine, but in the result I have "total\_goals" for each player, but not the first name and last name. How do I include the first and last name in the results ?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 6, 2022, 11:06am UTC](https://discuss.elastic.co/t/script-field-along-with-all-the-other-fields-using-painless/296384/2 "2022-02-06T11:06:46Z")

</div>

There are two ways.

The results are completely the same but the latter is better for the frexibility that you can use the field for aggregation for example.

Add `_source: true`:

```auto
GET test_hockey/_search
{
  "query": {
    "match_all": {}
  },
  "_source": true, 
  "script_fields": {
    "total_goals": {
      "script": {
        "lang": "painless",
        "source": """
          int total = 0;
          for (int i = 0; i < doc['goals'].length; ++i) {
            total += doc['goals'][i];
          }
          return total;
        """
      }
    }
  }
}

```

Use `runtime mappings` (use `emit` instead of `return`) and add `fields` parameter:

```auto
GET test_hockey/_search
{
  "query": {
    "match_all": {}
  },
  "runtime_mappings": {
    "total_goals": {
      "type":"long",
      "script": {
        "lang": "painless",
        "source": """
          int total = 0;
          for (int i = 0; i < doc['goals'].length; ++i) {
            total += doc['goals'][i];
          }
          emit(total);
        """
      }
    }
  },
  "fields":["total_goals"]
}

```

---

<div class="post-metadata">

**Author:** ![saurab](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@saurab](https://discuss.elastic.co/u/saurab)\
**Post date:** [February 6, 2022, 11:44am UTC](https://discuss.elastic.co/t/script-field-along-with-all-the-other-fields-using-painless/296384/3 "2022-02-06T11:44:49Z")

</div>

Thank you so much. both ways are useful for me. thanks again.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 6, 2022, 12:10pm UTC](https://discuss.elastic.co/t/script-field-along-with-all-the-other-fields-using-painless/296384/4 "2022-02-06T12:10:19Z")

</div>

I forgot to tell one thing. The latter is not always better.

Script\_fields are calculated for only matched document, while runtime mappings are calculated for all documents. Though this is the very reason that runtime mappings can be used for query and aggregation, if the index is huge and you query to filter few documents, script\_fields could be much faster. In this case of `match_all` query, there is no difference in fact.

---

<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:** [March 6, 2022, 12:10pm UTC](https://discuss.elastic.co/t/script-field-along-with-all-the-other-fields-using-painless/296384/5 "2022-03-06T12:10:24Z")

</div>

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