# How to access field value with LeafReaderContext in ScriptEngine

**URL:** <https://discuss.elastic.co/t/how-to-access-field-value-with-leafreadercontext-in-scriptengine/171069>\
**Category:** Elasticsearch\
**Created:** [March 6, 2019, 9:52am UTC](https://discuss.elastic.co/t/how-to-access-field-value-with-leafreadercontext-in-scriptengine/171069 "2019-03-06T09:52:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrikerMan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brikerman/32/36811_2.png) [@BrikerMan](https://discuss.elastic.co/u/BrikerMan)\
**Post date:** [March 6, 2019, 9:52am UTC](https://discuss.elastic.co/t/how-to-access-field-value-with-leafreadercontext-in-scriptengine/171069/1 "2019-03-06T09:52:23Z")

</div>

I am trying to create my customized `ScriptEngine`. In my case my data like  
{  
"body": "a b c a"  
}

```
{
  "body": "a b a a"
}

```

I want to access the body value "a b a a" during the ScoreScript.execute process. How I can do that?

```
private static class PureDfLeafFactory implements LeafFactory {

  @Override
  public ScoreScript newInstance(LeafReaderContext context)
  throws IOException {
    // is the any way to access field `body`'s value here
  }
}
```

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [March 9, 2019, 8:18pm UTC](https://discuss.elastic.co/t/how-to-access-field-value-with-leafreadercontext-in-scriptengine/171069/2 "2019-03-09T20:18:49Z")

</div>

Is the body field analyzed? If it is, you'll need to copy the field to a keyword field which can be stored in doc values. Then access the field through the `context.reader().getSortedSetDocValues()` method.

Note that this is not advisable: doing string parsing in a scoring script is going to destroy performance.

---

<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:** [April 6, 2019, 8:18pm UTC](https://discuss.elastic.co/t/how-to-access-field-value-with-leafreadercontext-in-scriptengine/171069/3 "2019-04-06T20:18:55Z")

</div>

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