aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/federation/FederationSearcherTestCase.java
blob: 3c7e122dc7d59d55666d8d415eb6579cdb7edd72 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.federation;

import com.yahoo.component.ComponentId;
import com.yahoo.component.chain.Chain;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.federation.sourceref.SearchChainResolver;
import com.yahoo.search.query.profile.QueryProfile;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.result.Hit;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchchain.SearchChain;
import com.yahoo.search.searchchain.SearchChainRegistry;
import com.yahoo.search.searchchain.model.federation.FederationOptions;
import com.yahoo.search.test.QueryTestCase;
import com.yahoo.yolean.trace.TraceNode;
import com.yahoo.yolean.trace.TraceVisitor;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

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

/**
 * Test for federation searcher. The searcher is also tested in
 * com.yahoo.prelude.searcher.test.BlendingSearcherTestCase.
 *
 * @author Arne Bergene Fossaa
 */
public class FederationSearcherTestCase {

    static final String SOURCE1 = "source1";
    static final String SOURCE2 = "source2";

    public static class TwoSourceChecker extends TraceVisitor {
        public boolean traceFromSource1 = false;
        public boolean traceFromSource2 = false;

        @Override
        public void visit(TraceNode node) {
            if (SOURCE1.equals(node.payload())) {
                traceFromSource1 = true;
            } else if (SOURCE2.equals(node.payload())) {
                traceFromSource2 = true;
            }
        }

    }

    private FederationConfig.Builder builder;
    private SearchChainRegistry chainRegistry;

    @BeforeEach
    public void setUp() throws Exception {
        builder = new FederationConfig.Builder();
        chainRegistry = new SearchChainRegistry();
    }

    @AfterEach
    public void tearDown() {
        builder = null;
        chainRegistry = null;
    }

    private void addChained(Searcher searcher, String sourceName) {
        builder.target(new FederationConfig.Target.Builder().
                id(sourceName).
                searchChain(new FederationConfig.Target.SearchChain.Builder().
                        searchChainId(sourceName).
                        timeoutMillis(10000).
                        useByDefault(true))
        );
        chainRegistry.register(new ComponentId(sourceName),
                createSearchChain(new ComponentId(sourceName), searcher));
    }

    private Searcher createFederationSearcher() {
        return new FederationSearcher(new FederationConfig(builder), new ComponentRegistry<>());
    }

    private SearchChain createSearchChain(ComponentId chainId,Searcher searcher) {
        return new SearchChain(chainId, searcher);
    }

    @Test
    void testQueryProfileNestedReferencing() {
        addChained(new MockSearcher(), "mySource1");
        addChained(new MockSearcher(), "mySource2");
        Chain<Searcher> mainChain = new Chain<>("default", createFederationSearcher());

        QueryProfile defaultProfile = new QueryProfile("default");
        defaultProfile.set("source.mySource1.hits", "%{hits}", null);
        defaultProfile.freeze();
        Query q = new Query(QueryTestCase.httpEncode("?query=test"), defaultProfile.compile(null));

        Result result = new Execution(mainChain, Execution.Context.createContextStub(chainRegistry)).search(q);
        assertNull(result.hits().getError());
        assertEquals("source:mySource1", result.hits().get(0).getId().stringValue());
        assertEquals("source:mySource2", result.hits().get(1).getId().stringValue());
    }

    @Test
    void testTraceTwoSources() {
        Chain<Searcher> mainChain = twoTracingSources(false);

        Query q = new Query(com.yahoo.search.test.QueryTestCase.httpEncode("?query=test&traceLevel=1"));

        Execution execution = new Execution(mainChain, Execution.Context.createContextStub(chainRegistry));
        Result result = execution.search(q);
        assertNull(result.hits().getError());
        TwoSourceChecker lookForTraces = new TwoSourceChecker();
        execution.trace().accept(lookForTraces);
        assertTrue(lookForTraces.traceFromSource1);
        assertTrue(lookForTraces.traceFromSource2);
    }

