Created
December 1, 2011 08:42
-
-
Save patrikjohansson/1415070 to your computer and use it in GitHub Desktop.
Problem when looping over arrays with objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adding a tweet with an array of tags and an array with weights for each detected language. | |
curl -XPUT http://localhost:9201/twitter/tweet/1 -d ' | |
{ | |
"tags" : ["foo", "bar"], | |
"weights" : [{"lang":"eng", "weight":0.97}, {"lang":"swe", "weight":0.03}] | |
} | |
' | |
{"ok":true,"_index":"twitter2","_type":"tweet","_id":"1","_version":1} | |
-------- | |
# Looping over the tags-array works as expcted and sets the tmp-variable to 'loop ok' | |
curl -XGET http://localhost:9201/twitter/tweet/_search?pretty=true -d ' | |
{ | |
"query": { | |
"match_all": {} | |
}, | |
"script_fields" : { | |
"foo" : { | |
"script":"tmp = ''; for(item : doc['tags'].values){tmp = 'loop ok'}; return tmp" | |
} | |
} | |
} | |
' | |
Returns: | |
{ | |
"took" : 4, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 1, | |
"max_score" : 1.0, | |
"hits" : [ { | |
"_index" : "twitter", | |
"_type" : "tweet", | |
"_id" : "1", | |
"_score" : 1.0, | |
"fields" : { | |
"foo" : "loop ok" | |
} | |
} ] | |
} | |
} | |
--------- | |
# Looping over the array named weights, which is an array of objects, does not work | |
curl -XGET http://localhost:9201/twitter/tweet/_search?pretty=true -d ' | |
{ | |
"query": { | |
"match_all": {} | |
}, | |
"script_fields" : { | |
"foo" : { | |
"script":"tmp = ''; for(item : doc['weights'].values){tmp = 'loop ok'}; return tmp" | |
} | |
} | |
} | |
' | |
Returns : | |
{ | |
"took" : 11, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 4, | |
"failed" : 1, | |
"failures" : [ { | |
"status" : 500, | |
"reason" : "CompileException[[Error: No field found for [weights]]\n[Near : {... tmp = ''; for(item : doc['weig ....}]\n ^\n[Line: 1, Column: 1]]; nested: ElasticSearchIllegalArgumentException[No field found for [weights]]; " | |
} ] | |
}, | |
"hits" : { | |
"total" : 1, | |
"max_score" : 1.0, | |
"hits" : [ ] | |
} | |
} | |
The stack trace can be found here: https://gist.github.com/1414981 | |
------- | |
Mapping for this index and type: | |
curl -XGET http://localhost:9201/twitter/tweet/_mapping?pretty=true | |
returns this: | |
{ | |
"tweet" : { | |
"properties" : { | |
"tags" : { | |
"type" : "string" | |
}, | |
"weights" : { | |
"dynamic" : "true", | |
"properties" : { | |
"weight" : { | |
"type" : "double" | |
}, | |
"lang" : { | |
"type" : "string" | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI: I get the same result with foreach...