# Need some input/advice on scripting approach

**URL:** <https://discuss.elastic.co/t/need-some-input-advice-on-scripting-approach/16809>\
**Category:** Elasticsearch\
**Created:** [April 3, 2014, 8:07pm UTC](https://discuss.elastic.co/t/need-some-input-advice-on-scripting-approach/16809 "2014-04-03T20:07:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_Lappen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/greg_lappen/32/1665_2.png) [@Greg\_Lappen](https://discuss.elastic.co/u/Greg_Lappen)\
**Post date:** [April 3, 2014, 8:07pm UTC](https://discuss.elastic.co/t/need-some-input-advice-on-scripting-approach/16809/1 "2014-04-03T20:07:24Z")

</div>

Hello - I am an elasticsearch newbie and after doing a lot of reading, I  
think I need to use scripts to accomplish my objectives. Basically, we are  
indexing and searching documents already, and now we need to do a  
non-trivial financial calculation based on user-provided inputs. The  
output of the calculation depends on the users inputs, and he output of the  
calculation will be a field in the results (a script\_field ?) as well as  
used to filter/query the results (a filter script?).

#1) I'd like to have the same script/function be used for the script\_field  
and the filter script to avoid code duplication - is this possible?

#2 ) Do I need to install my script inside elasticsearch (rather than pass  
it in my query) in order to achieve this?

It seems like that would be better for performance as well, which leads me  
to my next question:

#3) Should I write the script in Java for performance reasons? I have no  
experience with MVEL, and I know Java already - is Java faster than MVEL  
for number crunching?

Thanks in advance for any advice you might have!

Greg

--  
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/ab8e3d97-4d89-4f2e-bcbe-7fca5ee66a7f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ab8e3d97-4d89-4f2e-bcbe-7fca5ee66a7f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 3, 2014, 8:25pm UTC](https://discuss.elastic.co/t/need-some-input-advice-on-scripting-approach/16809/2 "2014-04-03T20:25:30Z")

</div>

On Thu, Apr 3, 2014 at 4:07 PM, Greg Lappen [greg@lapcominc.com](mailto:greg@lapcominc.com) wrote:

> Hello - I am an elasticsearch newbie and after doing a lot of reading, I  
> think I need to use scripts to accomplish my objectives. Basically, we are  
> indexing and searching documents already, and now we need to do a  
> non-trivial financial calculation based on user-provided inputs. The  
> output of the calculation depends on the users inputs, and he output of the  
> calculation will be a field in the results (a script\_field ?) as well as  
> used to filter/query the results (a filter script?).
> 
> #1) I'd like to have the same script/function be used for the script\_field  
> and the filter script to avoid code duplication - is this possible?

I don't know.

> #2 ) Do I need to install my script inside elasticsearch (rather than pass  
> it in my query) in order to achieve this?

Not if you write it in MVEL or some other scripting language for which you  
have installed a plugin.

> It seems like that would be better for performance as well, which leads me  
> to my next question:
> 
> #3) Should I write the script in Java for performance reasons? I have no  
> experience with MVEL, and I know Java already - is Java faster than MVEL  
> for number crunching?

Java is faster but I'd advise trying it with MVEL first anyway. MVEL is  
like Java except I never trust the order of operations so that shouldn't be  
a problem. Its much faster to prototype it and you may find that the  
ability to change it without restarting Elasticsearch is worth the speed  
difference.

One things to keep in mind: if you really really want it to be fast you  
should try to lean on Lucene as much as possible. Script filters have to  
examine every document but term and range filters use the lucene index to  
chop out huge swaths of documents at a time. They can also be cached  
really easily making them even quicker. So what I'm saying is if you can  
limit the documents you actually run the script filter against it'll be  
quicker.

> Thanks in advance for any advice you might have!
> 
> Greg
> 
> --  
> 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/ab8e3d97-4d89-4f2e-bcbe-7fca5ee66a7f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ab8e3d97-4d89-4f2e-bcbe-7fca5ee66a7f%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/ab8e3d97-4d89-4f2e-bcbe-7fca5ee66a7f%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/ab8e3d97-4d89-4f2e-bcbe-7fca5ee66a7f%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/CAPmjWd3sBPimWk2NED0gyC-NZ%2BKL%3DSPu4Y\_b%3Dm\_ORwWo1T7fFw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3sBPimWk2NED0gyC-NZ%2BKL%3DSPu4Y_b%3Dm_ORwWo1T7fFw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [April 3, 2014, 9:19pm UTC](https://discuss.elastic.co/t/need-some-input-advice-on-scripting-approach/16809/3 "2014-04-03T21:19:06Z")

</div>

For #1, if you define a native script in Java, yes the code can be shared  
between script\_fields and script filter. It's also possible to define an  
MVEL script in a file that you put inside the config/scripts folder but it  
might be hassle to maintain if you have lots of nodes.

Other questions, Nikolas is right.

--  
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/1d8df8cd-09dc-4d71-84ed-b1364e326bb0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1d8df8cd-09dc-4d71-84ed-b1364e326bb0%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/need-some-input-advice-on-scripting-approach/16809/4 "2017-07-06T01:38:26Z")

</div>


