Skip to content

Dense Vector Feature as a param to a Mustache script score template #465

@razilevin

Description

@razilevin

Trying to use embeddings to compute cosine similarity. The problem I am getting is there no way to pass the embedding as a param to invoke the following feature during logging.

{
    "name": "vector_simularity",
    "params": [
        "embedding"
    ],
    "template_language": "mustache",
    "template": {
        "function_score": {
            "script_score": {
                "script": {
                    "source": "1 + cosineSimilarity(params.query_vector, doc['base_name_vector'])",
                    "params": {
                        "query_vector": "{{#toJson}}embedding{{/toJson}}"
                    }
                }
            }
        }
    }
}

I got the idea to use toJson mustache template from another post which seems to match what I am tying to do #338

I get the following error when running the query

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "1 + cosineSimilarity(params['query_vector'], doc['base_name_vector'])",
          "                           ^---- HERE"
        ],
        "script": "1 + cosineSimilarity(params['query_vector'], doc['base_name_vector'])",
        "lang": "painless",
        "position": {
          "offset": 27,
          "start": 0,
          "end": 69
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "semantic_search",
        "node": "57-FXL1dQwOjKxaOn62-Dw",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "1 + cosineSimilarity(params.query_vector, doc['base_name_vector'])",
            "                           ^---- HERE"
          ],
          "script": "1 + cosineSimilarity(params.query_vector', doc['base_name_vector'])",
          "lang": "painless",
          "position": {
            "offset": 27,
            "start": 0,
            "end": 69
          },
          "caused_by": {
            "type": "class_cast_exception",
            "reason": "class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader 'bootstrap')"
          }
        }
      }
    ]
  },
  "status": 400
}

Please note a query like the following works as expected

{
  "query": {
    "size": 36,
    "script_score": {
      "query": {
        "match_all": {}
      },
      "script": {
        "source": "cosineSimilarity(params.queryVector, doc['base_name_vector']) + 1.0",
        "params": {
          "queryVector": query_embedding
        }
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions