elasticsearch/elasticsearch-lang-groovy — Issue #2: Examples for array structure in GXContextBuilder
Author: ianibo
Created: 2012-02-21 12:23:03+00:00
State: open

Heya All..

Apologies, more dumb questions. I'm really struggling to construct a usable example of the array type in the closure mapping for a search. I know the unit tests contain several simple examples of array mappings, but I just can't make this work with an actual search.

I'm trying to construct a dismax query through the groovy api that searches 2 fields (Although I have the same problem with sort ordering for multiple fields)... I've attached my closest attempts below, but if anyone can suggest where they are wrong i'd really appreciate it. Meanwhile I'll continue to try and understand GXContextBuilder a bit more.

Thanks in advance!
ian.

My closest effort:

def search_closure = {
source {
from = 0
size = 10
query {
dis_max {
queries = [
term ( fqn:'hello' ),
term ( placeName='hello')
]
}
}
sort {
pref {
order = 'desc'
}
}
}
}

def res = esclient.search(search_closure)

This Reports

java.lang.IllegalArgumentException: Array elements must be defined with the "element" method call eg: element(value)
at org.elasticsearch.groovy.common.xcontent.GXContentBuilder.invokeMethod(GXContentBuilder.groovy:121)

So I tried this:

def search_closure = {
source {
from = 0
size = 10
query {
dis_max {
queries = [
element {
term ( fqn:'hello' )
},
element {
term ( placeName='hello')
}
]
}
}
sort {
pref {
order = 'desc'
}
}
}
}

Which also reports the same.java.lang.IllegalArgumentException: Array elements can only be defined under "array" nodes

Finally, for fun, This one blows the top off the stack:

def search_closure = {
source {
from = 0
size = 10
query {
dis_max {
queries = [
term {
fqn='hello'
},
term {
placeName='hello'
}
]
}
}
sort {
pref {
order = 'desc'
}
}
}
}

testSearchClosure(search_closure);

def res = esclient.search(search_closure)

Which blows the stack with the following (repeated) stack trace

at org.elasticsearch.common.jackson.JsonStreamContext.(JsonStreamContext.java:53)
at org.elasticsearch.common.jackson.impl.JsonWriteContext.(JsonWriteContext.java:48)
at org.elasticsearch.common.jackson.impl.JsonWriteContext.createChildObjectContext(JsonWriteContext.java:82)
at org.elasticsearch.common.jackson.impl.Utf8Generator.writeStartObject(Utf8Generator.java:371)
at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeStartObject(JsonXContentGenerator.java:63)
at org.elasticsearch.common.xcontent.support.XContentMapConverter.writeMap(XContentMapConverter.java:117)
at org.elasticsearch.common.xcontent.support.XContentMapConverter.writeValue(XContentMapConverter.java:165)
at org.elasticsearch.common.xcontent.support.XContentMapConverter.writeMap(XContentMapConverter.java:125)
at org.elasticsearch.common.xcontent.support.XContentMapConverter.writeValue(XContentMapConverter.java:165)
at org.elasticsearch.common.xcontent.support.XContentMapConverter.writeIterable(XContentMapConverter.java:135)
at org.elasticsearch.common.xcontent.support.XContentMapConverter.writeValue(XContentMapConverter.java:167)
at org.elasticsearch.common.xcontent.support.XContentMapConverter.writeMap(XContentMapConverter.java:125)
at org.elasticsearch.common.xcontent.support.XContentMapConverter.writeValue(XContentMapConverter.java:165)
... Repeats...

Any ideas?