Skip to content

Instantly share code, notes, and snippets.

@dezsenyi
Created February 17, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dezsenyi/6a73e953ea6c78bf7774 to your computer and use it in GitHub Desktop.
Save dezsenyi/6a73e953ea6c78bf7774 to your computer and use it in GitHub Desktop.
ElasticSearch has_parent scoring problem
echo "Delete old data"
curl -XDELETE "http://localhost:9200/testindex"
echo "Create test index"
curl -XPUT "http://localhost:9200/testindex/" -d'
{
"index": {
"number_of_replicas": "0",
"number_of_shards": "1"
}
}'
echo "Create mapping for parent documents"
curl -XPUT "http://localhost:9200/testindex/_mapping/document" -d'
{
"document": {
"properties": {
"title": {
"type": "string"
}
}
}
}'
echo "Create mapping for child reviews"
curl -XPUT "http://localhost:9200/testindex/review/_mapping" -d'
{
"review": {
"_parent": {
"type": "document"
},
"properties": {
"comments": {
"type": "string"
}
}
}
}'
echo "Put 1 document and 1 child review to the index"
curl -XPUT "http://localhost:9200/testindex/document/1" -d'
{
"title": "This is just a simple test title"
}'
curl -XPUT "http://localhost:9200/testindex/review/2?parent=1" -d'
{
"comments": "Weird scoring example"
}'
echo "Test query 1:"
echo "Search for parent documents and also their child reviews"
echo "Scores are equal: 0.375 - it is fine!"
curl -XGET "http://localhost:9200/testindex/document,review/_search" -d'
{
"explain": false,
"query": {
"bool": {
"must": [
{
"dis_max": {
"queries": [
{
"query_string": {
"query": "simple",
"fields": [
"document.title"
]
}
},
{
"has_parent": {
"parent_type": "document",
"score_type": "score",
"query": {
"query_string": {
"query": "simple",
"fields": [
"document.title"
]
}
}
}
}
]
}
}
]
}
}
}'
echo "Test query 2:"
echo "The above query, but having it twice and connected with BOOL query"
echo "The scores of the two results should be the same, but we receive different scores"
echo "The problem seems that the queryNorm calculated differently for the has_parent parts..."
curl -XGET "http://localhost:9200/testindex/document,review/_search" -d'
{
"explain": false,
"query": {
"bool": {
"must": [
{
"dis_max": {
"queries": [
{
"query_string": {
"query": "simple",
"fields": [
"document.title"
]
}
},
{
"has_parent": {
"parent_type": "document",
"score_type": "score",
"query": {
"query_string": {
"query": "simple",
"fields": [
"document.title"
]
}
}
}
}
]
}
},
{
"dis_max": {
"queries": [
{
"multi_match": {
"query": "simple",
"fields": [
"document.title"
]
}
},
{
"has_parent": {
"parent_type": "document",
"score_type": "score",
"query": {
"multi_match": {
"query": "simple",
"fields": [
"document.title"
]
}
}
}
}
]
}
}
]
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment