Created
September 26, 2012 16:24
-
-
Save straux/3788994 to your computer and use it in GitHub Desktop.
Elastic search terms facets : problems when performing script_field on an object with array
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
# test performed on elasticsearch-0.19.0.RC2 and elasticsearch-0.19.9 | |
set -x | |
# delete index | |
curl -XDELETE '127.0.0.1:9200/test' | |
# create index | |
curl -XPUT '127.0.0.1:9200/test' | |
# mapping | |
curl -XPUT '127.0.0.1:9200/test/test/_mapping' -d ' | |
{ | |
"test" : { | |
"properties" : { | |
"array" : { | |
"type" : "object", | |
"properties" : { | |
"field" : { | |
"type" : "string", | |
"store" : "yes" | |
} | |
} | |
} | |
} | |
} | |
} | |
' | |
# index documents | |
curl -XPOST '127.0.0.1:9200/test/test/1' -d '{ | |
"array" : [ | |
{ "field" : "a" }, | |
{ "field" : "b" } | |
] | |
}' | |
curl -XPOST '127.0.0.1:9200/test/test/2' -d '{ | |
"array" : [ | |
{ "field" : "b" }, | |
{ "field" : "c" } | |
] | |
}' | |
# refresh to make docs available for search | |
curl -XPOST '127.0.0.1:9200/test/_refresh' | |
# term facets | |
curl -XPOST "127.0.0.1:9200/test/test/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"match_all" : {} | |
}, | |
"facets" : { | |
"test" : { | |
"terms" : { | |
"field" : "array.field", | |
"size" : 10 | |
} | |
} | |
} | |
}' | |
# ... | |
#"facets" : { | |
#"test" : { | |
#"_type" : "terms", | |
#"missing" : 0, | |
#"total" : 3, | |
#"other" : 0, | |
#"terms" : [ { | |
#"term" : "b", | |
#"count" : 2 | |
#}, { | |
#"term" : "c", | |
#"count" : 1 | |
#} ] | |
#} | |
#} | |
# OK, but ... where is the "a" value ? | |
# | |
# term facets with script on _source | |
curl -XPOST "127.0.0.1:9200/test/test/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"match_all" : {} | |
}, | |
"facets" : { | |
"test" : { | |
"terms" : { | |
"script_field" : "_source.array.field", | |
"size" : 10 | |
} | |
} | |
} | |
}' | |
# ... | |
#"failures" : [ { | |
#"index" : "test", | |
#"shard" : 3, | |
#"status" : 500, | |
#"reason" : "QueryPhaseExecutionException[[test][3]: query[ConstantScore(cache(_type:test))],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: could not access: field; in class: java.util.ArrayList]\n[Near : {... _source.array.field ....}]\n ^\n[Line: 1, Column: 1]]; " | |
#}, { | |
#"index" : "test", | |
#"shard" : 2, | |
#"status" : 500, | |
#"reason" : "QueryPhaseExecutionException[[test][2]: query[ConstantScore(cache(_type:test))],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: could not access: field; in class: java.util.ArrayList]\n[Near : {... _source.array.field ....}]\n ^\n[Line: 1, Column: 1]]; " | |
#} ] | |
# term facets with script on _fields | |
curl -XPOST "127.0.0.1:9200/test/test/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"match_all" : {} | |
}, | |
"facets" : { | |
"test" : { | |
"terms" : { | |
"script_field" : "_fields['\''array.field'\'']", | |
"size" : 10 | |
} | |
} | |
} | |
}' | |
# ... | |
# "facets" : { | |
#"test" : { | |
#"_type" : "terms", | |
#"missing" : 0, | |
#"total" : 2, | |
#"other" : 0, | |
#"terms" : [ { | |
#"term" : "org.elasticsearch.search.lookup.FieldLookup@53d34c", | |
#"count" : 1 | |
#}, { | |
#"term" : "org.elasticsearch.search.lookup.FieldLookup@1553743", | |
#"count" : 1 | |
#} ] | |
#} | |
#} | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I finally found the answer : you have to use the mvel projection
operator to iterate over the collection :
http://mvel.codehaus.org/MVEL+2.0+Projections+and+Folds
I my example, you can do :
{
"terms" : {
"script_field" : "(field in _source.array)",
"size" : 10
}
instead of "script_field" : "_source.array.field"
This does not explains why the "a" element is not taken in account in
the first facets performed on the index, though.