# Using java get document api within a script field

**URL:** <https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751>\
**Category:** Elasticsearch\
**Created:** [April 1, 2014, 11:44pm UTC](https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751 "2014-04-01T23:44:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mat1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mat1/32/1679_2.png) [@mat1](https://discuss.elastic.co/u/mat1)\
**Post date:** [April 1, 2014, 11:44pm UTC](https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751/1 "2014-04-01T23:44:40Z")

</div>

Is it possible to query the database from a script field by instantiating a java client and issuing a get request?  
Are there any examples of this?

Thanks  
Mat

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1d61260b-72e5-45de-bcfd-ffc74dacdc2a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1d61260b-72e5-45de-bcfd-ffc74dacdc2a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![mat1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mat1/32/1679_2.png) [@mat1](https://discuss.elastic.co/u/mat1)\
**Post date:** [April 2, 2014, 3:46am UTC](https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751/2 "2014-04-02T03:46:22Z")

</div>

Figured this out, however the code below seems to restart a new node on  
each request, and is not cleaning up after itself.  
Is there a better way to do this - for example by exposing a shared node  
object to the mvel script plugin ?

PUT /test/user/1  
{ "name":"jane"  
, "partner":2  
}

PUT /test/user/2  
{ "name":"john"  
, "partner":1  
}

GET /test/user/\_search  
{ "\_source":true  
, "script\_fields":  
{ "partner":  
{  
"script":"org.elasticsearch.node.NodeBuilder.nodeBuilder().node().client().prepareGet('test',  
'user',\_source.partner).execute().actionGet().getSource()"  
}  
}  
}

On Tuesday, April 1, 2014 4:44:40 PM UTC-7, mat taylor wrote:

> Is it possible to query the database from a script field by instantiating  
> a java client and issuing a get request?  
> Are there any examples of this?
> 
> Thanks  
> Mat

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a0f57ffc-cecf-4bc8-8082-9f5737faea87%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a0f57ffc-cecf-4bc8-8082-9f5737faea87%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 2, 2014, 4:46am UTC](https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751/3 "2014-04-02T04:46:17Z")

</div>

O\_o starting a Node from a script? First time I see that... Looks like a hack to me! 🙂

That said what is the use case here? Are you trying to perform some JOIN using scripts?

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 2 avr. 2014 à 05:46, mat taylor [mat.taylor@gmail.com](mailto:mat.taylor@gmail.com) a écrit :

Figured this out, however the code below seems to restart a new node on each request, and is not cleaning up after itself.  
Is there a better way to do this - for example by exposing a shared node object to the mvel script plugin ?

PUT /test/user/1  
{ "name":"jane"  
, "partner":2  
}

PUT /test/user/2  
{ "name":"john"  
, "partner":1  
}

GET /test/user/\_search  
{ "\_source":true  
, "script\_fields":  
{ "partner":  
{ "script":"org.elasticsearch.node.NodeBuilder.nodeBuilder().node().client().prepareGet('test', 'user',\_source.partner).execute().actionGet().getSource()"  
}  
}  
}

