Issue #8089 Improve painless script scoring with params.query_value. (#8086)

This is a minor improvement that replaces the full query_vector with the
reference string `params.query_value` used in the painless scripting
docs. I have tested it manually and it works on an example. This makes
the query about half the size and much easier to read.


https://opensearch.org/docs/latest/search-plugins/knn/painless-functions/#get-started-with-k-nns-painless-scripting-functions

@babbldev 
#8089

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
pull/8744/head
aerickson-clt 1 year ago committed by GitHub
parent 0ead8ea708
commit 0f68054401
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -265,17 +265,11 @@ def _default_script_query(
def __get_painless_scripting_source(
space_type: str, query_vector: List[float], vector_field: str = "vector_field"
space_type: str, vector_field: str = "vector_field"
) -> str:
"""For Painless Scripting, it returns the script source based on space type."""
source_value = (
"(1.0 + "
+ space_type
+ "("
+ str(query_vector)
+ ", doc['"
+ vector_field
+ "']))"
"(1.0 + " + space_type + "(params.query_value, doc['" + vector_field + "']))"
)
if space_type == "cosineSimilarity":
return source_value
@ -295,9 +289,7 @@ def _default_painless_scripting_query(
if not pre_filter:
pre_filter = MATCH_ALL_QUERY
source = __get_painless_scripting_source(
space_type, query_vector, vector_field=vector_field
)
source = __get_painless_scripting_source(space_type, vector_field=vector_field)
return {
"size": k,
"query": {

Loading…
Cancel
Save