# Script Filters

**URL:** <https://discuss.elastic.co/t/script-filters/7113>\
**Category:** Elasticsearch\
**Created:** [March 24, 2012, 10:08pm UTC](https://discuss.elastic.co/t/script-filters/7113 "2012-03-24T22:08:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![posejavier](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@posejavier](https://discuss.elastic.co/u/posejavier)\
**Post date:** [March 24, 2012, 10:08pm UTC](https://discuss.elastic.co/t/script-filters/7113/1 "2012-03-24T22:08:11Z")

</div>

Hi all,  
This is the second time I post this question. It is VERY URGENT. Could some one help me with it?  
I have a Script Filters defined in the couchdb river.  
In this script I need to refer to a field of the documents which has special  
characters on it, e.g. '-'  
So the field would be, for example: 'number-of-houses'

How can I refer to this field from the Script Filter ??

In a normal javascript in a view I would use doc['number-of-houses'], but in  
the Script Filters I don't know...  
I tried:

ctx.doc['number-of-houses']  
ctx.doc.'number-of-houses'  
ctx[doc]['number-of-houses']  
...

...but none of these options works.  
Do you know how should I refer to this field?

Many thanks in advance!!

---

<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:** [March 25, 2012, 12:46pm UTC](https://discuss.elastic.co/t/script-filters/7113/2 "2012-03-25T12:46:41Z")

</div>

Do you refer to a script defined in the river when you create it? Using  
ctx['doc']['number-of-houses'] shoudl work. How do you create teh river?  
Maybe you do it with curl and use ' to denote the body of the message?

On Sun, Mar 25, 2012 at 12:08 AM, posejavier [posejavier@hotmail.com](mailto:posejavier@hotmail.com) wrote:

> Hi all,  
> This is the second time I post this question. It is VERY URGENT. Could some  
> one help me with it?  
> I have a Script Filters defined in the couchdb river.  
> In this script I need to refer to a field of the documents which has  
> special  
> characters on it, e.g. '-'  
> So the field would be, for example: 'number-of-houses'
> 
> How can I refer to this field from the Script Filter ??
> 
> In a normal javascript in a view I would use doc['number-of-houses'], but  
> in  
> the Script Filters I don't know...  
> I tried:
> 
> ctx.doc['number-of-houses']  
> ctx.doc.'number-of-houses'  
> ctx[doc]['number-of-houses']  
> ...
> 
> ...but none of these options works.  
> Do you know how should I refer to this field?
> 
> Many thanks in advance!!
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Script-Filters-tp3854642p3854642.html](http://elasticsearch-users.115913.n3.nabble.com/Script-Filters-tp3854642p3854642.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![posejavier](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@posejavier](https://discuss.elastic.co/u/posejavier)\
**Post date:** [March 25, 2012, 10:32pm UTC](https://discuss.elastic.co/t/script-filters/7113/3 "2012-03-25T22:32:45Z")

</div>

Hi,  
many thanks for the reply (!!!)  
You are right, I am using curl to create the river and and use ' to denote the body of the message. Should I do it in another way?

I tried the ctx['doc']['number-of-houses'] in my curl code but still ES gives the same error:

The curl that I used is:

curl -XPUT 'localhost:9200/\_river/database1/\_meta' -d '{  
"type" : "couchdb",  
"couchdb" : {  
"host" : "localhost",  
"port" : 5984,  
"db" : "database1",  
"filter" : null,  
"script" : "if (ctx['doc']['number-of-houses']) ctx['doc']['number-of-houses'] = null"  
},  
"index" : {  
"index" : "database1",  
"type" : "database1",  
"bulk\_size" : "100",  
"bulk\_timeout" : "10ms"  
}  
}'

...but still ES gives the same error:

[2012-03-26 00:25:11,982][WARN][river.couchdb] [Rusty Collins] [cou  
chdb][database1] failed to script process {id=74a18d549947f264d0d8b553cf005d81,  
doc={\_rev=1-967a00dff5e02add41819138abb3284d, \_id=74a18d549947f264d0d8b553cf005d  
81}, seq=46, changes=[{rev=1-967a00dff5e02add41819138abb3284d}]}, ignoring  
org.mozilla.javascript.EcmaError: ReferenceError: "doc" is not defined. (Script1  
.js#1)  
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.jav  
a:3785)  
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.jav  
a:3763)  
at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java  
:3848)  
at org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1781)  
at org.mozilla.javascript.gen.Script1\_js\_1.\_c\_script\_0(Script1.js:1)  
at org.mozilla.javascript.gen.Script1\_js\_1.call(Script1.js)  
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:4  
26)  
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:317  
8)  
at org.mozilla.javascript.gen.Script1\_js\_1.call(Script1.js)  
at org.mozilla.javascript.gen.Script1\_js\_1.exec(Script1.js)  
at org.elasticsearch.script.javascript.JavaScriptScriptEngineService$Jav  
aScriptExecutableScript.run(JavaScriptScriptEngineService.java:186)  
at org.elasticsearch.river.couchdb.CouchdbRiver.processLine(CouchdbRiver  
.java:236)  
at org.elasticsearch.river.couchdb.CouchdbRiver.access$500(CouchdbRiver.  
java:64)  
at org.elasticsearch.river.couchdb.CouchdbRiver$Indexer.run(CouchdbRiver  
.java:329)  
at java.lang.Thread.run(Unknown Source)  
[2012-03-26 00:25:12,043][INFO][cluster.metadata] [Rusty Collins] [\_ri  
ver] update\_mapping [database1] (dynamic)  
[2012-03-26 00:27:07,258][INFO][cluster.metadata] [Rusty Collins] [dat  
abase1] deleting index  
[2012-03-26 00:27:14,296][INFO][cluster.metadata] [Rusty Collins] [\_ri  
ver] deleting index  
[2012-03-26 00:27:14,298][INFO][river.couchdb] [Rusty Collins] [cou  
chdb][database1] closing couchdb stream river

---

<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:** [March 27, 2012, 1:01pm UTC](https://discuss.elastic.co/t/script-filters/7113/4 "2012-03-27T13:01:28Z")

</div>

check the -d ', it means that the string starts, and then you use ' in the  
body...

On Mon, Mar 26, 2012 at 12:32 AM, posejavier [posejavier@hotmail.com](mailto:posejavier@hotmail.com) wrote:

> Hi,  
> many thanks for the reply (!!!)  
> You are right, I am using curl to create the river and and use ' to denote  
> the body of the message. Should I do it in another way?
> 
> I tried the ctx['doc']['number-of-houses'] in my curl code but still ES  
> gives the same error:
> 
> The curl that I used is:
> 
> curl -XPUT 'localhost:9200/\_river/database1/\_meta' -d '{  
> "type" : "couchdb",  
> "couchdb" : {  
> "host" : "localhost",  
> "port" : 5984,  
> "db" : "database1",  
> "filter" : null,  
> "script" : "if (ctx['doc']['number-of-houses'])  
> ctx['doc']['number-of-houses'] = null"  
> },  
> "index" : {  
> "index" : "database1",  
> "type" : "database1",  
> "bulk\_size" : "100",  
> "bulk\_timeout" : "10ms"  
> }  
> }'
> 
> ...but still ES gives the same error:
> 
> [2012-03-26 00:25:11,982][WARN][river.couchdb] [Rusty Collins]  
> [cou  
> chdb][database1] failed to script process  
> {id=74a18d549947f264d0d8b553cf005d81,  
> doc={\_rev=1-967a00dff5e02add41819138abb3284d,  
> \_id=74a18d549947f264d0d8b553cf005d  
> 81}, seq=46, changes=[{rev=1-967a00dff5e02add41819138abb3284d}]}, ignoring  
> org.mozilla.javascript.EcmaError: ReferenceError: "doc" is not defined.  
> (Script1  
> .js#1)  
> at  
> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.jav  
> a:3785)  
> at  
> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.jav  
> a:3763)  
> at  
> org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java  
> :3848)  
> at  
> org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1781)  
> at org.mozilla.javascript.gen.Script1\_js\_1.\_c\_script\_0(Script1.js:1)  
> at org.mozilla.javascript.gen.Script1\_js\_1.call(Script1.js)  
> at  
> org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:4  
> 26)  
> at  
> org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:317  
> 8)  
> at org.mozilla.javascript.gen.Script1\_js\_1.call(Script1.js)  
> at org.mozilla.javascript.gen.Script1\_js\_1.exec(Script1.js)  
> at  
> org.elasticsearch.script.javascript.JavaScriptScriptEngineService$Jav  
> aScriptExecutableScript.run(JavaScriptScriptEngineService.java:186)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver.processLine(CouchdbRiver  
> .java:236)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver.access$500(CouchdbRiver.  
> java:64)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver$Indexer.run(CouchdbRiver  
> .java:329)  
> at java.lang.Thread.run(Unknown Source)  
> [2012-03-26 00:25:12,043][INFO][cluster.metadata] [Rusty Collins]  
> [\_ri  
> ver] update\_mapping [database1] (dynamic)  
> [2012-03-26 00:27:07,258][INFO][cluster.metadata] [Rusty Collins]  
> [dat  
> abase1] deleting index  
> [2012-03-26 00:27:14,296][INFO][cluster.metadata] [Rusty Collins]  
> [\_ri  
> ver] deleting index  
> [2012-03-26 00:27:14,298][INFO][river.couchdb] [Rusty Collins]  
> [cou  
> chdb][database1] closing couchdb stream river
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Script-Filters-tp3854642p3856836.html](http://elasticsearch-users.115913.n3.nabble.com/Script-Filters-tp3854642p3856836.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![posejavier](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@posejavier](https://discuss.elastic.co/u/posejavier)\
**Post date:** [March 30, 2012, 11:12am UTC](https://discuss.elastic.co/t/script-filters/7113/5 "2012-03-30T11:12:52Z")

</div>

Many thanks !!!  
It worked perfect 🙂

---

<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:34am UTC](https://discuss.elastic.co/t/script-filters/7113/6 "2017-07-06T03:34:08Z")

</div>

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


