# Native Script Field

**URL:** <https://discuss.elastic.co/t/native-script-field/5316>\
**Category:** Elasticsearch\
**Created:** [September 6, 2011, 4:41pm UTC](https://discuss.elastic.co/t/native-script-field/5316 "2011-09-06T16:41:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [September 6, 2011, 4:41pm UTC](https://discuss.elastic.co/t/native-script-field/5316/1 "2011-09-06T16:41:04Z")

</div>

Hi,

I've implemented script based sorting using native Java Classes but  
I'm new to this for script based fields. Can someone give some some  
pointers on how to do this.

- David.

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [September 7, 2011, 10:53am UTC](https://discuss.elastic.co/t/native-script-field/5316/2 "2011-09-07T10:53:43Z")

</div>

Or, to put it another way, am I restricted to defining my scripted  
field in MVEL or can I use Java to perform my functions on other  
fields, at runtime, to produce the scripted fields?

On Sep 6, 5:41 pm, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:

> Hi,
> 
> I've implemented script based sorting using native Java Classes but  
> I'm new to this for script based fields. Can someone give some some  
> pointers on how to do this.
> 
> - David.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [September 7, 2011, 3:51pm UTC](https://discuss.elastic.co/t/native-script-field/5316/3 "2011-09-07T15:51:33Z")

</div>

You can use Java to perform your functions. Other than that, the question is  
not really focused, so hard to answer...

On Wed, Sep 7, 2011 at 1:53 PM, davrob2 [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:

> Or, to put it another way, am I restricted to defining my scripted  
> field in MVEL or can I use Java to perform my functions on other  
> fields, at runtime, to produce the scripted fields?
> 
> On Sep 6, 5:41 pm, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> 
> > Hi,
> > 
> > I've implemented script based sorting using native Java Classes but  
> > I'm new to this for script based fields. Can someone give some some  
> > pointers on how to do this.
> > 
> > - David.

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [September 8, 2011, 8:57am UTC](https://discuss.elastic.co/t/native-script-field/5316/4 "2011-09-08T08:57:07Z")

</div>

Thanks for the reply Shay.

I have been confused by what sort of ExecutableScript to return in my  
NativeScript factory but I think things are clearing up now. This is  
what my take on the process is now:

If I want to return a string, rather than a float for sorting, then I  
just need to extend AbstractSearchScript and overide the run()  
method. Ideally, I should use doc() to look up values most  
efficiently, or use fields() if Ihave defined an "addFields()" method  
in my SearchRequest, the source() method should only be used if I  
can't get the value I want any other way.

Additionally, is it possible to sort on scripted fields? If so, would  
I need a custom sort to do that, or would Elasticsearch detect the  
return type ( e.g. Date ) from a custom field and be able to sort it  
out-of-the box.

- David

On Sep 7, 4:51 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> You can use Java to perform your functions. Other than that, the question is  
> not really focused, so hard to answer...
> 
> On Wed, Sep 7, 2011 at 1:53 PM, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> 
> > Or, to put it another way, am I restricted to defining my scripted  
> > field in MVEL or can I use Java to perform my functions on other  
> > fields, at runtime, to produce the scripted fields?
> 
> > On Sep 6, 5:41 pm, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > 
> > > Hi,
> 
> > > I've implemented script based sorting using native Java Classes but  
> > > I'm new to this for script based fields. Can someone give some some  
> > > pointers on how to do this.
> 
> > > - David.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [September 8, 2011, 5:30pm UTC](https://discuss.elastic.co/t/native-script-field/5316/5 "2011-09-08T17:30:47Z")

</div>

On Thu, Sep 8, 2011 at 11:57 AM, davrob2 [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:

> Thanks for the reply Shay.
> 
> I have been confused by what sort of ExecutableScript to return in my  
> NativeScript factory but I think things are clearing up now. This is  
> what my take on the process is now:
> 
> If I want to return a string, rather than a float for sorting, then I  
> just need to extend AbstractSearchScript and overide the run()  
> method. Ideally, I should use doc() to look up values most  
> efficiently, or use fields() if Ihave defined an "addFields()" method  
> in my SearchRequest, the source() method should only be used if I  
> can't get the value I want any other way.

Accessing fields is expensive as well, it simply fetches it from stored  
fields. Note, even if you defined fields to be "fetched" (which might be  
stored), they are only extracted for the "size" docs you ask for, not across  
all hits matching the query (which your script probably do, depends where  
you use it).

> Additionally, is it possible to sort on scripted fields? If so, would  
> I need a custom sort to do that, or would Elasticsearch detect the  
> return type ( e.g. Date ) from a custom field and be able to sort it  
> out-of-the box.

For custom script sorting, you define what type base to do sorting, it can  
be either numeric or a string.

> - David
> 
> On Sep 7, 4:51 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > You can use Java to perform your functions. Other than that, the question  
> > is  
> > not really focused, so hard to answer...
> > 
> > On Wed, Sep 7, 2011 at 1:53 PM, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > 
> > > Or, to put it another way, am I restricted to defining my scripted  
> > > field in MVEL or can I use Java to perform my functions on other  
> > > fields, at runtime, to produce the scripted fields?
> > 
> > > On Sep 6, 5:41 pm, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > > 
> > > > Hi,
> > 
> > > > I've implemented script based sorting using native Java Classes but  
> > > > I'm new to this for script based fields. Can someone give some some  
> > > > pointers on how to do this.
> > 
> > > > - David.

---

<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:55am UTC](https://discuss.elastic.co/t/native-script-field/5316/6 "2017-07-06T03:55:01Z")

</div>


