@@ -29,6 +29,9 @@ PUT my_index
29
29
},
30
30
"my_sparse_vector" : {
31
31
"type" : "sparse_vector"
32
+ },
33
+ "status" : {
34
+ "type" : "keyword"
32
35
}
33
36
}
34
37
}
@@ -37,13 +40,15 @@ PUT my_index
37
40
PUT my_index/_doc/1
38
41
{
39
42
"my_dense_vector": [0.5, 10, 6],
40
- "my_sparse_vector": {"2": 1.5, "15" : 2, "50": -1.1, "4545": 1.1}
43
+ "my_sparse_vector": {"2": 1.5, "15" : 2, "50": -1.1, "4545": 1.1},
44
+ "status" : "published"
41
45
}
42
46
43
47
PUT my_index/_doc/2
44
48
{
45
49
"my_dense_vector": [-0.5, 10, 10],
46
- "my_sparse_vector": {"2": 2.5, "10" : 1.3, "55": -2.3, "113": 1.6}
50
+ "my_sparse_vector": {"2": 2.5, "10" : 1.3, "55": -2.3, "113": 1.6},
51
+ "status" : "published"
47
52
}
48
53
49
54
--------------------------------------------------
@@ -59,22 +64,29 @@ GET my_index/_search
59
64
{
60
65
"query": {
61
66
"script_score": {
62
- "query": {
63
- "match_all": {}
67
+ "query" : {
68
+ "bool" : {
69
+ "filter" : {
70
+ "term" : {
71
+ "status" : "published" <1>
72
+ }
73
+ }
74
+ }
64
75
},
65
76
"script": {
66
- "source": "cosineSimilarity(params.query_vector, doc['my_dense_vector']) + 1.0", <1 >
77
+ "source": "cosineSimilarity(params.query_vector, doc['my_dense_vector']) + 1.0", <2 >
67
78
"params": {
68
- "query_vector": [4, 3.4, -0.2] <2 >
79
+ "query_vector": [4, 3.4, -0.2] <3 >
69
80
}
70
81
}
71
82
}
72
83
}
73
84
}
74
85
--------------------------------------------------
75
86
// CONSOLE
76
- <1> The script adds 1.0 to the cosine similarity to prevent the score from being negative.
77
- <2> To take advantage of the script optimizations, provide a query vector as a script parameter.
87
+ <1> To restrict the number of documents on which script score calculation is applied, provide a filter.
88
+ <2> The script adds 1.0 to the cosine similarity to prevent the score from being negative.
89
+ <3> To take advantage of the script optimizations, provide a query vector as a script parameter.
78
90
79
91
NOTE: If a document's dense vector field has a number of dimensions
80
92
different from the query's vector, an error will be thrown.
@@ -88,8 +100,14 @@ GET my_index/_search
88
100
{
89
101
"query": {
90
102
"script_score": {
91
- "query": {
92
- "match_all": {}
103
+ "query" : {
104
+ "bool" : {
105
+ "filter" : {
106
+ "term" : {
107
+ "status" : "published"
108
+ }
109
+ }
110
+ }
93
111
},
94
112
"script": {
95
113
"source": "cosineSimilaritySparse(params.query_vector, doc['my_sparse_vector']) + 1.0",
@@ -112,8 +130,14 @@ GET my_index/_search
112
130
{
113
131
"query": {
114
132
"script_score": {
115
- "query": {
116
- "match_all": {}
133
+ "query" : {
134
+ "bool" : {
135
+ "filter" : {
136
+ "term" : {
137
+ "status" : "published"
138
+ }
139
+ }
140
+ }
117
141
},
118
142
"script": {
119
143
"source": """
@@ -141,8 +165,14 @@ GET my_index/_search
141
165
{
142
166
"query": {
143
167
"script_score": {
144
- "query": {
145
- "match_all": {}
168
+ "query" : {
169
+ "bool" : {
170
+ "filter" : {
171
+ "term" : {
172
+ "status" : "published"
173
+ }
174
+ }
175
+ }
146
176
},
147
177
"script": {
148
178
"source": """
@@ -169,8 +199,14 @@ GET my_index/_search
169
199
{
170
200
"query": {
171
201
"script_score": {
172
- "query": {
173
- "match_all": {}
202
+ "query" : {
203
+ "bool" : {
204
+ "filter" : {
205
+ "term" : {
206
+ "status" : "published"
207
+ }
208
+ }
209
+ }
174
210
},
175
211
"script": {
176
212
"source": "1 / (1 + l1norm(params.queryVector, doc['my_dense_vector']))", <1>
@@ -202,8 +238,14 @@ GET my_index/_search
202
238
{
203
239
"query": {
204
240
"script_score": {
205
- "query": {
206
- "match_all": {}
241
+ "query" : {
242
+ "bool" : {
243
+ "filter" : {
244
+ "term" : {
245
+ "status" : "published"
246
+ }
247
+ }
248
+ }
207
249
},
208
250
"script": {
209
251
"source": "1 / (1 + l1normSparse(params.queryVector, doc['my_sparse_vector']))",
@@ -227,8 +269,14 @@ GET my_index/_search
227
269
{
228
270
"query": {
229
271
"script_score": {
230
- "query": {
231
- "match_all": {}
272
+ "query" : {
273
+ "bool" : {
274
+ "filter" : {
275
+ "term" : {
276
+ "status" : "published"
277
+ }
278
+ }
279
+ }
232
280
},
233
281
"script": {
234
282
"source": "1 / (1 + l2norm(params.queryVector, doc['my_dense_vector']))",
@@ -251,8 +299,14 @@ GET my_index/_search
251
299
{
252
300
"query": {
253
301
"script_score": {
254
- "query": {
255
- "match_all": {}
302
+ "query" : {
303
+ "bool" : {
304
+ "filter" : {
305
+ "term" : {
306
+ "status" : "published"
307
+ }
308
+ }
309
+ }
256
310
},
257
311
"script": {
258
312
"source": "1 / (1 + l2normSparse(params.queryVector, doc['my_sparse_vector']))",
0 commit comments