# Groovy & Multiple term filters problem

**URL:** <https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451>\
**Category:** Elasticsearch\
**Created:** [January 20, 2012, 2:02pm UTC](https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451 "2012-01-20T14:02:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Johnson](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@Mark\_Johnson](https://discuss.elastic.co/u/Mark_Johnson)\
**Post date:** [January 20, 2012, 2:02pm UTC](https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451/1 "2012-01-20T14:02:34Z")

</div>

**Im using Groovy.** With the following query below im getting the error (bold). can anyone tell me where I am going wrong with my query,  
cheers

**Class  
java.lang.NullPointerException  
Message  
Cannot invoke method getAt() on null object**

query {  
filtered {  
query {   
query\_string (query: query\_terms)  
}  
filter {  
**and {  
filters [{  
term {  
subject = params.subject  
}  
}  
{  
term {  
provid = params.provider  
}  
}]  
\_name = "is\_this\_needed"  
}  
}**  
}  
}  
}  
facets {  
subject {  
terms {  
field = 'subject'  
}  
}  
provider {  
terms {  
field = 'provid'  
}  
}  
}

---

<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:** [January 20, 2012, 3:04pm UTC](https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451/2 "2012-01-20T15:04:27Z")

</div>

Maybe its the part that transforms the closure to a json? Where is the  
failure coming from? If so, I just took that code from grails, it seems to  
have its quirks, not sure why since I am not a groovy expert...

On Fri, Jan 20, 2012 at 4:02 PM, Mark Johnson [Mark@k-int.com](mailto:Mark@k-int.com) wrote:

> _Im using Groovy._ With the following query below im getting the error  
> (bold). can anyone tell me where I am going wrong with my query,  
> cheers
> 
> _Class  
> java.lang.NullPointerException  
> Message  
> Cannot invoke method getAt() on null object_
> 
> query {  
> filtered {  
> query {  
> query\_string (query: query\_terms)  
> }  
> filter {  
> and {  
> filters [{  
> term {  
> subject =  
> params.subject  
> }  
> }  
> {  
> term {  
> provid =  
> params.provider  
> }  
> }]  
> \_name = "is\_this\_needed"  
> }  
> }  
> }  
> }  
> }  
> facets {  
> subject {  
> terms {  
> field = 'subject'  
> }  
> }  
> provider {  
> terms {  
> field = 'provid'  
> }  
> }  
> }
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Groovy-Multiple-term-filters-problem-tp3675518p3675518.html](http://elasticsearch-users.115913.n3.nabble.com/Groovy-Multiple-term-filters-problem-tp3675518p3675518.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Mark\_Johnson](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@Mark\_Johnson](https://discuss.elastic.co/u/Mark_Johnson)\
**Post date:** [January 20, 2012, 3:24pm UTC](https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451/3 "2012-01-20T15:24:51Z")

</div>

The problem comes at the line:

**filters [{**

It doesnt seem to like the array use

would it be useful for me to print the Trace here?

---

<div class="post-metadata">

**Author:** ![egaumer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egaumer/32/2365_2.png) [@egaumer](https://discuss.elastic.co/u/egaumer)\
**Post date:** [January 20, 2012, 9:06pm UTC](https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451/4 "2012-01-20T21:06:23Z")

</div>

Keep in mind that what you're writing is groovy, not actual JSON.  
JSONBuilder (grails) is there to provide a DSL. I typically just write pure  
groovy code because of all the JSONBuilder quirks. It also makes modifying  
query objects easier.

Try converting your filter object to a groovy map. This is essentially what  
JSONBuilder would produce anyway (a map from a closure).

filters [  
[term:[subject:params.subject]],  
[term:[provid:params.provider]]  
]

You can rewrite the entire query as a map.

---

<div class="post-metadata">

**Author:** ![Mark\_Johnson](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@Mark\_Johnson](https://discuss.elastic.co/u/Mark_Johnson)\
**Post date:** [January 21, 2012, 4:41pm UTC](https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451/5 "2012-01-21T16:41:13Z")

</div>

Thanks Eric I'll give this a go on Monday

---

<div class="post-metadata">

**Author:** ![ianibbo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ianibbo/32/3012_2.png) [@ianibbo](https://discuss.elastic.co/u/ianibbo)\
**Post date:** [January 22, 2012, 1:24am UTC](https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451/6 "2012-01-22T01:24:38Z")

</div>

Heya all.. I've been looking at this for mark over the weekend... The stack-trace is attached below. It seems to cleanly describe the method calls assigned via the metaClass. Reading the code for GCContextBuilder there are some references to an element() call that I can't quite put into context... If anyone can point me in the right direction I'm more than happy to dig deeper...

Eric, you suggest compiling a pure json object, but I can't see a way to pass a pure groovy map through GClient... Are you suggesting doing away with GClient altogether and then just using a http client to post a manually constructed json object at the server?

Cheers all, any ideas most welcome!

Ian.

Cannot invoke method getAt() on null object. Stacktrace follows:  
java.lang.NullPointerException: Cannot invoke method getAt() on null object  
at com.k\_int.xcri.SearchController$\_index\_closure1\_closure3\_closure4\_closure6\_closure8\_closure9$$ENMgvvLk.doCall(SearchController.groovy:54)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.handleClosureNode(GXContentBuilder.groovy:156)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.invokeMethod(GXContentBuilder.groovy:118)  
at com.k\_int.xcri.SearchController$\_index\_closure1\_closure3\_closure4\_closure6\_closure8$$ENMgvvLk.doCall(SearchController.groovy:52)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.handleClosureNode(GXContentBuilder.groovy:156)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.invokeMethod(GXContentBuilder.groovy:118)  
at com.k\_int.xcri.SearchController$\_index\_closure1\_closure3\_closure4\_closure6$$ENMgvvLk.doCall(SearchController.groovy:51)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.handleClosureNode(GXContentBuilder.groovy:156)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.invokeMethod(GXContentBuilder.groovy:118)  
at com.k\_int.xcri.SearchController$\_index\_closure1\_closure3\_closure4$$ENMgvvLk.doCall(SearchController.groovy:47)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.handleClosureNode(GXContentBuilder.groovy:156)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.invokeMethod(GXContentBuilder.groovy:118)  
at com.k\_int.xcri.SearchController$\_index\_closure1\_closure3$$ENMgvvLk.doCall(SearchController.groovy:45)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.buildRoot(GXContentBuilder.groovy:73)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.build(GXContentBuilder.groovy:47)  
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.buildAsBytes(GXContentBuilder.groovy:63)  
at org.elasticsearch.groovy.client.GClient$\_\_clinit\_\_closure15.doCall(GClient.groovy:107)  
at com.k\_int.xcri.SearchController$\_index\_closure1$$ENMgvvLk.doCall(SearchController.groovy:42)  
at org.elasticsearch.groovy.client.GClient.search(GClient.groovy:308)  
at com.k\_int.xcri.SearchController$$ENMgvvLk.index(SearchController.groovy:82)  
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
at java.lang.Thread.run(Thread.java:662)

---

<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:41am UTC](https://discuss.elastic.co/t/groovy-multiple-term-filters-problem/6451/7 "2017-07-06T03:41:53Z")

</div>


