k-NN Painless Scripting extensions

With the k-NN plugin’s Painless Scripting extensions, you can use k-NN distance functions directly in your Painless scripts to perform operations on knn_vector fields. Painless has a strict list of allowed functions and classes per context to ensure its scripts are secure. The k-NN plugin adds Painless Scripting extensions to a few of the distance functions used in k-NN score script, so you can use them to customize your k-NN workload.

Get started with k-NN’s Painless Scripting functions

To use k-NN’s Painless Scripting functions, first create an index with knn_vector fields like in k-NN score script. Once the index is created and you ingest some data, you can use the painless extensions:

  1. GET my-knn-index-2/_search
  2. {
  3. "size": 2,
  4. "query": {
  5. "script_score": {
  6. "query": {
  7. "bool": {
  8. "filter": {
  9. "term": {
  10. "color": "BLUE"
  11. }
  12. }
  13. }
  14. },
  15. "script": {
  16. "source": "1.0 + cosineSimilarity(params.query_value, doc[params.field])",
  17. "params": {
  18. "field": "my_vector",
  19. "query_value": [9.9, 9.9]
  20. }
  21. }
  22. }
  23. }
  24. }

copy

field needs to map to a knn_vector field, and query_value needs to be a floating point array with the same dimension as field.

Function types

The following table describes the available painless functions the k-NN plugin provides:

Function nameFunction signatureDescription
l2Squaredfloat l2Squared (float[] queryVector, doc[‘vector field’])This function calculates the square of the L2 distance (Euclidean distance) between a given query vector and document vectors. The shorter the distance, the more relevant the document is, so this example inverts the return value of the l2Squared function. If the document vector matches the query vector, the result is 0, so this example also adds 1 to the distance to avoid divide by zero errors.
l1Normfloat l1Norm (float[] queryVector, doc[‘vector field’])This function calculates the L1 Norm distance (Manhattan distance) between a given query vector and document vectors.
cosineSimilarityfloat cosineSimilarity (float[] queryVector, doc[‘vector field’])Cosine similarity is an inner product of the query vector and document vector normalized to both have a length of 1. If the magnitude of the query vector doesn’t change throughout the query, you can pass the magnitude of the query vector to improve performance, instead of calculating the magnitude every time for every filtered document:
float cosineSimilarity (float[] queryVector, doc[‘vector field’], float normQueryVector)
In general, the range of cosine similarity is [-1, 1]. However, in the case of information retrieval, the cosine similarity of two documents ranges from 0 to 1 because the tf-idf statistic can’t be negative. Therefore, the k-NN plugin adds 1.0 in order to always yield a positive cosine similarity score.
hammingfloat hamming (float[] queryVector, doc[‘vector field’])This function calculates the Hamming distance between a given query vector and document vectors. The Hamming distance is the number of positions at which the corresponding elements are different. The shorter the distance, the more relevant the document is, so this example inverts the return value of the Hamming distance.

The hamming space type is supported for binary vectors in OpenSearch version 2.16 and later. For more information, see Binary k-NN vectors.

Constraints

  1. If a document’s knn_vector field has different dimensions than the query, the function throws an IllegalArgumentException.

  2. If a vector field doesn’t have a value, the function throws an IllegalStateException.

    You can avoid this situation by first checking if a document has a value in its field:

    1. "source": "doc[params.field].size() == 0 ? 0 : 1 / (1 + l2Squared(params.query_value, doc[params.field]))",

    Because scores can only be positive, this script ranks documents with vector fields higher than those without.

With cosine similarity, it is not valid to pass a zero vector ([0, 0, ...]) as input. This is because the magnitude of such a vector is 0, which raises a divide by 0 exception in the corresponding formula. Requests containing the zero vector will be rejected, and a corresponding exception will be thrown.