Skip to content

Commit de8b9f3

Browse files
Add filters in exampls of vector functions (elastic#45327)
1 parent fc7e525 commit de8b9f3

File tree

1 file changed

+76
-22
lines changed

1 file changed

+76
-22
lines changed

docs/reference/vectors/vector-functions.asciidoc

+76-22
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ PUT my_index
2929
},
3030
"my_sparse_vector" : {
3131
"type" : "sparse_vector"
32+
},
33+
"status" : {
34+
"type" : "keyword"
3235
}
3336
}
3437
}
@@ -37,13 +40,15 @@ PUT my_index
3740
PUT my_index/_doc/1
3841
{
3942
"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"
4145
}
4246
4347
PUT my_index/_doc/2
4448
{
4549
"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"
4752
}
4853
4954
--------------------------------------------------
@@ -59,22 +64,29 @@ GET my_index/_search
5964
{
6065
"query": {
6166
"script_score": {
62-
"query": {
63-
"match_all": {}
67+
"query" : {
68+
"bool" : {
69+
"filter" : {
70+
"term" : {
71+
"status" : "published" <1>
72+
}
73+
}
74+
}
6475
},
6576
"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>
6778
"params": {
68-
"query_vector": [4, 3.4, -0.2] <2>
79+
"query_vector": [4, 3.4, -0.2] <3>
6980
}
7081
}
7182
}
7283
}
7384
}
7485
--------------------------------------------------
7586
// 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.
7890

7991
NOTE: If a document's dense vector field has a number of dimensions
8092
different from the query's vector, an error will be thrown.
@@ -88,8 +100,14 @@ GET my_index/_search
88100
{
89101
"query": {
90102
"script_score": {
91-
"query": {
92-
"match_all": {}
103+
"query" : {
104+
"bool" : {
105+
"filter" : {
106+
"term" : {
107+
"status" : "published"
108+
}
109+
}
110+
}
93111
},
94112
"script": {
95113
"source": "cosineSimilaritySparse(params.query_vector, doc['my_sparse_vector']) + 1.0",
@@ -112,8 +130,14 @@ GET my_index/_search
112130
{
113131
"query": {
114132
"script_score": {
115-
"query": {
116-
"match_all": {}
133+
"query" : {
134+
"bool" : {
135+
"filter" : {
136+
"term" : {
137+
"status" : "published"
138+
}
139+
}
140+
}
117141
},
118142
"script": {
119143
"source": """
@@ -141,8 +165,14 @@ GET my_index/_search
141165
{
142166
"query": {
143167
"script_score": {
144-
"query": {
145-
"match_all": {}
168+
"query" : {
169+
"bool" : {
170+
"filter" : {
171+
"term" : {
172+
"status" : "published"
173+
}
174+
}
175+
}
146176
},
147177
"script": {
148178
"source": """
@@ -169,8 +199,14 @@ GET my_index/_search
169199
{
170200
"query": {
171201
"script_score": {
172-
"query": {
173-
"match_all": {}
202+
"query" : {
203+
"bool" : {
204+
"filter" : {
205+
"term" : {
206+
"status" : "published"
207+
}
208+
}
209+
}
174210
},
175211
"script": {
176212
"source": "1 / (1 + l1norm(params.queryVector, doc['my_dense_vector']))", <1>
@@ -202,8 +238,14 @@ GET my_index/_search
202238
{
203239
"query": {
204240
"script_score": {
205-
"query": {
206-
"match_all": {}
241+
"query" : {
242+
"bool" : {
243+
"filter" : {
244+
"term" : {
245+
"status" : "published"
246+
}
247+
}
248+
}
207249
},
208250
"script": {
209251
"source": "1 / (1 + l1normSparse(params.queryVector, doc['my_sparse_vector']))",
@@ -227,8 +269,14 @@ GET my_index/_search
227269
{
228270
"query": {
229271
"script_score": {
230-
"query": {
231-
"match_all": {}
272+
"query" : {
273+
"bool" : {
274+
"filter" : {
275+
"term" : {
276+
"status" : "published"
277+
}
278+
}
279+
}
232280
},
233281
"script": {
234282
"source": "1 / (1 + l2norm(params.queryVector, doc['my_dense_vector']))",
@@ -251,8 +299,14 @@ GET my_index/_search
251299
{
252300
"query": {
253301
"script_score": {
254-
"query": {
255-
"match_all": {}
302+
"query" : {
303+
"bool" : {
304+
"filter" : {
305+
"term" : {
306+
"status" : "published"
307+
}
308+
}
309+
}
256310
},
257311
"script": {
258312
"source": "1 / (1 + l2normSparse(params.queryVector, doc['my_sparse_vector']))",

0 commit comments

Comments
 (0)