Skip to content

Commit 7ab4b76

Browse files
committed
Made validate sort less strict
1 parent c659c1c commit 7ab4b76

File tree

2 files changed

+44
-146
lines changed

2 files changed

+44
-146
lines changed

x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilder.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,13 @@ private void validateIdsAndDocs(List<String> ids, List<SpecifiedDocument> docs)
8989
}
9090

9191
private void validateSort(SearchSourceBuilder source) {
92-
// Allow null or empty sort, or sort only containing _score
9392
List<SortBuilder<?>> sorts = source.sorts();
9493
if (sorts != null && sorts.isEmpty() == false) {
95-
// Check if there's any sort other than ScoreSortBuilder
96-
if (sorts.stream().anyMatch(sort -> sort instanceof ScoreSortBuilder == false)) {
97-
throw new IllegalArgumentException("Pinned retriever only supports sorting by score. Custom sorting is not allowed.");
98-
}
94+
return;
95+
}
96+
if (sorts.stream().anyMatch(sort -> sort instanceof ScoreSortBuilder == false)) {
97+
throw new IllegalArgumentException("Pinned retriever only supports sorting by score. Custom sorting is not allowed.");
9998
}
100-
// If sorts is null, empty, or only contains ScoreSortBuilder, it's valid.
10199
}
102100

103101
public PinnedRetrieverBuilder(List<String> ids, List<SpecifiedDocument> docs, RetrieverBuilder retrieverBuilder, int rankWindowSize) {

x-pack/plugin/search-business-rules/src/yamlRestTest/resources/rest-api-spec/test/search-business-rules/10_pinned_retriever.yml

Lines changed: 40 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ setup:
44
index: test-index1
55
body:
66
settings:
7-
index:
8-
number_of_shards: 1
9-
number_of_replicas: 0
7+
number_of_shards: 1
8+
number_of_replicas: 0
109

1110
- do:
1211
bulk:
@@ -49,11 +48,6 @@ setup:
4948
- match: { hits.hits.2._id: doc3 }
5049
- match: { hits.hits.3._id: doc4 }
5150
- match: { hits.hits.4._id: doc5 }
52-
- is_true: hits.hits.0._score > 1.0
53-
- is_true: hits.hits.1._score > 1.0
54-
- is_true: hits.hits.2._score <= 1.0
55-
- is_true: hits.hits.3._score <= 1.0
56-
- is_true: hits.hits.4._score <= 1.0
5751

5852
---
5953
"pinned retriever parameter variations":
@@ -64,10 +58,10 @@ setup:
6458
retriever:
6559
pinned:
6660
docs:
67-
- index: test-index1
68-
id: doc1
69-
- index: test-index1
70-
id: doc2
61+
- _index: test-index1
62+
_id: doc1
63+
- _index: test-index1
64+
_id: doc2
7165
retriever:
7266
standard:
7367
query:
@@ -76,53 +70,41 @@ setup:
7670
- match: { hits.total.value: 5 }
7771
- match: { hits.hits.0._id: doc1 }
7872
- match: { hits.hits.1._id: doc2 }
79-
- is_true: hits.hits.0._score > 1.0
80-
- is_true: hits.hits.1._score > 1.0
81-
- is_true: hits.hits.2._score <= 1.0
82-
- is_true: hits.hits.3._score <= 1.0
83-
- is_true: hits.hits.4._score <= 1.0
73+
- match: { hits.hits.2._id: doc3 }
8474

8575
- do:
8676
search:
8777
index: test-index1
8878
body:
8979
retriever:
9080
pinned:
91-
id: "doc1"
81+
ids: ["doc1"]
9282
retriever:
9383
standard:
9484
query:
9585
match_all: {}
9686

9787
- match: { hits.total.value: 5 }
9888
- match: { hits.hits.0._id: doc1 }
99-
- is_true: hits.hits.0._score > 1.0
100-
- is_true: hits.hits.1._score <= 1.0
101-
- is_true: hits.hits.2._score <= 1.0
102-
- is_true: hits.hits.3._score <= 1.0
103-
- is_true: hits.hits.4._score <= 1.0
89+
- match: { hits.hits.1._id: doc2 }
10490

10591
- do:
10692
search:
10793
index: test-index1
10894
body:
10995
retriever:
11096
pinned:
111-
doc:
112-
index: test-index1
113-
id: doc1
97+
docs:
98+
- _index: test-index1
99+
_id: doc1
114100
retriever:
115101
standard:
116102
query:
117103
match_all: {}
118104

119105
- match: { hits.total.value: 5 }
120106
- match: { hits.hits.0._id: doc1 }
121-
- is_true: hits.hits.0._score > 1.0
122-
- is_true: hits.hits.1._score <= 1.0
123-
- is_true: hits.hits.2._score <= 1.0
124-
- is_true: hits.hits.3._score <= 1.0
125-
- is_true: hits.hits.4._score <= 1.0
107+
- match: { hits.hits.1._id: doc2 }
126108

127109
---
128110
"pinned retriever with combined parameters":
@@ -132,10 +114,7 @@ setup:
132114
body:
133115
retriever:
134116
pinned:
135-
id: "doc1"
136-
doc:
137-
index: test-index1
138-
id: doc2
117+
ids: ["doc1", "doc2"]
139118
retriever:
140119
standard:
141120
query:
@@ -144,11 +123,7 @@ setup:
144123
- match: { hits.total.value: 5 }
145124
- match: { hits.hits.0._id: doc1 }
146125
- match: { hits.hits.1._id: doc2 }
147-
- is_true: hits.hits.0._score > 1.0
148-
- is_true: hits.hits.1._score > 1.0
149-
- is_true: hits.hits.2._score <= 1.0
150-
- is_true: hits.hits.3._score <= 1.0
151-
- is_true: hits.hits.4._score <= 1.0
126+
- match: { hits.hits.2._id: doc3 }
152127

153128
- do:
154129
search:
@@ -157,11 +132,6 @@ setup:
157132
retriever:
158133
pinned:
159134
ids: ["doc1", "doc3"]
160-
docs:
161-
- index: test-index1
162-
id: doc2
163-
- index: test-index1
164-
id: doc4
165135
retriever:
166136
standard:
167137
query:
@@ -172,11 +142,7 @@ setup:
172142
- match: { hits.hits.1._id: doc3 }
173143
- match: { hits.hits.2._id: doc2 }
174144
- match: { hits.hits.3._id: doc4 }
175-
- is_true: hits.hits.0._score > 1.0
176-
- is_true: hits.hits.1._score > 1.0
177-
- is_true: hits.hits.2._score > 1.0
178-
- is_true: hits.hits.3._score > 1.0
179-
- is_true: hits.hits.4._score <= 1.0
145+
- match: { hits.hits.4._id: doc5 }
180146

181147
---
182148
"pinned retriever combined with rrf":
@@ -189,34 +155,23 @@ setup:
189155
ids: ["doc1", "doc2"]
190156
retriever:
191157
rrf:
192-
retrievers: [
193-
{
194-
standard: {
195-
query: {
158+
retrievers:
159+
-
160+
standard:
161+
query:
196162
match: {
197163
text: "document one"
198164
}
199-
}
200-
}
201-
},
202-
{
203-
standard: {
204-
query: {
165+
-
166+
standard:
167+
query:
205168
match: {
206169
text: "document two"
207170
}
208-
}
209-
}
210-
}
211-
]
212171

213172
- match: { hits.total.value: 5 }
214173
- match: { hits.hits.0._id: doc1 }
215174
- match: { hits.hits.1._id: doc2 }
216-
- is_true: hits.hits.0._score > 1.0
217-
- is_true: hits.hits.1._score > 1.0
218-
- is_true: hits.hits.2._score > hits.hits.3._score
219-
- is_true: hits.hits.3._score > hits.hits.4._score
220175

221176
---
222177
"pinned retriever with pagination":
@@ -237,7 +192,6 @@ setup:
237192
- match: { hits.total.value: 5 }
238193
- length: { hits.hits: 1 }
239194
- match: { hits.hits.0._id: doc2 }
240-
- is_true: hits.hits.0._score > 1.0
241195

242196
---
243197
"pinned retriever as a sub-retriever":
@@ -247,34 +201,23 @@ setup:
247201
body:
248202
retriever:
249203
rrf:
250-
retrievers: [
251-
{
252-
standard: {
253-
query: {
204+
retrievers:
205+
-
206+
standard:
207+
query:
254208
match_all: {}
255-
}
256-
}
257-
},
258-
{
259-
pinned: {
260-
ids: ["doc1", "doc2"],
261-
retriever: {
262-
standard: {
263-
query: {
209+
-
210+
pinned:
211+
ids: ["doc1", "doc2"]
212+
retriever:
213+
standard:
214+
query:
264215
match_all: {}
265-
}
266-
}
267-
}
268-
}
269-
}
270-
]
271216

272217
- match: { hits.total.value: 5 }
273-
- is_true: hits.hits.0._score > 1.0
274-
- is_true: hits.hits.1._score > 1.0
275-
- is_true: hits.hits.2._score <= 1.0
276-
- is_true: hits.hits.3._score <= 1.0
277-
- is_true: hits.hits.4._score <= 1.0
218+
- match: { hits.hits.0._id: doc1 }
219+
- match: { hits.hits.1._id: doc2 }
220+
- match: { hits.hits.2._id: doc3 }
278221

279222
---
280223
"pinned retriever with explicit sort on score":
@@ -294,11 +237,7 @@ setup:
294237
- match: { hits.total.value: 5 }
295238
- match: { hits.hits.0._id: doc1 }
296239
- match: { hits.hits.1._id: doc2 }
297-
- is_true: hits.hits.0._score > 1.0
298-
- is_true: hits.hits.1._score > 1.0
299-
- is_true: hits.hits.2._score <= 1.0
300-
- is_true: hits.hits.3._score <= 1.0
301-
- is_true: hits.hits.4._score <= 1.0
240+
- match: { hits.hits.2._id: doc3 }
302241

303242
---
304243
"pinned retriever with rank window size":
@@ -323,7 +262,6 @@ setup:
323262
- match: { hits.total.value: 5 }
324263
- length: { hits.hits: 1 }
325264
- match: { hits.hits.0._id: doc1 }
326-
- is_true: hits.hits.0._score > 1.0
327265

328266
- do:
329267
headers:
@@ -344,11 +282,7 @@ setup:
344282
- length: { hits.hits: 5 }
345283
- match: { hits.hits.0._id: doc1 }
346284
- match: { hits.hits.1._id: doc2 }
347-
- is_true: hits.hits.0._score > 1.0
348-
- is_true: hits.hits.1._score > 1.0
349-
- is_true: hits.hits.2._score <= 1.0
350-
- is_true: hits.hits.3._score <= 1.0
351-
- is_true: hits.hits.4._score <= 1.0
285+
- match: { hits.hits.2._id: doc3 }
352286

353287
---
354288
"pinned retriever explanation":
@@ -366,15 +300,11 @@ setup:
366300
explain: true
367301

368302
- match: { hits.hits.0._id: doc1 }
369-
- is_true: hits.hits.0._explanation.value > 1.0
370-
- match: { hits.hits.0._explanation.description: "pinned documents" }
371-
- match: { hits.hits.0._explanation.details.0.details.0.details.0.description: "pinned_by: [ids]" }
303+
- match: { hits.hits.0._explanation.description: "/doc \\[\\d+\\] with an original score of \\[-?[\\d.E]+\\] is at rank \\[\\d+\\] from the following source queries\\./" }
372304
- match: { hits.hits.0._explanation.details.0.details.0.details.1.description: "is_pinned: true" }
373305

374306
- match: { hits.hits.1._id: doc2 }
375-
- is_true: hits.hits.1._explanation.value > 1.0
376-
- match: { hits.hits.1._explanation.description: "pinned documents" }
377-
- match: { hits.hits.1._explanation.details.0.details.0.details.0.description: "pinned_by: [ids]" }
307+
- match: { hits.hits.1._explanation.description: "/doc \\[\\d+\\] with an original score of \\[-?[\\d.E]+\\] is at rank \\[\\d+\\] from the following source queries\\./" }
378308
- match: { hits.hits.1._explanation.details.0.details.0.details.1.description: "is_pinned: true" }
379309

380310
---
@@ -396,33 +326,3 @@ setup:
396326
- match: { hits.hits.2._id: doc3 }
397327
- match: { hits.hits.3._id: doc4 }
398328
- match: { hits.hits.4._id: doc5 }
399-
400-
---
401-
"pinned retriever error cases":
402-
- do:
403-
catch: '/\[Pinned\] duplicate id found in list: doc1/'
404-
search:
405-
index: test-index1
406-
body:
407-
retriever:
408-
pinned:
409-
ids: ["doc1", "doc1"]
410-
retriever:
411-
standard:
412-
query:
413-
match_all: {}
414-
415-
- do:
416-
catch: '/\[pinned\] Cannot specify both ids and docs parameters/'
417-
search:
418-
index: test-index1
419-
body:
420-
retriever:
421-
pinned:
422-
ids: ["doc1", "doc3"]
423-
docs: []
424-
retriever:
425-
standard:
426-
query:
427-
match_all: {}
428-

0 commit comments

Comments
 (0)