    private Chain<Searcher> twoTracingSources(boolean strictContracts) {
        addChained(new Searcher() {
            @Override
            public Result search(Query query, Execution execution) {
                query.trace(SOURCE1, 1);
                return execution.search(query);
            }

        }, SOURCE1);

        addChained(new Searcher() {
            @Override
            public Result search(Query query, Execution execution) {
                query.trace(SOURCE2, 1);
                return execution.search(query);
            }

        }, SOURCE2);

        return new Chain<>("default",
                           new FederationSearcher(new FederationConfig(builder), new ComponentRegistry<>()));
    }

    @Test
    void testTraceOneSourceNoCloning() {
        Chain<Searcher> mainChain = twoTracingSources(true);

        Query q = new Query(com.yahoo.search.test.QueryTestCase.httpEncode("?query=test&traceLevel=1&sources=source1"));

        Execution execution = new Execution(mainChain, Execution.Context.createContextStub(chainRegistry));
        Result result = execution.search(q);
        assertNull(result.hits().getError());
        TwoSourceChecker lookForTraces = new TwoSourceChecker();
        execution.trace().accept(lookForTraces);
        assertTrue(lookForTraces.traceFromSource1);
        assertFalse(lookForTraces.traceFromSource2);
    }

    @Test
    void testTraceOneSourceWithCloning() {
        Chain<Searcher> mainChain = twoTracingSources(false);

        Query q = new Query(com.yahoo.search.test.QueryTestCase.httpEncode("?query=test&traceLevel=1&sources=source1"));

        Execution execution = new Execution(mainChain, Execution.Context.createContextStub(chainRegistry));
        Result result = execution.search(q);
        assertNull(result.hits().getError());
        TwoSourceChecker lookForTraces = new TwoSourceChecker();
        execution.trace().accept(lookForTraces);
        assertTrue(lookForTraces.traceFromSource1);
        assertFalse(lookForTraces.traceFromSource2);

    }

    @Test
    void testPropertyPropagation() {
        Result result = searchWithPropertyPropagation();

        assertEquals("source:mySource1", result.hits().get(0).getId().stringValue());
        assertEquals("source:mySource2", result.hits().get(1).getId().stringValue());
        assertEquals("nalle", result.hits().get(0).getQuery().getPresentation().getSummary());
        assertEquals("foo", result.hits().get(0).getQuery().properties().get("customSourceProperty"));
        assertNull(result.hits().get(1).getQuery().properties().get("customSourceProperty"));
        assertNull(result.hits().get(0).getQuery().properties().get("custom.source.property"));
        assertEquals("bar", result.hits().get(1).getQuery().properties().get("custom.source.property"));
        assertEquals(13, result.hits().get(0).getQuery().properties().get("hits"));
        assertEquals(1, result.hits().get(0).getQuery().properties().get("offset"));
        assertEquals(10, result.hits().get(1).getQuery().properties().get("hits"));
        assertEquals(0, result.hits().get(1).getQuery().properties().get("offset"));

        assertNull(result.hits().get(1).getQuery().getPresentation().getSummary());
    }

    private Result searchWithPropertyPropagation() {
        addChained(new MockSearcher(), "mySource1");
        addChained(new MockSearcher(), "mySource2");
        Chain<Searcher> mainChain = new Chain<>("default", createFederationSearcher());

        Query q = new Query(QueryTestCase.httpEncode("?query=test&source.mySource1.presentation.summary=nalle&source.mySource1.customSourceProperty=foo&source.mySource2.custom.source.property=bar&source.mySource1.hits=13&source.mySource1.offset=1"));

        Result result = new Execution(mainChain, Execution.Context.createContextStub(chainRegistry)).search(q);
        assertNull(result.hits().getError());
        return result;
    }

    @Test
    void testTopLevelHitGroupFieldPropagation() {
        addChained(new MockSearcher(), "mySource1");
        addChained(new AnotherMockSearcher(), "mySource2");
        Chain<Searcher> mainChain = new Chain<>("default", createFederationSearcher());

        Query q = new Query("?query=test");

        Result result = new Execution(mainChain, Execution.Context.createContextStub(chainRegistry)).search(q);
        assertNull(result.hits().getError());
        assertEquals("source:mySource1", result.hits().get(0).getId().stringValue());
        assertEquals("source:mySource2", result.hits().get(1).getId().stringValue());
        assertEquals(
                AnotherMockSearcher.IS_THIS_PROPAGATED,
                result.hits().get(1).getField(AnotherMockSearcher.PROPAGATION_KEY));
    }

    private static class MockSearcher extends Searcher {

        @Override
        public Result search(Query query, Execution execution) {
            String sourceName = query.properties().getString("sourceName", "unknown");
            Result result = new Result(query);
            for (int i = 1; i <= query.getHits(); i++) {
                Hit hit = new Hit(sourceName + ":" + i, 1d / i);
                hit.setSource(sourceName);
                result.hits().add(hit);
            }
            return result;
        }

    }

    private static class AnotherMockSearcher extends Searcher {

        private static final String PROPAGATION_KEY = "hello";
        private static final String IS_THIS_PROPAGATED = "is this propagated?";

        @Override
        public Result search(Query query, Execution execution) {
            final Result result = new Result(query);
            result.hits().setField(PROPAGATION_KEY, IS_THIS_PROPAGATED);
            return result;
        }
    }

    @Test
    void testProviderSelectionFromQueryProperties() {
        SearchChainRegistry registry = new SearchChainRegistry();
        registry.register(new Chain<>("provider1", new MockProvider("provider1")));
        registry.register(new Chain<>("provider2", new MockProvider("provider2")));
        registry.register(new Chain<>("default", createMultiProviderFederationSearcher()));
        assertSelects("provider1", registry);
        assertSelects("provider2", registry);
    }

    private void assertSelects(String providerName, SearchChainRegistry registry) {
        QueryProfile profile = new QueryProfile("test");
        profile.set("source.news.provider", providerName, null);
        Query query = new Query(QueryTestCase.httpEncode("?query=test&model.sources=news"), profile.compile(null));
        Result result = new Execution(registry.getComponent("default"), Execution.Context.createContextStub(registry)).search(query);
        assertEquals(1, result.hits().size());
        assertNotNull(result.hits().get(providerName + ":1"));
    }

    private FederationSearcher createMultiProviderFederationSearcher() {
        FederationOptions options = new FederationOptions();
        SearchChainResolver.Builder builder = new SearchChainResolver.Builder();

        ComponentId provider1 = new ComponentId("provider1");
        ComponentId provider2 = new ComponentId("provider2");
        ComponentId news = new ComponentId("news");
        builder.addSearchChain(provider1, options, List.of());
        builder.addSearchChain(provider2, options, List.of());
        builder.addSourceForProvider(news, provider1, provider1, true, options, List.of());
        builder.addSourceForProvider(news, provider2, provider2, false, options, List.of());

        return new FederationSearcher(new ComponentId("federation"), builder.build());
    }

    private static class MockProvider extends Searcher {

        private final String name;

        public MockProvider(String name) {
            this.name = name;
        }

        @Override
        public Result search(Query query, Execution execution) {
            Result result = new Result(query);
            result.hits().add(new Hit(name + ":1"));
            return result;
        }

    }

    private static class QueryCheckSearcher extends Searcher {

        private static final String STATUS = "status";
        public static final String FEDERATION_SEARCHER_HAS_CLONED_THE_QUERY = "FederationSearcher has cloned the query.";
        public static final String OK = "Got the correct query.";
        private final Query query;

        QueryCheckSearcher(Query query) {
            this.query = query;
        }

        @Override
        public Result search(Query query, Execution execution) {
            Result result = new Result(query);
            if (query != this.query) {
                result.hits().addError(ErrorMessage
                        .createErrorInPluginSearcher(FEDERATION_SEARCHER_HAS_CLONED_THE_QUERY));
            } else {
                final Hit h = new Hit("QueryCheckSearcher status hit");
                h.setField(STATUS, OK);
                result.hits().add(h);
            }
            return result;
        }
    }

}