summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/searcher/test/FieldCollapsingSearcherTestCase.java
blob: 361079f7595c87e0d97733b8784ce9307d319c49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher.test;

import com.yahoo.component.chain.Chain;
import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.prelude.searcher.FieldCollapsingSearcher;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.grouping.result.Group;
import com.yahoo.search.grouping.result.GroupList;
import com.yahoo.search.grouping.result.LongId;
import com.yahoo.search.grouping.result.RootId;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import com.yahoo.search.result.Relevance;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchchain.testutil.DocumentSourceSearcher;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Tests the FieldCollapsingSearcher class
 *
 * @author Steinar Knutsen
 */
public class FieldCollapsingSearcherTestCase {

    @Test
    void testFieldCollapsingWithoutHits() {
        // Set up
        Map<Searcher, Searcher> chained = new HashMap<>();

        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher();
        ZeroHitsControl checker = new ZeroHitsControl();
        chained.put(collapse, checker);

        Query q = new Query("?query=test_collapse&collapsefield=amid");
        Result r = doSearch(collapse, q, 0, 0, chained);

        assertEquals(0, r.getHitCount());
        assertNull(r.hits().getError());
        assertEquals(1, checker.queryCount);
    }

    @Test
    void testFieldCollapsingWithoutHitsHugeOffset() {
        Map<Searcher, Searcher> chained = new HashMap<>();

        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher();
        ZeroHitsControl checker = new ZeroHitsControl();
        chained.put(collapse, checker);

        Query q = new Query("?query=test_collapse&collapsefield=amid");
        Result r = doSearch(collapse, q, 1000, 0, chained);

        assertEquals(0, r.getHitCount());
        assertNull(r.hits().getError());
        assertEquals(1, checker.queryCount);
    }

    @Test
    void testFieldCollapsing() {
        Map<Searcher, Searcher> chained = new HashMap<>();

        // Set up
        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher();
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        chained.put(collapse, docsource);

        // Caveat: Collapse is set to false, because that's what the
        // collapser asks for
        Query q = new Query("?query=test_collapse&collapsefield=amid");
        // The searcher turns off collapsing further on in the chain
        q.properties().set("collapse", "0");
        Result r = new Result(q);
        r.hits().add(createHit("http://acme.org/a.html", 10, 0));
        r.hits().add(createHit("http://acme.org/b.html", 9, 0));
        r.hits().add(createHit("http://acme.org/c.html", 9, 1));
        r.hits().add(createHit("http://acme.org/d.html", 8, 1));
        r.hits().add(createHit("http://acme.org/e.html", 8, 2));
        r.hits().add(createHit("http://acme.org/f.html", 7, 2));
        r.hits().add(createHit("http://acme.org/g.html", 7, 3));
        r.hits().add(createHit("http://acme.org/h.html", 6, 3));
        r.setTotalHitCount(8);
        docsource.addResult(q, r);

        // Test basic collapsing on mid
        q = new Query("?query=test_collapse&collapsefield=amid");
        r = doSearch(collapse, q, 0, 10, chained);

        assertEquals(4, r.getHitCount());
        assertEquals(1, docsource.getQueryCount());
        assertHit("http://acme.org/a.html", 10, 0, r.hits().get(0));
        assertHit("http://acme.org/c.html", 9, 1, r.hits().get(1));
        assertHit("http://acme.org/e.html", 8, 2, r.hits().get(2));
        assertHit("http://acme.org/g.html", 7, 3, r.hits().get(3));
    }

    @Test
    void testFieldCollapsingTwoPhase() {
        // Set up
        Map<Searcher, Searcher> chained = new HashMap<>();
        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher();
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        chained.put(collapse, docsource);
        // Caveat: Collapse is set to false, because that's what the
        // collapser asks for
        Query q = new Query("?query=test_collapse&collapsefield=amid");
        // The searcher turns off collapsing further on in the chain
        q.properties().set("collapse", "0");
        Result r = new Result(q);
        r.hits().add(createHit("http://acme.org/a.html", 10, 0));
        r.hits().add(createHit("http://acme.org/b.html", 9, 0));
        r.hits().add(createHit("http://acme.org/c.html", 9, 1));
        r.hits().add(createHit("http://acme.org/d.html", 8, 1));
        r.hits().add(createHit("http://acme.org/e.html", 8, 2));
        r.hits().add(createHit("http://acme.org/f.html", 7, 2));
        r.hits().add(createHit("http://acme.org/g.html", 7, 3));
        r.hits().add(createHit("http://acme.org/h.html", 6, 3));
        r.setTotalHitCount(8);
        docsource.addResult(q, r);

        // Test basic collapsing on mid
        q = new Query("?query=test_collapse&collapsefield=amid");
        r = doSearch(collapse, q, 0, 10, chained);

        assertEquals(4, r.getHitCount());
        assertEquals(1, docsource.getQueryCount());
        assertHit("http://acme.org/a.html", 10, 0, r.hits().get(0));
        assertHit("http://acme.org/c.html", 9, 1, r.hits().get(1));
        assertHit("http://acme.org/e.html", 8, 2, r.hits().get(2));
        assertHit("http://acme.org/g.html", 7, 3, r.hits().get(3));
    }

    @Test
    void testNoCollapsingIfNotAskedTo() {
        // Set up
        Map<Searcher, Searcher> chained = new HashMap<>();
        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher();
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        chained.put(collapse, docsource);

        Query q = new Query("?query=test_collapse");
        Result r = new Result(q);
        r.hits().add(createHit("http://acme.org/a.html", 10, 0));
        r.hits().add(createHit("http://acme.org/b.html", 9, 0));
        r.hits().add(createHit("http://acme.org/c.html", 9, 1));
        r.hits().add(createHit("http://acme.org/d.html", 8, 1));
        r.hits().add(createHit("http://acme.org/e.html", 8, 2));
        r.hits().add(createHit("http://acme.org/f.html", 7, 2));
        r.hits().add(createHit("http://acme.org/g.html", 7, 3));
        r.hits().add(createHit("http://acme.org/h.html", 6, 3));
        r.setTotalHitCount(8);
        docsource.addResult(q, r);

        // Test that no collapsing occured
        q = new Query("?query=test_collapse");
        r = doSearch(collapse, q, 0, 10, chained);

        assertEquals(8, r.getHitCount());
        assertEquals(1, docsource.getQueryCount());
    }

    /**
     * Tests that collapsing many hits from one site works, and without
     * an excessive number of backend requests
     */
    @Test
    void testCollapsingLargeCollection() {
        // Set up
        Map<Searcher, Searcher> chained = new HashMap<>();
        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher(4, 2.0);
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        chained.put(collapse, docsource);

        Query q = new Query("?query=test_collapse&collapsesize=1&collapsefield=amid");
        // The searcher turns off collapsing further on in the chain
        q.properties().set("collapse", "0");
        Result r = new Result(q);
        r.hits().add(createHit("http://acme.org/a.html", 10, 0));
        r.hits().add(createHit("http://acme.org/b.html", 9, 0));
        r.hits().add(createHit("http://acme.org/c.html", 9, 0));
        r.hits().add(createHit("http://acme.org/d.html", 8, 0));
        r.hits().add(createHit("http://acme.org/e.html", 8, 0));
        r.hits().add(createHit("http://acme.org/f.html", 7, 0));
        r.hits().add(createHit("http://acme.org/g.html", 7, 0));
        r.hits().add(createHit("http://acme.org/h.html", 6, 0));
        r.hits().add(createHit("http://acme.org/i.html", 5, 1));
        r.hits().add(createHit("http://acme.org/j.html", 4, 2));
        r.setTotalHitCount(10);
        docsource.addResult(q, r);

        // Test collapsing
        q = new Query("?query=test_collapse&collapsesize=1&collapsefield=amid");
        r = doSearch(collapse, q, 0, 2, chained);

        assertEquals(2, r.getHitCount());
        assertEquals(2, docsource.getQueryCount());
        assertHit("http://acme.org/a.html", 10, 0, r.hits().get(0));
        assertHit("http://acme.org/i.html", 5, 1, r.hits().get(1));

        // Next results
        docsource.resetQueryCount();
        r = doSearch(collapse, q, 2, 2, chained);
        assertEquals(1, r.getHitCount());
        assertEquals(2, docsource.getQueryCount());
        assertHit("http://acme.org/j.html", 4, 2, r.hits().get(0));
    }

    /**
     * Tests collapsing of "messy" data
     */
    @Test
    void testCollapsingDispersedCollection() {
        // Set up
        Map<Searcher, Searcher> chained = new HashMap<>();
        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher(1, 2.0);
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        chained.put(collapse, docsource);

        Query q = new Query("?query=test_collapse&collapse=true&collapsefield=amid");
        // The searcher turns off collapsing further on in the chain
        q.properties().set("collapse", "0");
        Result r = new Result(q);
        r.hits().add(createHit("http://acme.org/a.html", 10, 1));
        r.hits().add(createHit("http://acme.org/b.html", 10, 1));
        r.hits().add(createHit("http://acme.org/c.html", 10, 0));
        r.hits().add(createHit("http://acme.org/d.html", 10, 0));
        r.hits().add(createHit("http://acme.org/e.html", 10, 0));
        r.hits().add(createHit("http://acme.org/f.html", 10, 0));
        r.hits().add(createHit("http://acme.org/g.html", 10, 0));
        r.hits().add(createHit("http://acme.org/h.html", 10, 0));
        r.hits().add(createHit("http://acme.org/i.html", 10, 0));
        r.hits().add(createHit("http://acme.org/j.html", 10, 1));
        r.setTotalHitCount(10);
        docsource.addResult(q, r);

        // Test collapsing
        q = new Query("?query=test_collapse&collapse=true&collapsefield=amid");
        r = doSearch(collapse, q, 0, 3, chained);

        assertEquals(2, r.getHitCount());
        assertHit("http://acme.org/a.html", 10, 1, r.hits().get(0));
        assertHit("http://acme.org/c.html", 10, 0, r.hits().get(1));
    }

    @Test
    void testQueryTransformAndCollapsing() {
        // Set up
        Map<Searcher, Searcher> chained = new HashMap<>();
        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher();
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        Searcher messUp = new QueryMessupSearcher();

        chained.put(collapse, messUp);
        chained.put(messUp, docsource);

        // Caveat: Collapse is set to false, because that's what the collapser asks for
        Query q = new Query("?query=%22test%20collapse%22+b&collapsefield=amid&type=all");

        // The searcher turns off collapsing further on in the chain
        q.properties().set("collapse", "0");
        Result r = new Result(q);
        r.hits().add(createHit("http://acme.org/a.html", 10, 0));
        r.hits().add(createHit("http://acme.org/b.html", 9, 0));
        r.hits().add(createHit("http://acme.org/c.html", 9, 0));
        r.hits().add(createHit("http://acme.org/d.html", 8, 0));
        r.hits().add(createHit("http://acme.org/e.html", 8, 0));
        r.hits().add(createHit("http://acme.org/f.html", 7, 0));
        r.hits().add(createHit("http://acme.org/g.html", 7, 0));
        r.hits().add(createHit("http://acme.org/h.html", 6, 1));
        r.setTotalHitCount(8);
        docsource.addResult(q, r);

        // Test basic collapsing on mid
        q = new Query("?query=%22test%20collapse%22&collapsefield=amid&type=all");
        r = doSearch(collapse, q, 0, 2, chained);

        assertEquals(2, docsource.getQueryCount());
        assertEquals(2, r.getHitCount());
        assertHit("http://acme.org/a.html", 10, 0, r.hits().get(0));
        assertHit("http://acme.org/h.html", 6, 1, r.hits().get(1));
    }

    @Test
    void testFieldCollapsingTwoPhaseSelectSummary() {
        // Set up
        Map<Searcher, Searcher> chained = new HashMap<>();
        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher();
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        chained.put(collapse, docsource);
        // Caveat: Collapse is set to false, because that's what the
        // collapser asks for
        Query q = new Query("?query=test_collapse&collapsefield=amid&summary=placeholder");
        // The searcher turns off collapsing further on in the chain
        q.properties().set("collapse", "0");
        Result r = new Result(q);
        r.hits().add(createHit("http://acme.org/a.html", 10, 0));
        r.hits().add(createHit("http://acme.org/b.html", 9, 0));
        r.hits().add(createHit("http://acme.org/c.html", 9, 1));
        r.hits().add(createHit("http://acme.org/d.html", 8, 1));
        r.hits().add(createHit("http://acme.org/e.html", 8, 2));
        r.hits().add(createHit("http://acme.org/f.html", 7, 2));
        r.hits().add(createHit("http://acme.org/g.html", 7, 3));
        r.hits().add(createHit("http://acme.org/h.html", 6, 3));
        r.setTotalHitCount(8);
        docsource.addResult(q, r);

        // Test basic collapsing on mid
        q = new Query("?query=test_collapse&collapsefield=amid&summary=placeholder");
        r = doSearch(collapse, q, 0, 10, chained);

        assertEquals(4, r.getHitCount());
        assertEquals(1, docsource.getQueryCount());
        assertTrue(r.isFilled("placeholder"));
        assertHit("http://acme.org/a.html", 10, 0, r.hits().get(0));
        assertHit("http://acme.org/c.html", 9, 1, r.hits().get(1));
        assertHit("http://acme.org/e.html", 8, 2, r.hits().get(2));
        assertHit("http://acme.org/g.html", 7, 3, r.hits().get(3));

        docsource.resetQueryCount();
        // Test basic collapsing on mid
        q = new Query("?collapse.summary=short&query=test_collapse&collapsefield=amid&summary=placeholder");
        r = doSearch(collapse, q, 0, 10, chained);

        assertEquals(4, r.getHitCount());
        assertEquals(1, docsource.getQueryCount());
        assertFalse(r.isFilled("placeholder"));
        assertTrue(r.isFilled("short"));
        assertHit("http://acme.org/a.html", 10, 0, r.hits().get(0));
        assertHit("http://acme.org/c.html", 9, 1, r.hits().get(1));
        assertHit("http://acme.org/e.html", 8, 2, r.hits().get(2));
        assertHit("http://acme.org/g.html", 7, 3, r.hits().get(3));
    }

    @Test
    void testFieldCollapsingWithGrouping() {
        // Set up
        FieldCollapsingSearcher collapse = new FieldCollapsingSearcher();
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        Chain<Searcher> chain = new Chain<>(collapse, new AddAggregationStyleGroupingResultSearcher(), docsource);

        // Caveat: Collapse is set to false, because that's what the
        // collapser asks for
        Query q = new Query("?query=test_collapse&collapsefield=amid");
        // The searcher turns off collapsing further on in the chain
        q.properties().set("collapse", "0");
        Result r = new Result(q);
        r.hits().add(createHit("http://acme.org/a.html", 10, 0));
        r.hits().add(createHit("http://acme.org/b.html", 9, 0));
        r.hits().add(createHit("http://acme.org/c.html", 9, 1));
        r.hits().add(createHit("http://acme.org/d.html", 8, 1));
        r.hits().add(createHit("http://acme.org/e.html", 8, 2));
        r.hits().add(createHit("http://acme.org/f.html", 7, 2));
        r.hits().add(createHit("http://acme.org/g.html", 7, 3));
        r.hits().add(createHit("http://acme.org/h.html", 6, 3));
        r.setTotalHitCount(8);
        docsource.addResult(q, r);

        // Test basic collapsing on mid
        Query query = new Query("?query=test_collapse&collapsefield=amid");
        Result result = new Execution(chain, Execution.Context.createContextStub()).search(query);

        // Assert that the regular hits are collapsed
        assertEquals(4 + 1, result.getHitCount());
        assertEquals(1, docsource.getQueryCount());
        assertHit("http://acme.org/a.html", 10, 0, result.hits().get(0));
        assertHit("http://acme.org/c.html", 9, 1, result.hits().get(1));
        assertHit("http://acme.org/e.html", 8, 2, result.hits().get(2));
        assertHit("http://acme.org/g.html", 7, 3, result.hits().get(3));

        // Assert that the aggregation group hierarchy is left intact
        HitGroup root = getFirstGroupIn(result.hits());
        assertNotNull(root);
        assertEquals("group:root:", root.getId().stringValue().substring(0, 11)); // The id ends by a global counter currently
        assertEquals(1, root.size());
        HitGroup groupList = (GroupList) root.get("grouplist:g1");
        assertNotNull(groupList);
        assertEquals(1, groupList.size());
        HitGroup group = (HitGroup) groupList.get("group:long:37");
        assertNotNull(group);
    }

