Skip to content

Commit b84a6b1

Browse files
authored
fix: opensearch script score with filters (#3321)
* fix opensearch script score filters * add comment * add integration test * update schema
1 parent 6cb4e93 commit b84a6b1

File tree

4 files changed

+86
-59
lines changed

4 files changed

+86
-59
lines changed

haystack/document_stores/opensearch.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,12 @@ def query_by_embedding(
429429
# +1 in similarity to avoid negative numbers (for cosine sim)
430430
body: Dict[str, Any] = {"size": top_k, "query": self._get_vector_similarity_query(query_emb, top_k)}
431431
if filters:
432-
if not "bool" in body["query"]:
433-
body["query"]["bool"] = {}
434-
body["query"]["bool"]["filter"] = LogicalFilterClause.parse(filters).convert_to_elasticsearch()
432+
filter_ = LogicalFilterClause.parse(filters).convert_to_elasticsearch()
433+
if "script_score" in body["query"]:
434+
# set filter for pre-filtering (see https://opensearch.org/docs/latest/search-plugins/knn/knn-score-script/)
435+
body["query"]["script_score"]["query"] = {"bool": {"filter": filter_}}
436+
else:
437+
body["query"]["bool"]["filter"] = filter_
435438

436439
excluded_meta_data: Optional[list] = None
437440

haystack/json-schemas/haystack-pipeline-1.10.0rc0.schema.json

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,10 +3044,10 @@
30443044
"title": "Use Auth Token",
30453045
"anyOf": [
30463046
{
3047-
"type": "boolean"
3047+
"type": "string"
30483048
},
30493049
{
3050-
"type": "string"
3050+
"type": "boolean"
30513051
},
30523052
{
30533053
"type": "null"
@@ -3345,10 +3345,10 @@
33453345
"title": "Use Auth Token",
33463346
"anyOf": [
33473347
{
3348-
"type": "boolean"
3348+
"type": "string"
33493349
},
33503350
{
3351-
"type": "string"
3351+
"type": "boolean"
33523352
},
33533353
{
33543354
"type": "null"
@@ -4423,10 +4423,10 @@
44234423
"title": "Use Auth Token",
44244424
"anyOf": [
44254425
{
4426-
"type": "boolean"
4426+
"type": "string"
44274427
},
44284428
{
4429-
"type": "string"
4429+
"type": "boolean"
44304430
},
44314431
{
44324432
"type": "null"
@@ -4577,10 +4577,10 @@
45774577
"title": "Use Auth Token",
45784578
"anyOf": [
45794579
{
4580-
"type": "boolean"
4580+
"type": "string"
45814581
},
45824582
{
4583-
"type": "string"
4583+
"type": "boolean"
45844584
},
45854585
{
45864586
"type": "null"
@@ -4720,10 +4720,10 @@
47204720
"title": "Use Auth Token",
47214721
"anyOf": [
47224722
{
4723-
"type": "boolean"
4723+
"type": "string"
47244724
},
47254725
{
4726-
"type": "string"
4726+
"type": "boolean"
47274727
},
47284728
{
47294729
"type": "null"
@@ -4853,10 +4853,10 @@
48534853
"title": "Use Auth Token",
48544854
"anyOf": [
48554855
{
4856-
"type": "boolean"
4856+
"type": "string"
48574857
},
48584858
{
4859-
"type": "string"
4859+
"type": "boolean"
48604860
},
48614861
{
48624862
"type": "null"
@@ -5013,10 +5013,10 @@
50135013
"title": "Use Auth Token",
50145014
"anyOf": [
50155015
{
5016-
"type": "boolean"
5016+
"type": "string"
50175017
},
50185018
{
5019-
"type": "string"
5019+
"type": "boolean"
50205020
},
50215021
{
50225022
"type": "null"
@@ -5105,10 +5105,10 @@
51055105
"title": "Use Auth Token",
51065106
"anyOf": [
51075107
{
5108-
"type": "boolean"
5108+
"type": "string"
51095109
},
51105110
{
5111-
"type": "string"
5111+
"type": "boolean"
51125112
},
51135113
{
51145114
"type": "null"
@@ -5289,10 +5289,10 @@
52895289
"title": "Use Auth Token",
52905290
"anyOf": [
52915291
{
5292-
"type": "boolean"
5292+
"type": "string"
52935293
},
52945294
{
5295-
"type": "string"
5295+
"type": "boolean"
52965296
},
52975297
{
52985298
"type": "null"
@@ -5872,10 +5872,10 @@
58725872
"title": "Use Auth Token",
58735873
"anyOf": [
58745874
{
5875-
"type": "boolean"
5875+
"type": "string"
58765876
},
58775877
{
5878-
"type": "string"
5878+
"type": "boolean"
58795879
},
58805880
{
58815881
"type": "null"
@@ -6002,10 +6002,10 @@
60026002
"title": "Use Auth Token",
60036003
"anyOf": [
60046004
{
6005-
"type": "boolean"
6005+
"type": "string"
60066006
},
60076007
{
6008-
"type": "string"
6008+
"type": "boolean"
60096009
},
60106010
{
60116011
"type": "null"
@@ -6133,10 +6133,10 @@
61336133
"title": "Use Auth Token",
61346134
"anyOf": [
61356135
{
6136-
"type": "boolean"
6136+
"type": "string"
61376137
},
61386138
{
6139-
"type": "string"
6139+
"type": "boolean"
61406140
},
61416141
{
61426142
"type": "null"
@@ -6264,10 +6264,10 @@
62646264
"title": "Use Auth Token",
62656265
"anyOf": [
62666266
{
6267-
"type": "boolean"
6267+
"type": "string"
62686268
},
62696269
{
6270-
"type": "string"
6270+
"type": "boolean"
62716271
},
62726272
{
62736273
"type": "null"
@@ -6376,10 +6376,10 @@
63766376
"title": "Use Auth Token",
63776377
"anyOf": [
63786378
{
6379-
"type": "boolean"
6379+
"type": "string"
63806380
},
63816381
{
6382-
"type": "string"
6382+
"type": "boolean"
63836383
},
63846384
{
63856385
"type": "null"

haystack/json-schemas/haystack-pipeline-main.schema.json

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,10 +3044,10 @@
30443044
"title": "Use Auth Token",
30453045
"anyOf": [
30463046
{
3047-
"type": "boolean"
3047+
"type": "string"
30483048
},
30493049
{
3050-
"type": "string"
3050+
"type": "boolean"
30513051
},
30523052
{
30533053
"type": "null"
@@ -3345,10 +3345,10 @@
33453345
"title": "Use Auth Token",
33463346
"anyOf": [
33473347
{
3348-
"type": "boolean"
3348+
"type": "string"
33493349
},
33503350
{
3351-
"type": "string"
3351+
"type": "boolean"
33523352
},
33533353
{
33543354
"type": "null"
@@ -4423,10 +4423,10 @@
44234423
"title": "Use Auth Token",
44244424
"anyOf": [
44254425
{
4426-
"type": "boolean"
4426+
"type": "string"
44274427
},
44284428
{
4429-
"type": "string"
4429+
"type": "boolean"
44304430
},
44314431
{
44324432
"type": "null"
@@ -4577,10 +4577,10 @@
45774577
"title": "Use Auth Token",
45784578
"anyOf": [
45794579
{
4580-
"type": "boolean"
4580+
"type": "string"
45814581
},
45824582
{
4583-
"type": "string"
4583+
"type": "boolean"
45844584
},
45854585
{
45864586
"type": "null"
@@ -4720,10 +4720,10 @@
47204720
"title": "Use Auth Token",
47214721
"anyOf": [
47224722
{
4723-
"type": "boolean"
4723+
"type": "string"
47244724
},
47254725
{
4726-
"type": "string"
4726+
"type": "boolean"
47274727
},
47284728
{
47294729
"type": "null"
@@ -4853,10 +4853,10 @@
48534853
"title": "Use Auth Token",
48544854
"anyOf": [
48554855
{
4856-
"type": "boolean"
4856+
"type": "string"
48574857
},
48584858
{
4859-
"type": "string"
4859+
"type": "boolean"
48604860
},
48614861
{
48624862
"type": "null"
@@ -5013,10 +5013,10 @@
50135013
"title": "Use Auth Token",
50145014
"anyOf": [
50155015
{
5016-
"type": "boolean"
5016+
"type": "string"
50175017
},
50185018
{
5019-
"type": "string"
5019+
"type": "boolean"
50205020
},
50215021
{
50225022
"type": "null"
@@ -5105,10 +5105,10 @@
51055105
"title": "Use Auth Token",
51065106
"anyOf": [
51075107
{
5108-
"type": "boolean"
5108+
"type": "string"
51095109
},
51105110
{
5111-
"type": "string"
5111+
"type": "boolean"
51125112
},
51135113
{
51145114
"type": "null"
@@ -5289,10 +5289,10 @@
52895289
"title": "Use Auth Token",
52905290
"anyOf": [
52915291
{
5292-
"type": "boolean"
5292+
"type": "string"
52935293
},
52945294
{
5295-
"type": "string"
5295+
"type": "boolean"
52965296
},
52975297
{
52985298
"type": "null"
@@ -5872,10 +5872,10 @@
58725872
"title": "Use Auth Token",
58735873
"anyOf": [
58745874
{
5875-
"type": "boolean"
5875+
"type": "string"
58765876
},
58775877
{
5878-
"type": "string"
5878+
"type": "boolean"
58795879
},
58805880
{
58815881
"type": "null"
@@ -6002,10 +6002,10 @@
60026002
"title": "Use Auth Token",
60036003
"anyOf": [
60046004
{
6005-
"type": "boolean"
6005+
"type": "string"
60066006
},
60076007
{
6008-
"type": "string"
6008+
"type": "boolean"
60096009
},
60106010
{
60116011
"type": "null"
@@ -6133,10 +6133,10 @@
61336133
"title": "Use Auth Token",
61346134
"anyOf": [
61356135
{
6136-
"type": "boolean"
6136+
"type": "string"
61376137
},
61386138
{
6139-
"type": "string"
6139+
"type": "boolean"
61406140
},
61416141
{
61426142
"type": "null"
@@ -6264,10 +6264,10 @@
62646264
"title": "Use Auth Token",
62656265
"anyOf": [
62666266
{
6267-
"type": "boolean"
6267+
"type": "string"
62686268
},
62696269
{
6270-
"type": "string"
6270+
"type": "boolean"
62716271
},
62726272
{
62736273
"type": "null"
@@ -6376,10 +6376,10 @@
63766376
"title": "Use Auth Token",
63776377
"anyOf": [
63786378
{
6379-
"type": "boolean"
6379+
"type": "string"
63806380
},
63816381
{
6382-
"type": "string"
6382+
"type": "boolean"
63836383
},
63846384
{
63856385
"type": "null"

0 commit comments

Comments
 (0)