Skip to content

Wrong percentile_ranks calculation #28536

@martini97

Description

@martini97

Elasticsearch version (bin/elasticsearch --version):
Version: 6.1.3, Build: af51318/2018-01-26T18:22:55.523Z, JVM: 1.8.0_16

Plugins installed: []

JVM version (java -version):
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

OS version (uname -a if on a Unix-like system):
Linux 053db24b9161 4.13.0-32-generic #35~16.04.1-Ubuntu SMP Thu Jan 25 10:13:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Description of the problem including expected versus actual behavior:
Percentile ranks returns incorrect value for nested aggregation.
Query that I'm using:

curl -XGET "http://elasticsearch:9200/questions/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "player": {
      "filter": {
        "bool": {
          "must": [ { "term": { "playerId": 1 } } ]
        }
      },
      "aggs": {
        "topics": {
          "terms": { "field": "topicId" },
          "aggs": {
            "date": {
              "date_histogram": {
                "field": "date",
                "interval": "day"
              },
              "aggs": {
                "percentage": {
                  "percentile_ranks": {
                    "field": "correctAsInt",
                    "values": [ 50 ]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}'

Result:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 70,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "player": {
      "doc_count": 60,
      "topics": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 1,
            "doc_count": 30,
            "date": {
              "buckets": [
                {
                  "key_as_string": "2018-02-06T00:00:00.000Z",
                  "key": 1517875200000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 30
                    }
                  }
                },
                {
                  "key_as_string": "2018-02-07T00:00:00.000Z",
                  "key": 1517961600000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 40
                    }
                  }
                },
                {
                  "key_as_string": "2018-02-08T00:00:00.000Z",
                  "key": 1518048000000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 20
                    }
                  }
                }
              ]
            }
          },
          {
            "key": 2,
            "doc_count": 30,
            "date": {
              "buckets": [
                {
                  "key_as_string": "2018-02-06T00:00:00.000Z",
                  "key": 1517875200000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 70
                    }
                  }
                },
                {
                  "key_as_string": "2018-02-07T00:00:00.000Z",
                  "key": 1517961600000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 60
                    }
                  }
                },
                {
                  "key_as_string": "2018-02-08T00:00:00.000Z",
                  "key": 1518048000000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 0      <---- this is the only wrong value
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

Expected result:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 70,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "player": {
      "doc_count": 60,
      "topics": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 1,
            "doc_count": 30,
            "date": {
              "buckets": [
                {
                  "key_as_string": "2018-02-06T00:00:00.000Z",
                  "key": 1517875200000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 30
                    }
                  }
                },
                {
                  "key_as_string": "2018-02-07T00:00:00.000Z",
                  "key": 1517961600000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 40
                    }
                  }
                },
                {
                  "key_as_string": "2018-02-08T00:00:00.000Z",
                  "key": 1518048000000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 20
                    }
                  }
                }
              ]
            }
          },
          {
            "key": 2,
            "doc_count": 30,
            "date": {
              "buckets": [
                {
                  "key_as_string": "2018-02-06T00:00:00.000Z",
                  "key": 1517875200000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 70
                    }
                  }
                },
                {
                  "key_as_string": "2018-02-07T00:00:00.000Z",
                  "key": 1517961600000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 60
                    }
                  }
                },
                {
                  "key_as_string": "2018-02-08T00:00:00.000Z",
                  "key": 1518048000000,
                  "doc_count": 10,
                  "percentage": {
                    "values": {
                      "50.0": 80      <---- correct value
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

The correctAsInt field used to be a boolean, but we changed so we could use this aggregation, it only receive 2 values: 100 if true or 0 if false.

Steps to reproduce:

Please include a minimal but complete recreation of the problem, including
(e.g.) index creation, mappings, settings, query etc. The easier you make for
us to reproduce it, the more likely that somebody will take the time to look at it.

  1. Create mapping:
curl -XPUT "http://elasticsearch:9200/questions" -H 'Content-Type: application/json' -d'
{
    "mappings": {
        "questions": {
            "properties": {
                "playerId": { "type": "integer" },
                "topicId": { "type": "integer" },
                "correctAsInt": { "type": "integer" },
                "date": { "type": "date" }
            }
        }
    }
}'
  1. Insert data:
curl -XPOST "http://elasticsearch:9200/_bulk" -H 'Content-Type: application/json' -d'
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "0" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "1" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "2" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "3" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "4" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "5" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "6" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "7" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "8" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "9" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "10" } }
{ "correctAsInt": 0,   "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "11" } }
{ "correctAsInt": 0,   "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "12" } }
{ "correctAsInt": 0,   "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "13" } }
{ "correctAsInt": 0, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "14" } }
{ "correctAsInt": 100, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "15" } }
{ "correctAsInt": 100, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "16" } }
{ "correctAsInt": 100, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "17" } }
{ "correctAsInt": 100, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "18" } }
{ "correctAsInt": 100, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "19" } }
{ "correctAsInt": 100, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "20" } }
{ "correctAsInt": 0,   "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "21" } }
{ "correctAsInt": 0,   "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "22" } }
{ "correctAsInt": 100,   "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "23" } }
{ "correctAsInt": 100, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "24" } }
{ "correctAsInt": 100, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "25" } }
{ "correctAsInt": 100, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "26" } }
{ "correctAsInt": 100, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "27" } }
{ "correctAsInt": 100, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "28" } }
{ "correctAsInt": 100, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "29" } }
{ "correctAsInt": 100, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "30" } }
{ "correctAsInt": 100,   "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "31" } }
{ "correctAsInt": 100,   "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "32" } }
{ "correctAsInt": 100,   "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "33" } }
{ "correctAsInt": 0, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "34" } }
{ "correctAsInt": 0, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "35" } }
{ "correctAsInt": 0, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "36" } }
{ "correctAsInt": 0, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "37" } }
{ "correctAsInt": 0, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "38" } }
{ "correctAsInt": 0, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "39" } }
{ "correctAsInt": 0, "date": "2018-02-06T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "40" } }
{ "correctAsInt": 0, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "41" } }
{ "correctAsInt": 100,   "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "42" } }
{ "correctAsInt": 100,   "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "43" } }
{ "correctAsInt": 100,   "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "44" } }
{ "correctAsInt": 100, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "45" } }
{ "correctAsInt": 0, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "46" } }
{ "correctAsInt": 0, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "47" } }
{ "correctAsInt": 0, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "48" } }
{ "correctAsInt": 0, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "49" } }
{ "correctAsInt": 0, "date": "2018-02-07T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "50" } }
{ "correctAsInt": 0, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "51" } }
{ "correctAsInt": 100,   "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "52" } }
{ "correctAsInt": 100,   "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "53" } }
{ "correctAsInt": 0,   "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "54" } }
{ "correctAsInt": 0, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "55" } }
{ "correctAsInt": 0, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "56" } }
{ "correctAsInt": 0, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "57" } }
{ "correctAsInt": 0, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "58" } }
{ "correctAsInt": 0, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "59" } }
{ "correctAsInt": 0, "date": "2018-02-08T12:16:40.816Z", "playerId": 1, "topicId": 2 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "60" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "61" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "62" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "63" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "64" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "65" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "66" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "67" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "68" } }
{ "correctAsInt": 0,   "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
{ "create" : { "_index" : "questions", "_type" : "questions", "_id" : "69" } }
{ "correctAsInt": 100, "date": "2018-02-06T12:17:16.942Z", "playerId": 2, "topicId": 1 }
'
  1. Run the query:
curl -XGET "http://elasticsearch:9200/questions/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "player": {
      "filter": {
        "bool": {
          "must": [ { "term": { "playerId": 1 } } ]
        }
      },
      "aggs": {
        "topics": {
          "terms": { "field": "topicId" },
          "aggs": {
            "date": {
              "date_histogram": {
                "field": "date",
                "interval": "day"
              },
              "aggs": {
                "percentage": {
                  "percentile_ranks": {
                    "field": "correctAsInt",
                    "values": [ 50 ]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}'

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions