summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/fastsearch/PartialFillTestCase.java
blob: 7760e204d4bee2aa9a80bf8a03f9b3dbe90de22e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;

import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.result.ErrorHit;
import com.yahoo.search.result.ErrorMessage;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

/**
 * @author havardpe
 */
public class PartialFillTestCase {

    public static class FS4 extends VespaBackend {
        public List<Result> history = new ArrayList<>();
        protected Result doSearch2(String schema, Query query) {
            return new Result(query);
        }
        protected void doPartialFill(Result result, String summaryClass) {
            history.add(result);
        }
    }

    public static class BadFS4 extends VespaBackend {
        protected Result doSearch2(String schema, Query query) {
            return new Result(query);
        }
        protected void doPartialFill(Result result, String summaryClass) {
            if (result.hits().getErrorHit() == null) {
                result.hits().addError(ErrorMessage.createUnspecifiedError("error"));
            }
        }
    }

    @Test
    void testPartitioning() {
        FS4 fs4 = new FS4();
        Query a = new Query("/?query=foo");
        Query b = new Query("/?query=bar");
        Query c = new Query("/?query=foo"); // equal to a
        Result r = new Result(new Query("/?query=ignorethis"));
        for (int i = 0; i < 7; i++) {
            FastHit h = new FastHit();
            h.setQuery(a);
            h.setFillable();
            r.hits().add(h);
        }
        for (int i = 0; i < 5; i++) {
            FastHit h = new FastHit();
            h.setQuery(b);
            h.setFillable();
            r.hits().add(h);
        }
        for (int i = 0; i < 3; i++) {
            FastHit h = new FastHit();
            h.setQuery(c);
            h.setFillable();
            r.hits().add(h);
        }
        for (int i = 0; i < 2; i++) {
            FastHit h = new FastHit();
            // no query assigned
            h.setFillable();
            r.hits().add(h);
        }
        for (int i = 0; i < 5; i++) {
            FastHit h = new FastHit();
            // not fillable
            h.setQuery(a);
            r.hits().add(h);
        }
        for (int i = 0; i < 5; i++) {
            FastHit h = new FastHit();
            // already filled
            h.setQuery(a);
            h.setFilled("default");
            r.hits().add(h);
        }
        doFill(fs4, r, "default");
        assertNull(r.hits().getErrorHit());
        assertEquals(4, fs4.history.size());
        assertEquals(a, fs4.history.get(0).getQuery());
        assertEquals(7, fs4.history.get(0).getHitCount());
        assertEquals(b, fs4.history.get(1).getQuery());
        assertEquals(5, fs4.history.get(1).getHitCount());
        assertEquals(c, fs4.history.get(2).getQuery());
        assertEquals(3, fs4.history.get(2).getHitCount());
        assertEquals(r.getQuery(), fs4.history.get(3).getQuery());
        assertEquals(2, fs4.history.get(3).getHitCount());
    }

    @Test
    void testMergeErrors() {
        BadFS4 fs4 = new BadFS4();
        Query a = new Query("/?query=foo");
        Query b = new Query("/?query=bar");
        Result r = new Result(new Query("/?query=ignorethis"));
        {
            FastHit h = new FastHit();
            h.setQuery(a);
            h.setFillable();
            r.hits().add(h);
        }
        {
            FastHit h = new FastHit();
            h.setQuery(b);
            h.setFillable();
            r.hits().add(h);
        }
        doFill(fs4, r, "default");
        ErrorHit eh = r.hits().getErrorHit();
        assertNotNull(eh);
        ErrorMessage exp_sub = ErrorMessage.createUnspecifiedError("error");
        int n = 0;
        for (Iterator<? extends com.yahoo.search.result.ErrorMessage> i = eh.errorIterator(); i.hasNext(); ) {
            com.yahoo.search.result.ErrorMessage error = i.next();
            switch (n) {
                case 0:
                case 1:
                    assertEquals(exp_sub, error);
                    break;
                default:
                    fail();
            }
            n++;
        }
    }

    private void doFill(VespaBackend searcher, Result result, String summaryClass) {
        searcher.fill(result, summaryClass);
    }

}