# Native script in ES

**URL:** <https://discuss.elastic.co/t/native-script-in-es/18562>\
**Category:** Elasticsearch\
**Created:** [July 9, 2014, 8:41pm UTC](https://discuss.elastic.co/t/native-script-in-es/18562 "2014-07-09T20:41:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian\_M](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@Adrian\_M](https://discuss.elastic.co/u/Adrian_M)\
**Post date:** [July 9, 2014, 8:41pm UTC](https://discuss.elastic.co/t/native-script-in-es/18562/1 "2014-07-09T20:41:57Z")

</div>

Hi,  
I'm curious how scripts work in ES.  
I've created a simple script which checks if a field in a record is equal  
to a certain value then return 1 otherwise return 0. I used  
AbstractDoubleSearchScript as the super class for this script.  
My records contains some a data field that is an int and a date field  
(long) (some other ones too that I don't care too much yet)

What I've noticed is that when I execute the script, each record in the ES  
db goes through the script and is returned either w a score of 0 or 1.  
What I would like to know is if it is possible to do a range query, say  
given 2 date params, and select only the records that fall between those  
dates to run through the script.

I know how to do this with the API but not in script format. For example  
this non script code works:  
SearchRequestBuilder s = client.prepareSearch("posts")

.setQuery(QueryBuilders.queryString(query).defaultField("post.body"))  
.setSize(count)  
.setFrom(start)

.setFilter(FilterBuilders.rangeFilter("post.creationDate").from(interval.getStart.toString()).to(interval.getEnd.toString()))  
.setFilter(FilterBuilders.existsFilter("userId"));  
s.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH);

But when extending AbstractDoubleSearchScript I have access only to a few  
things like doc(), source(), fields(), lookup() and I'm not sure how to  
filter records.  
Also I can't find documentation on what these are ie: what is source()  
other than a Map.

Any help on filtering is greatly appreciated!  
Thanks

--  
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/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%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:** [July 9, 2014, 11:02pm UTC](https://discuss.elastic.co/t/native-script-in-es/18562/2 "2014-07-09T23:02:28Z")

</div>

Scripts are not an alternative to write filters or queries, they are part  
of the filter or query computation. With scripts, you can access document  
fields and perform evaluations depending on one or more field values, for  
example, for custom scoring. Also, index term statistics can be accessed  
from scripts. Script results are passed back to the query processing.

Jörg

On Wed, Jul 9, 2014 at 10:41 PM, Adrian [eamocanu@gmail.com](mailto:eamocanu@gmail.com) wrote:

> Hi,  
> I'm curious how scripts work in ES.  
> I've created a simple script which checks if a field in a record is equal  
> to a certain value then return 1 otherwise return 0. I used  
> AbstractDoubleSearchScript as the super class for this script.  
> My records contains some a data field that is an int and a date field  
> (long) (some other ones too that I don't care too much yet)
> 
> What I've noticed is that when I execute the script, each record in the ES  
> db goes through the script and is returned either w a score of 0 or 1.  
> What I would like to know is if it is possible to do a range query, say  
> given 2 date params, and select only the records that fall between those  
> dates to run through the script.
> 
> I know how to do this with the API but not in script format. For example  
> this non script code works:  
> SearchRequestBuilder s = client.prepareSearch("posts")
> 
> .setQuery(QueryBuilders.queryString(query).defaultField("post.body"))  
> .setSize(count)  
> .setFrom(start)
> 
> .setFilter(FilterBuilders.rangeFilter("post.creationDate").from(interval.getStart.toString()).to(interval.getEnd.toString()))  
> .setFilter(FilterBuilders.existsFilter("userId"));  
> s.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH);
> 
> But when extending AbstractDoubleSearchScript I have access only to a few  
> things like doc(), source(), fields(), lookup() and I'm not sure how to  
> filter records.  
> Also I can't find documentation on what these are ie: what is source()  
> other than a Map.
> 
> Any help on filtering is greatly appreciated!  
> Thanks
> 
> --  
> 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/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%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/CAKdsXoHAVe3\_M5%2BuWhoZW2fxwmZKsu%3DM3ELij-Q8Ef1X6wmXYA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoHAVe3_M5%2BuWhoZW2fxwmZKsu%3DM3ELij-Q8Ef1X6wmXYA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Adrian\_M](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@Adrian\_M](https://discuss.elastic.co/u/Adrian_M)\
**Post date:** [July 10, 2014, 7:14pm UTC](https://discuss.elastic.co/t/native-script-in-es/18562/3 "2014-07-10T19:14:13Z")

</div>

Ok, thanks.

Though I've noticed that I can inject Node in the Factory method to get the  
client in the script as shown here:  
[https://github.com/imotov/elasticsearch-native-script-example/blob/master/src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java](https://github.com/imotov/elasticsearch-native-script-example/blob/master/src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java).

Couldn't I in this case perform a query through the client?

On Wednesday, 9 July 2014 19:03:49 UTC-4, Jörg Prante wrote:

> Scripts are not an alternative to write filters or queries, they are part  
> of the filter or query computation. With scripts, you can access document  
> fields and perform evaluations depending on one or more field values, for  
> example, for custom scoring. Also, index term statistics can be accessed  
> from scripts. Script results are passed back to the query processing.
> 
> Jörg
> 
> On Wed, Jul 9, 2014 at 10:41 PM, Adrian \<[eamo...@gmail.com](mailto:eamo...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Hi,  
> > I'm curious how scripts work in ES.  
> > I've created a simple script which checks if a field in a record is equal  
> > to a certain value then return 1 otherwise return 0. I used  
> > AbstractDoubleSearchScript as the super class for this script.  
> > My records contains some a data field that is an int and a date field  
> > (long) (some other ones too that I don't care too much yet)
> > 
> > What I've noticed is that when I execute the script, each record in the  
> > ES db goes through the script and is returned either w a score of 0 or 1.  
> > What I would like to know is if it is possible to do a range query, say  
> > given 2 date params, and select only the records that fall between those  
> > dates to run through the script.
> > 
> > I know how to do this with the API but not in script format. For example  
> > this non script code works:  
> > SearchRequestBuilder s = client.prepareSearch("posts")
> > 
> > .setQuery(QueryBuilders.queryString(query).defaultField("post.body"))  
> > .setSize(count)  
> > .setFrom(start)
> > 
> > .setFilter(FilterBuilders.rangeFilter("post.creationDate").from(interval.getStart.toString()).to(interval.getEnd.toString()))  
> > .setFilter(FilterBuilders.existsFilter("userId"));  
> > s.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH);
> > 
> > But when extending AbstractDoubleSearchScript I have access only to a few  
> > things like doc(), source(), fields(), lookup() and I'm not sure how to  
> > filter records.  
> > Also I can't find documentation on what these are ie: what is source()  
> > other than a Map.
> > 
> > Any help on filtering is greatly appreciated!  
> > Thanks
> > 
> > --  
> > 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/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%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/5b353881-4a58-48af-980b-7a0e5f2ae2cf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%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:** [July 10, 2014, 7:27pm UTC](https://discuss.elastic.co/t/native-script-in-es/18562/4 "2014-07-10T19:27:21Z")

</div>

Yes, this is a lookup from one document of another document using a get  
request. Note the result of the lookup is a single document source, whereas  
a search request provides a search response with a document source list,  
which is vastly oversized for use from a script, and takes even longer to  
complete. Note also the comment "This is not very efficient" ... searches  
would be even more harmful at that place in a script code. I do not  
recommend it. Instead, use search requests from a client.

Jörg

On Thu, Jul 10, 2014 at 9:14 PM, Adrian [eamocanu@gmail.com](mailto:eamocanu@gmail.com) wrote:

> Ok, thanks.
> 
> Though I've noticed that I can inject Node in the Factory method to get  
> the client in the script as shown here:  
> [https://github.com/imotov/elasticsearch-native-script-example/blob/master/src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java](https://github.com/imotov/elasticsearch-native-script-example/blob/master/src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java)  
> .
> 
> Couldn't I in this case perform a query through the client?
> 
> On Wednesday, 9 July 2014 19:03:49 UTC-4, Jörg Prante wrote:
> 
> > Scripts are not an alternative to write filters or queries, they are part  
> > of the filter or query computation. With scripts, you can access document  
> > fields and perform evaluations depending on one or more field values, for  
> > example, for custom scoring. Also, index term statistics can be accessed  
> > from scripts. Script results are passed back to the query processing.
> > 
> > Jörg
> > 
> > On Wed, Jul 9, 2014 at 10:41 PM, Adrian [eamo...@gmail.com](mailto:eamo...@gmail.com) wrote:
> > 
> > > Hi,  
> > > I'm curious how scripts work in ES.  
> > > I've created a simple script which checks if a field in a record is  
> > > equal to a certain value then return 1 otherwise return 0. I used  
> > > AbstractDoubleSearchScript as the super class for this script.  
> > > My records contains some a data field that is an int and a date field  
> > > (long) (some other ones too that I don't care too much yet)
> > > 
> > > What I've noticed is that when I execute the script, each record in the  
> > > ES db goes through the script and is returned either w a score of 0 or 1.  
> > > What I would like to know is if it is possible to do a range query, say  
> > > given 2 date params, and select only the records that fall between those  
> > > dates to run through the script.
> > > 
> > > I know how to do this with the API but not in script format. For example  
> > > this non script code works:  
> > > SearchRequestBuilder s = client.prepareSearch("posts")  
> > > .setQuery(QueryBuilders.queryString(query).  
> > > defaultField("post.body"))  
> > > .setSize(count)  
> > > .setFrom(start)  
> > > .setFilter(FilterBuilders.rangeFilter("post.  
> > > creationDate").from(interval.getStart.toString()).to(  
> > > interval.getEnd.toString()))  
> > > .setFilter(FilterBuilders.existsFilter("userId"));  
> > > s.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH);
> > > 
> > > But when extending AbstractDoubleSearchScript I have access only to a  
> > > few things like doc(), source(), fields(), lookup() and I'm not sure how to  
> > > filter records.  
> > > Also I can't find documentation on what these are ie: what is source()  
> > > other than a Map.
> > > 
> > > Any help on filtering is greatly appreciated!  
> > > Thanks
> > > 
> > > --  
> > > 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).
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%  
> > > [40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%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/5b353881-4a58-48af-980b-7a0e5f2ae2cf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%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/CAKdsXoGFFQNdRuj6PD5wL0g20pO4XzsPu5SLRJ753SELn7Rn7w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGFFQNdRuj6PD5wL0g20pO4XzsPu5SLRJ753SELn7Rn7w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Adrian\_M](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@Adrian\_M](https://discuss.elastic.co/u/Adrian_M)\
**Post date:** [July 10, 2014, 7:38pm UTC](https://discuss.elastic.co/t/native-script-in-es/18562/5 "2014-07-10T19:38:59Z")

</div>

I understand what you mean regarding efficiency/speed.  
My reason for doing things this way was so that I could compare 2 time  
series, ones coming from 1 query and another coming from another query.

On Thursday, 10 July 2014 15:27:28 UTC-4, Jörg Prante wrote:

> Yes, this is a lookup from one document of another document using a get  
> request. Note the result of the lookup is a single document source, whereas  
> a search request provides a search response with a document source list,  
> which is vastly oversized for use from a script, and takes even longer to  
> complete. Note also the comment "This is not very efficient" ... searches  
> would be even more harmful at that place in a script code. I do not  
> recommend it. Instead, use search requests from a client.
> 
> Jörg
> 
> On Thu, Jul 10, 2014 at 9:14 PM, Adrian \<[eamo...@gmail.com](mailto:eamo...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Ok, thanks.
> > 
> > Though I've noticed that I can inject Node in the Factory method to get  
> > the client in the script as shown here:  
> > [https://github.com/imotov/elasticsearch-native-script-example/blob/master/src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java](https://github.com/imotov/elasticsearch-native-script-example/blob/master/src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java)  
> > .
> > 
> > Couldn't I in this case perform a query through the client?
> > 
> > On Wednesday, 9 July 2014 19:03:49 UTC-4, Jörg Prante wrote:
> > 
> > > Scripts are not an alternative to write filters or queries, they are  
> > > part of the filter or query computation. With scripts, you can access  
> > > document fields and perform evaluations depending on one or more field  
> > > values, for example, for custom scoring. Also, index term statistics can be  
> > > accessed from scripts. Script results are passed back to the query  
> > > processing.
> > > 
> > > Jörg
> > > 
> > > On Wed, Jul 9, 2014 at 10:41 PM, Adrian [eamo...@gmail.com](mailto:eamo...@gmail.com) wrote:
> > > 
> > > > Hi,  
> > > > I'm curious how scripts work in ES.  
> > > > I've created a simple script which checks if a field in a record is  
> > > > equal to a certain value then return 1 otherwise return 0. I used  
> > > > AbstractDoubleSearchScript as the super class for this script.  
> > > > My records contains some a data field that is an int and a date field  
> > > > (long) (some other ones too that I don't care too much yet)
> > > > 
> > > > What I've noticed is that when I execute the script, each record in the  
> > > > ES db goes through the script and is returned either w a score of 0 or 1.  
> > > > What I would like to know is if it is possible to do a range query, say  
> > > > given 2 date params, and select only the records that fall between those  
> > > > dates to run through the script.
> > > > 
> > > > I know how to do this with the API but not in script format. For  
> > > > example this non script code works:  
> > > > SearchRequestBuilder s = client.prepareSearch("posts")  
> > > > .setQuery(QueryBuilders.queryString(query).  
> > > > defaultField("post.body"))  
> > > > .setSize(count)  
> > > > .setFrom(start)  
> > > > .setFilter(FilterBuilders.rangeFilter("post.  
> > > > creationDate").from(interval.getStart.toString()).to(  
> > > > interval.getEnd.toString()))  
> > > > .setFilter(FilterBuilders.existsFilter("userId"));  
> > > > s.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH);
> > > > 
> > > > But when extending AbstractDoubleSearchScript I have access only to a  
> > > > few things like doc(), source(), fields(), lookup() and I'm not sure how to  
> > > > filter records.  
> > > > Also I can't find documentation on what these are ie: what is source()  
> > > > other than a Map.
> > > > 
> > > > Any help on filtering is greatly appreciated!  
> > > > Thanks
> > > > 
> > > > --  
> > > > 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).
> > > > 
> > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%  
> > > > [40googlegroups.com](http://40googlegroups.com)  
> > > > [https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%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/96487e27-b538-48c1-9840-42e75ea021f3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/96487e27-b538-48c1-9840-42e75ea021f3%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:** [July 10, 2014, 10:11pm UTC](https://discuss.elastic.co/t/native-script-in-es/18562/6 "2014-07-10T22:11:35Z")

</div>

The fastest way I know is to execute the two queries, scan/scroll over  
result docs, save one result set, and compare values while iterating over  
the other.

Jörg

On Thu, Jul 10, 2014 at 9:38 PM, Adrian [eamocanu@gmail.com](mailto:eamocanu@gmail.com) wrote:

> I understand what you mean regarding efficiency/speed.  
> My reason for doing things this way was so that I could compare 2 time  
> series, ones coming from 1 query and another coming from another query.
> 
> On Thursday, 10 July 2014 15:27:28 UTC-4, Jörg Prante wrote:
> 
> > Yes, this is a lookup from one document of another document using a get  
> > request. Note the result of the lookup is a single document source, whereas  
> > a search request provides a search response with a document source list,  
> > which is vastly oversized for use from a script, and takes even longer to  
> > complete. Note also the comment "This is not very efficient" ... searches  
> > would be even more harmful at that place in a script code. I do not  
> > recommend it. Instead, use search requests from a client.
> > 
> > Jörg
> > 
> > On Thu, Jul 10, 2014 at 9:14 PM, Adrian [eamo...@gmail.com](mailto:eamo...@gmail.com) wrote:
> > 
> > > Ok, thanks.
> > > 
> > > Though I've noticed that I can inject Node in the Factory method to get  
> > > the client in the script as shown here: [imotov (Igor Motov) · GitHub](https://github.com/imotov/)  
> > > elasticsearch-native-script-example/blob/master/src/main/  
> > > java/org/elasticsearch/examples/nativescript/script/LookupScript.java.
> > > 
> > > Couldn't I in this case perform a query through the client?
> > > 
> > > On Wednesday, 9 July 2014 19:03:49 UTC-4, Jörg Prante wrote:
> > > 
> > > > Scripts are not an alternative to write filters or queries, they are  
> > > > part of the filter or query computation. With scripts, you can access  
> > > > document fields and perform evaluations depending on one or more field  
> > > > values, for example, for custom scoring. Also, index term statistics can be  
> > > > accessed from scripts. Script results are passed back to the query  
> > > > processing.
> > > > 
> > > > Jörg
> > > > 
> > > > On Wed, Jul 9, 2014 at 10:41 PM, Adrian [eamo...@gmail.com](mailto:eamo...@gmail.com) wrote:
> > > > 
> > > > > Hi,  
> > > > > I'm curious how scripts work in ES.  
> > > > > I've created a simple script which checks if a field in a record is  
> > > > > equal to a certain value then return 1 otherwise return 0. I used  
> > > > > AbstractDoubleSearchScript as the super class for this script.  
> > > > > My records contains some a data field that is an int and a date field  
> > > > > (long) (some other ones too that I don't care too much yet)
> > > > > 
> > > > > What I've noticed is that when I execute the script, each record in  
> > > > > the ES db goes through the script and is returned either w a score of 0 or  
> > > > > 1.  
> > > > > What I would like to know is if it is possible to do a range query,  
> > > > > say given 2 date params, and select only the records that fall between  
> > > > > those dates to run through the script.
> > > > > 
> > > > > I know how to do this with the API but not in script format. For  
> > > > > example this non script code works:  
> > > > > SearchRequestBuilder s = client.prepareSearch("posts")  
> > > > > .setQuery(QueryBuilders.queryString(query).  
> > > > > defaultField("post.body"))  
> > > > > .setSize(count)  
> > > > > .setFrom(start)  
> > > > > .setFilter(FilterBuilders.rangeFilter("post.  
> > > > > creationDate").from(interval.getStart.toString()).to(interval.  
> > > > > getEnd.toString()))  
> > > > > .setFilter(FilterBuilders.existsFilter("userId"));  
> > > > > s.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH);
> > > > > 
> > > > > But when extending AbstractDoubleSearchScript I have access only to a  
> > > > > few things like doc(), source(), fields(), lookup() and I'm not sure how to  
> > > > > filter records.  
> > > > > Also I can't find documentation on what these are ie: what is source()  
> > > > > other than a Map.
> > > > > 
> > > > > Any help on filtering is greatly appreciated!  
> > > > > Thanks
> > > > > 
> > > > > --  
> > > > > 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).
> > > > > 
> > > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > > msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%40goo  
> > > > > [glegroups.com](http://glegroups.com)  
> > > > > [https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1673cee1-468e-462b-85a4-c8e45fa74156%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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%  
> > > > [40googlegroups.com](http://40googlegroups.com)  
> > > > [https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5b353881-4a58-48af-980b-7a0e5f2ae2cf%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/96487e27-b538-48c1-9840-42e75ea021f3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/96487e27-b538-48c1-9840-42e75ea021f3%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/96487e27-b538-48c1-9840-42e75ea021f3%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/96487e27-b538-48c1-9840-42e75ea021f3%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/CAKdsXoEdphZhSAa6ypMoudokvq%3D2QrtnoOH\_CM-6PHRRVQvjQw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEdphZhSAa6ypMoudokvq%3D2QrtnoOH_CM-6PHRRVQvjQw%40mail.gmail.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:16am UTC](https://discuss.elastic.co/t/native-script-in-es/18562/7 "2017-07-06T01:16:40Z")

</div>


