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

import com.yahoo.container.QrSearchersConfig;
import com.yahoo.container.handler.VipStatus;
import com.yahoo.container.protect.Error;
import com.yahoo.prelude.fastsearch.ClusterParams;
import com.yahoo.prelude.fastsearch.DocumentdbInfoConfig;
import com.yahoo.prelude.fastsearch.FastSearcher;
import com.yahoo.prelude.fastsearch.SummaryParameters;
import com.yahoo.prelude.fastsearch.VespaBackEndSearcher;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.dispatch.MockDispatcher;
import com.yahoo.search.dispatch.rpc.RpcResourcePool;
import com.yahoo.search.dispatch.searchcluster.Node;
import com.yahoo.search.grouping.GroupingRequest;
import com.yahoo.search.grouping.request.AllOperation;
import com.yahoo.search.grouping.request.EachOperation;
import com.yahoo.search.grouping.request.GroupingOperation;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.schema.DocumentSummary;
import com.yahoo.search.schema.RankProfile;
import com.yahoo.search.schema.Schema;
import com.yahoo.search.schema.SchemaInfo;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

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


/**
 * Tests the Fast searcher
 *
 * @author bratseth
 */
public class FastSearcherTestCase {
    private static final String SCHEMA = "test";
    private static final String CLUSTER = "test";

    @Test
    void testNullQuery() {
        Logger.getLogger(FastSearcher.class.getName()).setLevel(Level.ALL);
        FastSearcher fastSearcher = new FastSearcher("container.0",
                MockDispatcher.create(List.of()),
                new SummaryParameters(null),
                new ClusterParams("testhittype"),
                documentdbInfoConfig(SCHEMA),
                schemaInfo(SCHEMA));

        String query = "?junkparam=ignored";
        Result result = doSearch(fastSearcher, new Query(query), 0, 10);
        ErrorMessage message = result.hits().getError();

        assertNotNull(message, "Got error");
        assertEquals("Null query", message.getMessage());
        assertEquals(query, message.getDetailedMessage());
        assertEquals(Error.NULL_QUERY.code, message.getCode());
    }

    private Result doSearch(VespaBackEndSearcher searcher, Query query, int offset, int hits) {
        query.setOffset(offset);
        query.setHits(hits);
        return searcher.search(SCHEMA, query);
    }

    @Test
    void testSinglePassGroupingIsForcedWithSingleNodeGroups() {
        FastSearcher fastSearcher = new FastSearcher("container.0",
                MockDispatcher.create(List.of(new Node(CLUSTER, 0, "host0", 0))),
                new SummaryParameters(null),
                new ClusterParams("testhittype"),
                documentdbInfoConfig(SCHEMA),
                schemaInfo(SCHEMA));
        Query q = new Query("?query=foo");
        GroupingRequest request1 = GroupingRequest.newInstance(q);
        request1.setRootOperation(new AllOperation());

        GroupingRequest request2 = GroupingRequest.newInstance(q);
        AllOperation all = new AllOperation();
        all.addChild(new EachOperation());
        all.addChild(new EachOperation());
        request2.setRootOperation(all);

        assertForceSinglePassIs(false, q);
        fastSearcher.search(SCHEMA, q);
        assertForceSinglePassIs(true, q);
    }

    @Test
    void testRankProfileValidation() {
        FastSearcher fastSearcher = new FastSearcher("container.0",
                MockDispatcher.create(List.of(new Node(CLUSTER, 0, "host0", 0))),
                new SummaryParameters(null),
                new ClusterParams("testhittype"),
                documentdbInfoConfig(SCHEMA),
                schemaInfo(SCHEMA));
        assertFalse(searchError("?query=q", fastSearcher).contains("does not contain requested rank profile"));
        assertFalse(searchError("?query=q&ranking.profile=default", fastSearcher).contains("does not contain requested rank profile"));
        assertTrue(searchError("?query=q&ranking.profile=nosuch", fastSearcher).contains("does not contain requested rank profile"));
    }

