# Cannot access doc values after forcemerging

**URL:** <https://discuss.elastic.co/t/cannot-access-doc-values-after-forcemerging/206870>\
**Category:** Elasticsearch\
**Created:** [November 6, 2019, 11:07pm UTC](https://discuss.elastic.co/t/cannot-access-doc-values-after-forcemerging/206870 "2019-11-06T23:07:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_Reback](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_reback/32/38713_2.png) [@Joshua\_Reback](https://discuss.elastic.co/u/Joshua_Reback)\
**Post date:** [November 6, 2019, 11:07pm UTC](https://discuss.elastic.co/t/cannot-access-doc-values-after-forcemerging/206870/1 "2019-11-06T23:07:04Z")

</div>

Hi, I am working on a scripted engine which needs to access doc values to do some calculations.

The code it uses to access the doc value below:

**ScriptDocValues.Longs docValues = (ScriptDocValues.Longs) lookup.doc().getLeafDocLookup(context).get(length\_field);**

- length\_field is the name of a field (and is a parameter to the script)
- lookup is of type org.elasticsearch.search.lookup.SearchLookup (it is provided in the scripted engine)
- context is of type org.elasticsearch.script.ScriptContext (it is also provided in the scripted engine)

When I create an index, and put a few documents in that index, the scripted engine can execute just fine.

However, after forcemerging and setting max\_num\_segments to 1, running the scripted engine results in an index\_out\_of\_bounds exception from Elasticsearch. Interestingly, even when I get an exception, I can see that the key is still present in the doc values because I tried logging the output of the containsKey method:

**lookup.doc().getLeafDocLookup(context).containsKey(length\_field)**

and it prints true.

Can somebody please help me with this?

(Note I think this question is asking the same thing: [Access doc fields value in script-expert-scoring](https://discuss.elastic.co/t/access-doc-fields-value-in-script-expert-scoring/171217))

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 9, 2019, 12:03am UTC](https://discuss.elastic.co/t/cannot-access-doc-values-after-forcemerging/206870/2 "2019-11-09T00:03:24Z")

</div>

Can you show the full stacktrace? Can you reproduce it using painless instead of your search engine? Which version of elasticsearch are you using?

---

<div class="post-metadata">

**Author:** ![Joshua\_Reback](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_reback/32/38713_2.png) [@Joshua\_Reback](https://discuss.elastic.co/u/Joshua_Reback)\
**Post date:** [November 11, 2019, 7:30pm UTC](https://discuss.elastic.co/t/cannot-access-doc-values-after-forcemerging/206870/3 "2019-11-11T19:30:12Z")

</div>

Hi Igor, thanks for the reply.

I actually managed to get it working by using the Lucene docValue methods directly. In case it helps anyone else, the code I used is below:

```
SortedNumericDocValues iterator = context.reader().getSortedNumericDocValues(length_field);
if (!iterator.advanceExact(currentDocid)){
    throw new IOException("Cannot read length of document");
}
long bodyLength = iterator.nextValue();

```

The Lucene methods used above are documented here: [https://lucene.apache.org/core/7\_5\_0/core/org/apache/lucene/index/SortedNumericDocValues.html](https://lucene.apache.org/core/7_5_0/core/org/apache/lucene/index/SortedNumericDocValues.html)

---

<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:** [December 9, 2019, 7:36pm UTC](https://discuss.elastic.co/t/cannot-access-doc-values-after-forcemerging/206870/4 "2019-12-09T19:36:20Z")

</div>

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