0
votes
  • Using Elasticsearch v6.5

Hi, I am using function_score query with field_value_factor on a date field. My query is following:

POST /users/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },

      "functions": [
        {
          "field_value_factor": {
            "field": "createdAt"
          }
        }
      ]
    }
  }
}

Response
200 OK
{
  "hits": {
    "hits": [
      {
        "_score": 1545536870000000,
        "_type": "_doc",
        "_id": "user1",
        "_source": {
          "createdAt": 1545536877421,
          "firstName": "foo1"
        },
        "_index": "users"
      },
      {
        "_score": 1545536870000000,
        "_type": "_doc",
        "_id": "user2",
        "_source": {
          "createdAt": 1545536877422,
          "firstName": "foo2"
        },
        "_index": "users"
      }
    ],
    "max_score": 1545536870000000
  },
  "took": 17
}

My Question are: 1. why it returned same score for both the documents even when the field's values are different? 2. why the score trimmed the actual field value into a smaller value and then multiple by 10000000. Isn't the whole point of using field value factor is to score those documents higher which has higher field value.

1

1 Answers

1
votes

If you add the explain:true parameter you will see that you are exceeding the max value for score, which is why they end up all being the same.

 "hits": {
"total": 2,
"max_score": 1545536930000,
"hits": [
  {
    "_shard": "[test1][2]",
    "_node": "L0mg3oZdRdSSlah6QPVqjQ",
    "_index": "test1",
    "_type": "doc",
    "_id": "2",
    "_score": 1545536930000,
    "_source": {
      "createdAt": 1545536877422,
      "firstName": "foo2"
    },
    "_explanation": {
      "value": 1545536930000,
      "description": "function score, product of:",
      "details": [
        {
          "value": 1,
          "description": "*:*, product of:",
          "details": [
            {
              "value": 1,
              "description": "boost",
              "details": []
            },
            {
              "value": 1,
              "description": "queryNorm",
              "details": []
            }
          ]
        },
        {
          "value": 1545536930000,
          "description": "min of:",
          "details": [
            {
              "value": 1545536930000,
              "description": "field value function: none(doc['createdAt'].value * factor=1.0)",
              "details": []
            },
            {
              "value": 3.4028235e+38,
              "description": "maxBoost",
              "details": []
            }
          ]
        }
      ]
    }
  },
  {
    "_shard": "[test1][3]",
    "_node": "L0mg3oZdRdSSlah6QPVqjQ",
    "_index": "test1",
    "_type": "doc",
    "_id": "1",
    "_score": 1545536930000,
    "_source": {
      "createdAt": 1545536877421,
      "firstName": "foo1"
    },
    "_explanation": {
      "value": 1545536930000,
      "description": "function score, product of:",
      "details": [
        {
          "value": 1,
          "description": "*:*, product of:",
          "details": [
            {
              "value": 1,
              "description": "boost",
              "details": []
            },
            {
              "value": 1,
              "description": "queryNorm",
              "details": []
            }
          ]
        },
        {
          "value": 1545536930000,
          "description": "min of:",
          "details": [
            {
              "value": 1545536930000,
              "description": "field value function: none(doc['createdAt'].value * factor=1.0)",
              "details": []
            },
            {
              "value": 3.4028235e+38,
              "description": "maxBoost",
              "details": []
            }
          ]
        }
      ]
    }
  }
]

}

Quoting from this github issue link:

The final _score is a float, which can only represent integers accurately up to to 2^25. Timestamps are of the order of 2^40, so cannot be represented accurately, hence the rounding that you are seeing.