> On Tuesday, April 1, 2014 4:44:40 PM UTC-7, mat taylor wrote:  
> Is it possible to query the database from a script field by instantiating a java client and issuing a get request?  
> Are there any examples of this?  
> Thanks  
> Mat

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a0f57ffc-cecf-4bc8-8082-9f5737faea87%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a0f57ffc-cecf-4bc8-8082-9f5737faea87%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0B3391C2-9F52-4B23-86F1-E4F7AA6FD2AB%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/0B3391C2-9F52-4B23-86F1-E4F7AA6FD2AB%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![mat1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mat1/32/1679_2.png) [@mat1](https://discuss.elastic.co/u/mat1)\
**Post date:** [April 2, 2014, 5:52am UTC](https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751/4 "2014-04-02T05:52:16Z")

</div>

Yes. I wanted to join documents via a script, to avoid multiple round trips  
to the client. Could be used to autoload parent docs from child docs for  
example. I don't want to start a new node and client on each script call,  
for obvious reasons. I suppose a native script plugin could do the trick  
but I was wondering if there was any other way to invoke a shared node  
client from a script which could be used accross multiple requests.

On Tuesday, April 1, 2014 9:46:17 PM UTC-7, David Pilato wrote:

> O\_o starting a Node from a script? First time I see that... Looks like a  
> hack to me! 🙂
> 
> That said what is the use case here? Are you trying to perform some JOIN  
> using scripts?
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 2 avr. 2014 à 05:46, mat taylor \<[mat.t...@gmail.com](mailto:mat.t...@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> Figured this out, however the code below seems to restart a new node on  
> each request, and is not cleaning up after itself.  
> Is there a better way to do this - for example by exposing a shared node  
> object to the mvel script plugin ?
> 
> PUT /test/user/1  
> { "name":"jane"  
> , "partner":2  
> }
> 
> PUT /test/user/2  
> { "name":"john"  
> , "partner":1  
> }
> 
> GET /test/user/\_search  
> { "\_source":true  
> , "script\_fields":  
> { "partner":  
> {  
> "script":"org.elasticsearch.node.NodeBuilder.nodeBuilder().node().client().prepareGet('test',  
> 'user',\_source.partner).execute().actionGet().getSource()"  
> }  
> }  
> }
> 
> On Tuesday, April 1, 2014 4:44:40 PM UTC-7, mat taylor wrote:
> 
> > Is it possible to query the database from a script field by instantiating  
> > a java client and issuing a get request?  
> > Are there any examples of this?
> > 
> > Thanks  
> > Mat
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/a0f57ffc-cecf-4bc8-8082-9f5737faea87%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a0f57ffc-cecf-4bc8-8082-9f5737faea87%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/a0f57ffc-cecf-4bc8-8082-9f5737faea87%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a0f57ffc-cecf-4bc8-8082-9f5737faea87%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/37072cd6-2c0a-4476-936a-23848c92c868%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/37072cd6-2c0a-4476-936a-23848c92c868%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [April 2, 2014, 8:17am UTC](https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751/5 "2014-04-02T08:17:52Z")

</div>

I wrote a denormalizer plugin where I use a node client from a field  
analyzer for a field type "deref". A node client is started as a singleton  
per node where the plugin is installed. It can ask other indexes/types for  
a doc by given ID for injecting additional terms from an array of terms of  
the referenced doc into the current tokenizer stream.

It is not well tested and early in development, but I can share the code if  
there is interest.

I would never start a client node from a a script, because of the enormous  
overhead, as you have experienced.

Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEYBU64spM4QvtRSQDVxjMK7-yRENWC%2BqFhr4mkTHN49w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEYBU64spM4QvtRSQDVxjMK7-yRENWC%2BqFhr4mkTHN49w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![mat1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mat1/32/1679_2.png) [@mat1](https://discuss.elastic.co/u/mat1)\
**Post date:** [April 2, 2014, 6:55pm UTC](https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751/6 "2014-04-02T18:55:23Z")

</div>

Yes that would be very interesting.  
I have also got a good workaround to my issue now by using the lookup  
script from [GitHub - imotov/elasticsearch-native-script-example: Example of Now Deprecated Native Script Plugin for Elasticsearch](https://github.com/imotov/elasticsearch-native-script-example)

On Wednesday, April 2, 2014 1:17:52 AM UTC-7, Jörg Prante wrote:

> I wrote a denormalizer plugin where I use a node client from a field  
> analyzer for a field type "deref". A node client is started as a singleton  
> per node where the plugin is installed. It can ask other indexes/types for  
> a doc by given ID for injecting additional terms from an array of terms of  
> the referenced doc into the current tokenizer stream.
> 
> It is not well tested and early in development, but I can share the code  
> if there is interest.
> 
> I would never start a client node from a a script, because of the enormous  
> overhead, as you have experienced.
> 
> Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/29da35bf-44e1-4fbf-85b6-2253f6943cbb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/29da35bf-44e1-4fbf-85b6-2253f6943cbb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:38am UTC](https://discuss.elastic.co/t/using-java-get-document-api-within-a-script-field/16751/7 "2017-07-06T01:38:41Z")

</div>


