# Access to documents in ScriptEngine plugin

**URL:** <https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278>\
**Category:** Elasticsearch\
**Created:** [July 2, 2018, 9:08pm UTC](https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278 "2018-07-02T21:08:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameron\_VandenBerg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cameron_vandenberg/32/45440_2.png) [@Cameron\_VandenBerg](https://discuss.elastic.co/u/Cameron_VandenBerg)\
**Post date:** [July 2, 2018, 9:08pm UTC](https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278/1 "2018-07-02T21:08:00Z")

</div>

Is it possible to access document fields or termvectors within a plugin that implements ScriptEngine? I have a field for each document which contains the length of the document, and I would like to use that value as well as tf to compute a score. I am following this example for writing a plugin: [https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-engine.html](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-engine.html). However, all my attempts to access the actual document come back null.

---

<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:** [July 2, 2018, 9:21pm UTC](https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278/2 "2018-07-02T21:21:39Z")

</div>

Can you share any of your code?

---

<div class="post-metadata">

**Author:** ![Cameron\_VandenBerg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cameron_vandenberg/32/45440_2.png) [@Cameron\_VandenBerg](https://discuss.elastic.co/u/Cameron_VandenBerg)\
**Post date:** [July 3, 2018, 3:03pm UTC](https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278/3 "2018-07-03T15:03:04Z")

</div>

Here is one example of what I have tried. This is a part of my class which implements ScriptEngine just like in the example. The problem is that the Document, which is returned from the reader (the LeafReader from the context) is null. Is there any way to get a handle on the document within the ScriptEngine context?

'''  
@Override  
public SearchScript newInstance(LeafReaderContext context) throws IOException {  
LeafReader reader = context.reader();  
PostingsEnum postings = context.reader().postings(new Term(field, term));

```
				return new SearchScript(p, lookup, context) {
					int currentDocid = -1;

					@Override
					public void setDocument(int docid) {
						if (postings != null) {
							// advance has undefined behavior calling with a
							// docid <= its current docid
							if (postings.docID() < docid) {
								try {
									postings.advance(docid);
								} catch (IOException e) {
									throw new UncheckedIOException(e);
								}
							}
						}
						currentDocid = docid;
					}

					@Override
					public double runAsDouble() {
						try {
							double mleScore = 0f;
							Document doc = reader.document(currentDocid);
							System.out.println("Document: " + doc);
							double doclen = Double.valueOf(doc.getField("body_len").stringValue()).doubleValue();
							System.out.println("Document length: " + doclen);
							if (postings != null) {
								mleScore = postings.freq() / doclen;
							}

							return mleScore;
						} catch (IOException e) {
							throw new UncheckedIOException(e);
						}
					}
				};
			}

```

'''

---

<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:** [July 3, 2018, 4:27pm UTC](https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278/4 "2018-07-03T16:27:40Z")

</div>

If you want doc values, the `Document` is not what you want (that is for stored fields access). You need to get an appropriate doc values instance for the type of data. You can do this using the helper `DocValues` class from Lucene. For example, to get numeric doc values for a field called "mynum", you would do this next to the `postings` declaration:

```auto
SortedNumericDocValues mynumValues = DocValues.getSortedNumeric(reader, "mynum");
boolean hasMynumValue;

```

Then in `setDocument`:

```auto
hasMynumValue = mynumValues.advanceExact(docid);

```

And finally in the scoring function you can use the docvalue iterator to extract the values for the current document by calling `mynumValues.nextValue()` for each value the doc has (you can find how many values to expect with `mynumValues.docValuesCount()`.

---

<div class="post-metadata">

**Author:** ![Cameron\_VandenBerg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cameron_vandenberg/32/45440_2.png) [@Cameron\_VandenBerg](https://discuss.elastic.co/u/Cameron_VandenBerg)\
**Post date:** [July 3, 2018, 7:52pm UTC](https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278/5 "2018-07-03T19:52:45Z")

</div>

Thank you. If the termvector is stored for a field, is there a way to access that in the ScriptEngine as well?

---

<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:** [July 10, 2018, 6:46am UTC](https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278/6 "2018-07-10T06:46:35Z")

</div>

Yes, you could access term vectors, but you will need to [read Lucene documentation](https://lucene.apache.org/core/7_3_0/core/org/apache/lucene/index/IndexReader.html#getTermVector-int-java.lang.String-) to learn how to access. An advanced script implemented through a ScriptEngine has a LeafReader, which is an IndexReader, and has `getTermVector`.

---

<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:** [August 7, 2018, 6:46am UTC](https://discuss.elastic.co/t/access-to-documents-in-scriptengine-plugin/138278/7 "2018-08-07T06:46:38Z")

</div>

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