# Accessing document fields from preloaded script

**URL:** <https://discuss.elastic.co/t/accessing-document-fields-from-preloaded-script/9959>\
**Category:** Elasticsearch\
**Created:** [December 5, 2012, 7:59pm UTC](https://discuss.elastic.co/t/accessing-document-fields-from-preloaded-script/9959 "2012-12-05T19:59:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![movelikethewind](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/movelikethewind/32/2591_2.png) [@movelikethewind](https://discuss.elastic.co/u/movelikethewind)\
**Post date:** [December 5, 2012, 7:59pm UTC](https://discuss.elastic.co/t/accessing-document-fields-from-preloaded-script/9959/1 "2012-12-05T19:59:38Z")

</div>

I'm trying to use a preloaded javascript file stored in  
/config/scripts/test.js

[http://www.elasticsearch](http://www.elasticsearch)  
.org/guide/reference/query-dsl/custom-score-query.html

I'm using the Tire gem for Rails integration.

query do  
custom\_score :params =\> {😛 =\> 5}, :script =\> "test", :lang =\> "js" do  
string '\*'  
end  
end

test.js:  
java.lang.System.out.println(doc['components'].value)  
5

I want to access one of my document fields (type is text), but the output  
of doc['components'].value is just 0. What might be the problem here?

Thanks!

--

---

<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:** [December 7, 2012, 3:58am UTC](https://discuss.elastic.co/t/accessing-document-fields-from-preloaded-script/9959/2 "2012-12-07T03:58:20Z")

</div>

I don't see anything particularly wrong with your sample. When  
doc['some\_field'].value returns 0, it might indicate that 'some\_field' is  
mapped as integer and is missing for the given record. But since you have  
stated that the field "components" is indexed as text, I am not really sure  
what it might be. Are you restarting elasticsearch every time you update  
the script? If yes, could you provide a short repro of this issue?

On Wednesday, December 5, 2012 2:59:38 PM UTC-5, George Xing wrote:

> I'm trying to use a preloaded javascript file stored in  
> /config/scripts/test.js
> 
> [http://www.elasticsearch](http://www.elasticsearch)  
> .org/guide/reference/query-dsl/custom-score-query.html
> 
> I'm using the Tire gem for Rails integration.
> 
> query do  
> custom\_score :params =\> {😛 =\> 5}, :script =\> "test", :lang =\> "js" do  
> string '\*'  
> end  
> end
> 
> test.js:  
> java.lang.System.out.println(doc['components'].value)  
> 5
> 
> I want to access one of my document fields (type is text), but the output  
> of doc['components'].value is just 0. What might be the problem here?
> 
> Thanks!

--

---

<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:00am UTC](https://discuss.elastic.co/t/accessing-document-fields-from-preloaded-script/9959/3 "2017-07-06T03:00:57Z")

</div>

Closed on July 6, 2017, 3:00am UTC


