# How to retrieve payloads as byte arrays in Java scoring script?

**URL:** https://discuss.elastic.co/t/how-to-retrieve-payloads-as-byte-arrays-in-java-scoring-script/41399
**Category:** Elasticsearch
**Created:** [February 10, 2016, 4:08pm UTC](https://discuss.elastic.co/t/how-to-retrieve-payloads-as-byte-arrays-in-java-scoring-script/41399 "2016-02-10T16:08:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![steinar](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@steinar](https://discuss.elastic.co/u/steinar)
#### Post date: [February 10, 2016, 4:08pm UTC](https://discuss.elastic.co/t/how-to-retrieve-payloads-as-byte-arrays-in-java-scoring-script/41399/1 "2016-02-10T16:08:32Z")

</div>

I am working with Elasticsearch 2.1.1. I am writing a native (Java) scoring script plugin by subclassing AbstractDoubleSearchScript. My goal is to iterate all of the payloads for a given term in a given field. I obtain an Iterator of TermPosition as follows:

`Iterator<TermPosition> itr = indexLookup().get(myField).get(myTerm).iterator();`

So far so good. I can now step through the TermPosition instances, which is great.

Now for the problem: Class org.elasticsearch.search.lookup.TermPosition exposes only the following public methods for retrieving payloads from term occurrences:

```
public String payloadAsString()
public float payloadAsFloat()
public int payloadAsInt()

```

Under the hood, of course, payloads are byte arrays. I have a highly customized kind of payload that is not suitable for viewing as String, float or int.

I think it would be easy, and I believe useful for covering many more payload use cases, to add the following method to class TermPosition:

`public byte[] payloadAsBytes()`

Is there a different way I should be using to step through the payloads of a term of a document as byte arrays that I am perhaps overlooking? If not, does the idea of adding the payloadAsBytes() method seem useful? I am new to the community so a pointer on how to vet such an idea and get it in the queue for development appreciated.

---

<div class="post-metadata">

### Author: ![steinar](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@steinar](https://discuss.elastic.co/u/steinar)
#### Post date: [February 18, 2016, 3:37pm UTC](https://discuss.elastic.co/t/how-to-retrieve-payloads-as-byte-arrays-in-java-scoring-script/41399/2 "2016-02-18T15:37:34Z")

</div>

Answering my own question here: Just use the public payload member on TermPosition. This lets you get at a payload as a byte array. Silly I did not see that before 🙂

---

<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:15pm UTC](https://discuss.elastic.co/t/how-to-retrieve-payloads-as-byte-arrays-in-java-scoring-script/41399/3 "2017-07-05T23:15:12Z")

</div>