    @Test
    void testSummaryNeedsQuery() {
        var documentDb = new DocumentdbInfoConfig(new DocumentdbInfoConfig.Builder().documentdb(new DocumentdbInfoConfig.Documentdb.Builder().name(SCHEMA)));
        var schema = new Schema.Builder(SCHEMA)
                .add(new DocumentSummary.Builder("default").build())
                .add(new RankProfile.Builder("default").setHasRankFeatures(false)
                        .setHasSummaryFeatures(false)
                        .build());
        FastSearcher backend = new FastSearcher("container.0",
                MockDispatcher.create(Collections.singletonList(new Node(CLUSTER, 0, "host0", 0))),
                new SummaryParameters(null),
                new ClusterParams("testhittype"),
                documentDb,
                new SchemaInfo(List.of(schema.build()), List.of()));
        Query q = new Query("?query=foo");
        Result result = doSearch(backend, q, 0, 10);
        assertFalse(backend.summaryNeedsQuery(q));

        q = new Query("?query=select+*+from+source+where+title+contains+%22foobar%22+and++geoLocation%28myfieldname%2C+63.5%2C+10.5%2C+%22999+km%22%29%3B");
        q.getModel().setType(Query.Type.YQL);
        result = doSearch(backend, q, 0, 10);
        assertTrue(backend.summaryNeedsQuery(q));
    }

    @Test
    void testSinglePassGroupingIsNotForcedWithSingleNodeGroups() {
        MockDispatcher dispatcher = MockDispatcher.create(List.of(new Node(CLUSTER, 0, "host0", 0), new Node(CLUSTER, 2, "host1", 0)));

        FastSearcher fastSearcher = new FastSearcher("container.0",
                dispatcher,
                new SummaryParameters(null),
                new ClusterParams("testhittype"),
                documentdbInfoConfig(SCHEMA),
                schemaInfo(SCHEMA));
        Query q = new Query("?query=foo");
        GroupingRequest request1 = GroupingRequest.newInstance(q);
        request1.setRootOperation(new AllOperation());

        GroupingRequest request2 = GroupingRequest.newInstance(q);
        AllOperation all = new AllOperation();
        all.addChild(new EachOperation());
        all.addChild(new EachOperation());
        request2.setRootOperation(all);

        assertForceSinglePassIs(false, q);
        fastSearcher.search(SCHEMA, q);
        assertForceSinglePassIs(false, q);
    }

    private void assertForceSinglePassIs(boolean expected, Query query) {
        for (GroupingRequest request : query.getSelect().getGrouping())
            assertForceSinglePassIs(expected, request.getRootOperation());
    }

    private void assertForceSinglePassIs(boolean expected, GroupingOperation operation) {
        assertEquals(expected, operation.getForceSinglePass(), "Force single pass is " + expected + " in " + operation);
        for (GroupingOperation child : operation.getChildren())
            assertForceSinglePassIs(expected, child);
    }

    @Test
    void testDispatchReconfig() {
        String clusterName = "a";
        var b = new QrSearchersConfig.Builder();
        var searchClusterB = new QrSearchersConfig.Searchcluster.Builder();
        searchClusterB.name(clusterName);
        b.searchcluster(searchClusterB);
        VipStatus vipStatus = new VipStatus(b.build());
        List<Node> nodes_1 = List.of(new Node(CLUSTER, 0, "host0", 0));
        RpcResourcePool rpcPool_1 = new RpcResourcePool(MockDispatcher.toDispatchConfig(), MockDispatcher.toNodesConfig(nodes_1));
        MockDispatcher dispatch_1 = MockDispatcher.create(nodes_1, rpcPool_1, vipStatus);
        dispatch_1.clusterMonitor.shutdown();
        vipStatus.addToRotation(clusterName);
        assertTrue(vipStatus.isInRotation());
        dispatch_1.deconstruct();
        assertTrue(vipStatus.isInRotation()); //Verify that deconstruct does not touch vipstatus
    }

    private String searchError(String query, VespaBackEndSearcher searcher) {
        return search(query, searcher).hits().getError().getDetailedMessage();
    }

    private Result search(String query, VespaBackEndSearcher searcher) {
        return searcher.search(SCHEMA, new Query(query));
    }

    private DocumentdbInfoConfig documentdbInfoConfig(String schemaName) {
        var db = new DocumentdbInfoConfig.Documentdb.Builder().name(schemaName);
        return new DocumentdbInfoConfig.Builder().documentdb(db).build();
    }

    private SchemaInfo schemaInfo(String schemaName) {
        var schema = new Schema.Builder(schemaName);
        schema.add(new RankProfile.Builder("default").build());
        return new SchemaInfo(List.of(schema.build()),  List.of());
    }

}