# Using groovy script within function\_score and access field payload

**URL:** <https://discuss.elastic.co/t/using-groovy-script-within-function-score-and-access-field-payload/28743>\
**Category:** Elasticsearch\
**Created:** [September 6, 2015, 2:43pm UTC](https://discuss.elastic.co/t/using-groovy-script-within-function-score-and-access-field-payload/28743 "2015-09-06T14:43:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [September 6, 2015, 2:43pm UTC](https://discuss.elastic.co/t/using-groovy-script-within-function-score-and-access-field-payload/28743/1 "2015-09-06T14:43:33Z")

</div>

I am trying to understand what is the best way to access the payload while using groovy script.

I want to do something like this:

```
sum = 0;
categories = doc['category'].values;
for (category in categories){
   sum += category.payload
}
return sum;

```

Based on elasticsearch [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-advanced-scripting.html) I can do it with \_index which is not what I am looking for, \_index give access to statistics in the scope of the index, and not for a specific document.

I want to go over every document, and take its payload and multiply it with some constant.

When doing this: \_index['category'].get('term', \_PAYLOADS) I will get a list of all payloads of "term", which is not what I am looking for.

Is there a way to access a field payload from the scope of a document?

---

<div class="post-metadata">

**Author:** ![a2tirb](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@a2tirb](https://discuss.elastic.co/u/a2tirb)\
**Post date:** [September 7, 2015, 4:06pm UTC](https://discuss.elastic.co/t/using-groovy-script-within-function-score-and-access-field-payload/28743/2 "2015-09-07T16:06:49Z")

</div>

Payloads are per occurrence of a term in a document so one term can have several payloads in one document. `_index['category'].get('term', _PAYLOADS)` will give you an iterator over the payloads and should have as many elements as there are occurrences of this term in a document.This will always be an iterator even if the term occurs only once.  
What do you mean by _When doing this: \_index['category'].get('term', \_PAYLOADS) I will get a list of all payloads of "term"_? Do you get back more than expected?

---

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [September 7, 2015, 7:46pm UTC](https://discuss.elastic.co/t/using-groovy-script-within-function-score-and-access-field-payload/28743/3 "2015-09-07T19:46:28Z")

</div>

Oh! so maybe I got this wrong.  
Let me try to put it into an example.

If I have the following 3 documents:

```
doc1: 
{
   "id": 1,
   "categories": ["1000|0.1","1001|0.2"]
}

doc2:
{
   "id": 2,
   "categories": ["1000|0.6","1001|0.7"]
}

 doc3:
{
   "id": 3,
   "categories": ["1000|0.4","1001|0.5"]
}

```

If I will do this:

```
_index['categories'].get('1000', _PAYLOADS)

```

What I understood is that I will get an iterator on:

```
[0.1,0.6,0.4] 

```

3 times, 1 for each doc.  
And not an iterator on:

```
[0.1] (in case of doc1)
[0.6] (in case of doc2)
[0.4] (in case of doc3)

```

Is that correct? or I got it wrong?

Thanks!

---

<div class="post-metadata">

**Author:** ![a2tirb](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@a2tirb](https://discuss.elastic.co/u/a2tirb)\
**Post date:** [September 8, 2015, 9:02am UTC](https://discuss.elastic.co/t/using-groovy-script-within-function-score-and-access-field-payload/28743/4 "2015-09-08T09:02:34Z")

</div>

You will get an iterator for each doc, each containing only the payloads for this document. There is actually no way to get all payloads for all documents at the same time.

Here is an example:

```auto
DELETE testidx

PUT testidx
{
  "mappings": {
    "doc": {
      "properties": {
        "categories": {
          "type": "string",
          "analyzer": "payload"
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "payload": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "delimited_payload_filter"
          ]
        }
      }
    }
  }
}

POST testidx/doc/
{
   "id": 1,
   "categories": ["1000|0.1","1001|0.2"]
}

POST testidx/doc/
{
   "id": 2,
   "categories": ["1000|0.2","1001|0.2"]
}

POST testidx/doc/
{
   "id": 3,
   "categories": ["1000|0.3","1001|0.2"]
}

GET testidx/doc/_search
{
  "fields": [
    "_source"
  ],
  "script_fields": {
    "payloads": {
      "script": "payloads = []; positions = _index['categories'].get('1000', _PAYLOADS); for(pos in positions){payloads.add(pos.payloadAsFloat(0))}; payloads"
    }
  }
}

```

yields:

```auto
"hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "testidx",
            "_type": "doc",
            "_id": "AU-sK5gxjoOwWjOATroI",
            "_score": 1,
            "_source": {
               "id": 3,
               "categories": [
                  "1000|0.3",
                  "1001|0.2"
               ]
            },
            "fields": {
               "payloads": [
                  [
                     0.3
                  ]
               ]
            }
         },
         {
            "_index": "testidx",
            "_type": "doc",
            "_id": "AU-sJk0KjoOwWjOATrk2",
            "_score": 1,
            "_source": {
               "id": 1,
               "categories": [
                  "1000|0.1",
                  "1001|0.2"
               ]
            },
            "fields": {
               "payloads": [
                  [
                     0.1
                  ]
               ]
            }
         },
         {
            "_index": "testidx",
            "_type": "doc",
            "_id": "AU-sK6BajoOwWjOATroJ",
            "_score": 1,
            "_source": {
               "id": 2,
               "categories": [
                  "1000|0.2",
                  "1001|0.2"
               ]
            },
            "fields": {
               "payloads": [
                  [
                     0.2
                  ]
               ]
            }
         }
      ]
   }

```

---

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [September 9, 2015, 3:15pm UTC](https://discuss.elastic.co/t/using-groovy-script-within-function-score-and-access-field-payload/28743/5 "2015-09-09T15:15:38Z")

</div>

This is awesome!! thanks!!!

Do you think it make sense to use \_index to access payload at scale?  
Meaning, I want to be able to query my index, running a groovy script which will use the \_index on every query.  
From what I read \_index is not very performant.

So is it safe to use \_index? or there is a more performant option?

And thanks again!!

---

<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:** [July 5, 2017, 11:51pm UTC](https://discuss.elastic.co/t/using-groovy-script-within-function-score-and-access-field-payload/28743/6 "2017-07-05T23:51:31Z")

</div>