    private Group getFirstGroupIn(HitGroup hits) {
        for (Hit h : hits)
            if (h instanceof Group) return (Group)h;
        return null;
    }

    private Result doSearch(Searcher searcher, Query query, int offset, int hits, Map<Searcher, Searcher> chained) {
        query.setOffset(offset);
        query.setHits(hits);
        return createExecution(searcher, chained).search(query);
    }

    private Chain<Searcher> chainedAsSearchChain(Searcher topOfChain, Map<Searcher, Searcher> chained) {
        List<Searcher> searchers = new ArrayList<>();
        for (Searcher current = topOfChain; current != null; current = chained.get(current))
            searchers.add(current);
        return new Chain<>(searchers);
    }

    private Execution createExecution(Searcher searcher, Map<Searcher, Searcher> chained) {
        return new Execution(chainedAsSearchChain(searcher, chained), Execution.Context.createContextStub());
    }

    /**
     * Simulates the return when grouping is used for aggregation purposes and there is a plain hit list in addition:
     * The returned result contains both regular hits at the top level (from non-grouping)
     * and groups contained aggregation information.
     */
    private static class AddAggregationStyleGroupingResultSearcher extends Searcher {

        @Override
        public Result search(Query query, Execution execution) {
            Result r = execution.search(query);
            r.hits().add(createAggregationGroup("g1"));
            return r;
        }

        private HitGroup createAggregationGroup(String label) {
            Group root = new Group(new RootId(0), new Relevance(1));
            GroupList groupList = new GroupList(label);
            root.add(groupList);
            Group value = new Group(new LongId(37L), new Relevance(2.11));
            groupList.add(value);
            return root;
        }
    }

    private FastHit createHit(String uri,int relevancy,int mid) {
        FastHit hit = new FastHit(uri,relevancy);
        hit.setField("amid", String.valueOf(mid));
        return hit;
    }

    private void assertHit(String uri,int relevancy,int mid,Hit hit) {
        assertEquals(uri,hit.getId().toString());
        assertEquals(relevancy, ((int) hit.getRelevance().getScore()));
        assertEquals(mid,Integer.parseInt((String) hit.getField("amid")));
    }

    private static class ZeroHitsControl extends com.yahoo.search.Searcher {

        public int queryCount = 0;

        @Override
        public Result search(Query query, Execution execution) {
            ++queryCount;
            if (query.getHits() == 0) {
                return new Result(query);
            } else {
                return new Result(query, ErrorMessage.createIllegalQuery("Did not request zero hits."));
            }
        }
    }

    public static class QueryMessupSearcher extends Searcher {

        @Override
        public Result search(com.yahoo.search.Query query, Execution execution) {
            AndItem a = new AndItem();
            a.addItem(query.getModel().getQueryTree().getRoot());
            WordItem item = new WordItem("b");
            item.setFromQuery(true);
            a.addItem(item);

            query.getModel().getQueryTree().setRoot(a);

            return execution.search(query);
        }

    }

}