# Most efficient way of accessing long\[\]\[\] data in Painless scripts

**URL:** <https://discuss.elastic.co/t/most-efficient-way-of-accessing-long-data-in-painless-scripts/336437>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 20, 2023, 6:48am UTC](https://discuss.elastic.co/t/most-efficient-way-of-accessing-long-data-in-painless-scripts/336437 "2023-06-20T06:48:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pyppe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pyppe/32/3531_2.png) [@Pyppe](https://discuss.elastic.co/u/Pyppe)\
**Post date:** [June 20, 2023, 6:48am UTC](https://discuss.elastic.co/t/most-efficient-way-of-accessing-long-data-in-painless-scripts/336437/1 "2023-06-20T06:48:07Z")

</div>

Hi!

We have a custom solution for calculating similarities using feature vectors. Each document in Elasticsearch index can have multiple entities. Thus, for each document we have basically `long[][]` formatted data we would like to use to calculate distances. Such as this:

```json
[ 
 [378322298287171600,-9182346388506132000,-7884923301547995000,2954398850619687400,5792760765226170000,6941191355558596000,-9175934689997701000,2453767474651472000],
 [2395942447151390700,7206045950792974000,-6761273774897486000,648553841033347700,-4591414079501816000,3563632123683616000,288379928265751740,733693665263878500],
  ...
]

```

How should we define the mappings in order to access this data in Painless scripts in a performant way:

```plaintext
long[][] vectors = doc['vectors'].value;

```

Thanks for all the tips! 🙏

---

<div class="post-metadata">

**Author:** ![Pyppe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pyppe/32/3531_2.png) [@Pyppe](https://discuss.elastic.co/u/Pyppe)\
**Post date:** [June 20, 2023, 1:06pm UTC](https://discuss.elastic.co/t/most-efficient-way-of-accessing-long-data-in-painless-scripts/336437/2 "2023-06-20T13:06:46Z")

</div>

FYI: I found [Access fields in a document with the field API | Elasticsearch Guide [8.8] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.8/script-fields-api.html) which talks about accessing binary format, so I tried that with a mapping:

```json
"viewVectors" : {
  "properties" : {
    "id_01_00" : {
      "type" : "binary",
      "store" : true,
      "doc_values" : true
    }
  }
}

```

And with that I seem to be able to get access to `BytesRef`... However, I'd like to use Java's `ByteArrayInputStream` & `ObjectInputStream` to transform it into `List<List<Long>>`, but apparently the Stream classes from `java.io.*` are not available in Painless. 😔

---

<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 18, 2023, 1:07pm UTC](https://discuss.elastic.co/t/most-efficient-way-of-accessing-long-data-in-painless-scripts/336437/3 "2023-07-18T13:07:38Z")

</div>

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