Elasticsearch painless script l2norm are different from l2_norm knnSearch

I would like to test l2norm by this:

POST /_scripts/painless/_execute
{
  "script": {
    "source": "l2norm(params.v1, params.v2)",
    "params": {
      "v1": [1.0, 1.0],
      "v2": [1.0, 1.0]
    }
  }
}

However, it throws exception,

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "l2norm(params.v1, params. ...",
          "^---- HERE"
        ],
        "script" : "l2norm(params.v1, params.v2)",
        "lang" : "painless",
        "position" : {
          "offset" : 0,
          "start" : 0,
          "end" : 25
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "l2norm(params.v1, params. ...",
      "^---- HERE"
    ],
    "script" : "l2norm(params.v1, params.v2)",
    "lang" : "painless",
    "position" : {
      "offset" : 0,
      "start" : 0,
      "end" : 25
    },
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Unknown call [l2norm] with [2] arguments."
    }
  },
  "status" : 400
}

The reason I would like to test that is because I noticed for a dense_vector having l2_norm, knnSearch result score is different from that using script l2norm.

PUT product-index
{
  "mappings": {
    "properties": {
      "product-vector": {
        "type": "dense_vector",
        "dims": 5,
        "index": true,
        "similarity": "l2_norm"
      },
      "price": {
        "type": "long"
      }
    }
  }
}

POST product-index/_bulk?refresh=true
{ "index": { "_id": "1" } }
{ "product-vector": [230.0, 300.33, -34.8988, 15.555, -200.0], "price": 1599 }
{ "index": { "_id": "2" } }
{ "product-vector": [-0.5, 100.0, -13.0, 14.8, -156.0], "price": 799 }
{ "index": { "_id": "3" } }
{ "product-vector": [0.5, 111.3, -13.0, 14.8, -156.0], "price": 1099 }

POST product-index/_knn_search
{
  "_source": false,
  "fields": [
    "serverId",
    "id",
    "createTime",
    "groupId",
    "userId",
    "appId"
  ],
  "knn": {
    "field": "product-vector",
    "query_vector": [-0.5, 90.0, -10, 14.8, -156.0],
    "k": 10,
    "num_candidates": 100
  }
}


POST product-index/_search
{
  "query": {
    "script_score": {
      "query" : {
        "bool" : {
          "filter" : {
            "range" : {
              "price" : {
                "gte": 1000
              }
            }
          }
        }
      },
      "script": {
        "source": "l2norm(params.queryVector, 'product-vector')",
        "params": {
          "queryVector": [-0.5, 90.0, -10, 14.8, -156.0]
        }
      }
    }
  }
}

In above code, _search using l2_norm script has max_score : 316.10992, while _knn_search has max_score : 0.009090909. I would like dig into what causes this difference, since they both are l2_norm.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.