# Native script doc() access problems

**URL:** <https://discuss.elastic.co/t/native-script-doc-access-problems/9496>\
**Category:** Elasticsearch\
**Created:** [October 29, 2012, 8:33pm UTC](https://discuss.elastic.co/t/native-script-doc-access-problems/9496 "2012-10-29T20:33:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_King](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_king/32/2631_2.png) [@William\_King](https://discuss.elastic.co/u/William_King)\
**Post date:** [October 29, 2012, 8:33pm UTC](https://discuss.elastic.co/t/native-script-doc-access-problems/9496/1 "2012-10-29T20:33:06Z")

</div>

Hello,

I'm having a problem with a native script I'm currently working on. In the  
start of the script I use doc().get() to access data and all is well.  
However later on I try to access data again in the same manner (after an if  
statement) and I get a blank value returned. If I try access these fields  
from earlier on in the script all is well.

I was trying to only access certain fields in an if statement to keep the  
amount of fields I accessed limited, but it seems that this won't work?

--

---

<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:** [October 29, 2012, 9:57pm UTC](https://discuss.elastic.co/t/native-script-doc-access-problems/9496/2 "2012-10-29T21:57:54Z")

</div>

Could you post some code that would help us to reproduce the problem?

On Monday, October 29, 2012 4:33:06 PM UTC-4, William King wrote:

> Hello,
> 
> I'm having a problem with a native script I'm currently working on. In the  
> start of the script I use doc().get() to access data and all is well.  
> However later on I try to access data again in the same manner (after an  
> if statement) and I get a blank value returned. If I try access these  
> fields from earlier on in the script all is well.
> 
> I was trying to only access certain fields in an if statement to keep the  
> amount of fields I accessed limited, but it seems that this won't work?

--

---

<div class="post-metadata">

**Author:** ![William\_King](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_king/32/2631_2.png) [@William\_King](https://discuss.elastic.co/u/William_King)\
**Post date:** [October 29, 2012, 10:07pm UTC](https://discuss.elastic.co/t/native-script-doc-access-problems/9496/3 "2012-10-29T22:07:36Z")

</div>

It's actually looking like the problem is something else entirely.

The script I'm running is within a "has\_child" filter (not query) and it  
looks like sometimes the filter is hitting the parent of the children that  
I'm querying. So my previous diagnostic was completely off base. I'm  
guessing this is the issue as I've been checking the doc.field("\_id")  
values of each document hit and the ones that are causing issues have  
document IDs which correspond to the parent and not the child.

Is this behavior normal?

On Monday, October 29, 2012 2:57:54 PM UTC-7, Igor Motov wrote:

> Could you post some code that would help us to reproduce the problem?
> 
> On Monday, October 29, 2012 4:33:06 PM UTC-4, William King wrote:
> 
> > Hello,
> > 
> > I'm having a problem with a native script I'm currently working on. In  
> > the start of the script I use doc().get() to access data and all is well.  
> > However later on I try to access data again in the same manner (after an  
> > if statement) and I get a blank value returned. If I try access these  
> > fields from earlier on in the script all is well.
> > 
> > I was trying to only access certain fields in an if statement to keep the  
> > amount of fields I accessed limited, but it seems that this won't work?

--

---

<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:** [October 29, 2012, 10:22pm UTC](https://discuss.elastic.co/t/native-script-doc-access-problems/9496/4 "2012-10-29T22:22:25Z")

</div>

I am not sure which type of script you are running in your "has\_child"  
filter. Could you give an example of the query that you are executing?

On Monday, October 29, 2012 6:07:36 PM UTC-4, William King wrote:

> It's actually looking like the problem is something else entirely.
> 
> The script I'm running is within a "has\_child" filter (not query) and it  
> looks like sometimes the filter is hitting the parent of the children that  
> I'm querying. So my previous diagnostic was completely off base. I'm  
> guessing this is the issue as I've been checking the doc.field("\_id")  
> values of each document hit and the ones that are causing issues have  
> document IDs which correspond to the parent and not the child.
> 
> Is this behavior normal?
> 
> On Monday, October 29, 2012 2:57:54 PM UTC-7, Igor Motov wrote:
> 
> > Could you post some code that would help us to reproduce the problem?
> > 
> > On Monday, October 29, 2012 4:33:06 PM UTC-4, William King wrote:
> > 
> > > Hello,
> > > 
> > > I'm having a problem with a native script I'm currently working on. In  
> > > the start of the script I use doc().get() to access data and all is well.  
> > > However later on I try to access data again in the same manner (after an  
> > > if statement) and I get a blank value returned. If I try access these  
> > > fields from earlier on in the script all is well.
> > > 
> > > I was trying to only access certain fields in an if statement to keep  
> > > the amount of fields I accessed limited, but it seems that this won't work?

--

---

<div class="post-metadata">

**Author:** ![William\_King](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_king/32/2631_2.png) [@William\_King](https://discuss.elastic.co/u/William_King)\
**Post date:** [October 29, 2012, 10:35pm UTC](https://discuss.elastic.co/t/native-script-doc-access-problems/9496/5 "2012-10-29T22:35:27Z")

</div>

Sure, I'm actually calling this from a plugin I'm working on. Here's a gist  
of the java code:

> <https://gist.github.com/UpOutServers/3976983>

On Monday, October 29, 2012 3:22:25 PM UTC-7, Igor Motov wrote:

> I am not sure which type of script you are running in your "has\_child"  
> filter. Could you give an example of the query that you are executing?
> 
> On Monday, October 29, 2012 6:07:36 PM UTC-4, William King wrote:
> 
> > It's actually looking like the problem is something else entirely.
> > 
> > The script I'm running is within a "has\_child" filter (not query) and it  
> > looks like sometimes the filter is hitting the parent of the children that  
> > I'm querying. So my previous diagnostic was completely off base. I'm  
> > guessing this is the issue as I've been checking the doc.field("\_id")  
> > values of each document hit and the ones that are causing issues have  
> > document IDs which correspond to the parent and not the child.
> > 
> > Is this behavior normal?
> > 
> > On Monday, October 29, 2012 2:57:54 PM UTC-7, Igor Motov wrote:
> > 
> > > Could you post some code that would help us to reproduce the problem?
> > > 
> > > On Monday, October 29, 2012 4:33:06 PM UTC-4, William King wrote:
> > > 
> > > > Hello,
> > > > 
> > > > I'm having a problem with a native script I'm currently working on. In  
> > > > the start of the script I use doc().get() to access data and all is well.  
> > > > However later on I try to access data again in the same manner (after  
> > > > an if statement) and I get a blank value returned. If I try access these  
> > > > fields from earlier on in the script all is well.
> > > > 
> > > > I was trying to only access certain fields in an if statement to keep  
> > > > the amount of fields I accessed limited, but it seems that this won't work?

--

---

<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:** [October 30, 2012, 2:01am UTC](https://discuss.elastic.co/t/native-script-doc-access-problems/9496/6 "2012-10-30T02:01:33Z")

</div>

Looking at your java code I would say  
that TimeframeFilterScriptFactory.NAME should be called for every  
timeframe and TimeframeSortScriptFactory.NAME should be called for every  
event.

I tried to reproduce the issue based on your description, but everything  
seems to be working fine: [gist:3977859 · GitHub](https://gist.github.com/3977859) Could you  
modify this script to demonstrate the issue that you are experiencing?

On Monday, October 29, 2012 6:35:27 PM UTC-4, William King wrote:

> Sure, I'm actually calling this from a plugin I'm working on. Here's a  
> gist of the java code:
> 
> [gist:3976983 · GitHub](https://gist.github.com/3976983)
> 
> On Monday, October 29, 2012 3:22:25 PM UTC-7, Igor Motov wrote:
> 
> > I am not sure which type of script you are running in your "has\_child"  
> > filter. Could you give an example of the query that you are executing?
> > 
> > On Monday, October 29, 2012 6:07:36 PM UTC-4, William King wrote:
> > 
> > > It's actually looking like the problem is something else entirely.
> > > 
> > > The script I'm running is within a "has\_child" filter (not query) and it  
> > > looks like sometimes the filter is hitting the parent of the children that  
> > > I'm querying. So my previous diagnostic was completely off base. I'm  
> > > guessing this is the issue as I've been checking the doc.field("\_id")  
> > > values of each document hit and the ones that are causing issues have  
> > > document IDs which correspond to the parent and not the child.
> > > 
> > > Is this behavior normal?
> > > 
> > > On Monday, October 29, 2012 2:57:54 PM UTC-7, Igor Motov wrote:
> > > 
> > > > Could you post some code that would help us to reproduce the problem?
> > > > 
> > > > On Monday, October 29, 2012 4:33:06 PM UTC-4, William King wrote:
> > > > 
> > > > > Hello,
> > > > > 
> > > > > I'm having a problem with a native script I'm currently working on. In  
> > > > > the start of the script I use doc().get() to access data and all is well.  
> > > > > However later on I try to access data again in the same manner (after  
> > > > > an if statement) and I get a blank value returned. If I try access these  
> > > > > fields from earlier on in the script all is well.
> > > > > 
> > > > > I was trying to only access certain fields in an if statement to keep  
> > > > > the amount of fields I accessed limited, but it seems that this won't work?

--

---

<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 6, 2017, 3:06am UTC](https://discuss.elastic.co/t/native-script-doc-access-problems/9496/7 "2017-07-06T03:06:52Z")

</div>


