From 0f68054401a435767014cbac2accbf7fcbc0452a Mon Sep 17 00:00:00 2001 From: aerickson-clt <32244272+aerickson-clt@users.noreply.github.com> Date: Fri, 4 Aug 2023 02:06:17 -0400 Subject: [PATCH] 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 --- .../vectorstores/opensearch_vector_search.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/libs/langchain/langchain/vectorstores/opensearch_vector_search.py b/libs/langchain/langchain/vectorstores/opensearch_vector_search.py index 1e688a5c65..8e84835df2 100644 --- a/libs/langchain/langchain/vectorstores/opensearch_vector_search.py +++ b/libs/langchain/langchain/vectorstores/opensearch_vector_search.py @@ -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": {