Skip to content

Instantly share code, notes, and snippets.

@imotov
Last active December 11, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save imotov/4569086 to your computer and use it in GitHub Desktop.
Save imotov/4569086 to your computer and use it in GitHub Desktop.
curl -XDELETE localhost:9200/test-idx
curl -XPUT localhost:9200/test-idx -d '{
"settings": {
"index.number_of_shards": 1,
"index.number_of_replicas": 0
},
"mappings": {
"doc": {
"properties": {
"name": {"type": "string"}
}
},
"score": {
"_parent": {
"type": "doc"
},
"properties": {
"point_type": {"type": "string", "index": "not_analyzed"},
"points": {"type": "integer"}
}
}
}
}'
curl -XPUT "localhost:9200/test-idx/doc/1" -d '{"name": "doc_1"}'
curl -XPUT "localhost:9200/test-idx/score/1-1?parent=1" -d '{"point_type": "odd", "points": 5}'
curl -XPUT "localhost:9200/test-idx/score/1-2?parent=1" -d '{"point_type": "odd", "points": 3}'
curl -XPUT "localhost:9200/test-idx/score/1-3?parent=1" -d '{"point_type": "even", "points": 8}'
curl -XPUT "localhost:9200/test-idx/doc/2" -d '{"name": "doc_2"}'
curl -XPUT "localhost:9200/test-idx/score/2-1?parent=2" -d '{"point_type": "odd", "points": 7}'
curl -XPUT "localhost:9200/test-idx/score/2-2?parent=2" -d '{"point_type": "even", "points": 6}'
curl -XPUT "localhost:9200/test-idx/score/2-3?parent=2" -d '{"point_type": "even", "points": 4}'
curl -XPOST localhost:9200/_refresh
echo
# Note: we have to specify fully qualified name (score.points) in the script as a workaround for this TODO:
# https://github.com/elasticsearch/elasticsearch/blob/3cd54fc4ee70a395e26dc5220e8245a55e2723aa/src/main/java/org/elasticsearch/index/query/HasChildQueryParser.java#L68
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
"query": {
"has_child": {
"type": "score",
"score_type" : "sum",
"query" : {
"custom_score": {
"script": "doc[\"score.points\"].value * 10",
"query": {
"term" : {
"point_type" : "odd"
}
}
}
}
}
}
}'
@clintongormley
Copy link

Heya @imotov

Could you explain what it is you're working around here? Why do you need to specify both types at the top level, then filter on the parent type?

ta

clint

@imotov
Copy link
Author

imotov commented Jan 19, 2013

That's an excellent question @clintongormley! When request is parsed, elasticsearch is using types specified on the URL to resolve fields in the queries. It works fine in most cases except has_child and top_children queries and filters, where fields in internal query should be resolved against the child type. Unfortunately, the type change is not getting propagated to internal queries and they still use parent's type to resolve fields. As a result, the field points in the script is getting resolved against doc type and fails.

However, because you asked and I had to think a little bit more about it, a much simpler workaround came to my mind - using full field name. I have updated the gist. Thank you for such a great question, Clint!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment