aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/SearchBuilder.java
blob: 762c0fec8386ebdbdbd52b149bbfa71100a570d1 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;

import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.io.IOUtils;
import com.yahoo.io.reader.NamedReader;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.searchdefinition.derived.SearchOrderer;
import com.yahoo.searchdefinition.document.SDDocumentType;
import com.yahoo.searchdefinition.parser.ParseException;
import com.yahoo.searchdefinition.parser.SDParser;
import com.yahoo.searchdefinition.parser.SimpleCharStream;
import com.yahoo.searchdefinition.parser.TokenMgrError;
import com.yahoo.searchdefinition.processing.Processing;
import com.yahoo.vespa.documentmodel.DocumentModel;
import com.yahoo.vespa.model.container.search.QueryProfiles;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Helper class for importing {@link Search} objects in an unambiguous way. The pattern for using this is to 1) Import
 * all available search definitions, using the importXXX() methods, 2) provide the available rank types and rank
 * expressions, using the setRankXXX() methods, 3) invoke the {@link #build()} method, and 4) retrieve the built
 * search objects using the {@link #getSearch(String)} method.
 */
// TODO: This should be cleaned up and more or maybe completely taken over by MockApplicationPackage
public class SearchBuilder {

    private final DocumentTypeManager docTypeMgr = new DocumentTypeManager();
    private List<Search> searchList = new LinkedList<>();
    private ApplicationPackage app = null;
    private boolean isBuilt = false;
    private DocumentModel model = new DocumentModel();
    private final RankProfileRegistry rankProfileRegistry;
    private final QueryProfileRegistry queryProfileRegistry;

    /** For testing only */
    public SearchBuilder() {
        this(MockApplicationPackage.createEmpty(), new RankProfileRegistry(), new QueryProfileRegistry());
    }

    /** For testing only */
    public SearchBuilder(ApplicationPackage app) {
        this(app, new RankProfileRegistry(), new QueryProfileRegistry());
    }

    /** For testing only */
    public SearchBuilder(RankProfileRegistry rankProfileRegistry) {
        this(MockApplicationPackage.createEmpty(), rankProfileRegistry, new QueryProfileRegistry());
    }

    /** For testing only */
    public SearchBuilder(RankProfileRegistry rankProfileRegistry, QueryProfileRegistry queryProfileRegistry) {
        this(MockApplicationPackage.createEmpty(), rankProfileRegistry, queryProfileRegistry);
    }

    public SearchBuilder(ApplicationPackage app,
                         RankProfileRegistry rankProfileRegistry,
                         QueryProfileRegistry queryProfileRegistry) {
        this.app = app;
        this.rankProfileRegistry = rankProfileRegistry;
        this.queryProfileRegistry = queryProfileRegistry;
    }

    /**
     * Import search definition.
     *
     * @param fileName The name of the file to import.
     * @param deployLogger Logger for deploy messages.
     * @return The name of the imported object.
     * @throws IOException    Thrown if the file can not be read for some reason.
     * @throws ParseException Thrown if the file does not contain a valid search definition.       ```
     */
    public String importFile(String fileName, DeployLogger deployLogger) throws IOException, ParseException {
        File file = new File(fileName);
        return importString(IOUtils.readFile(file), file.getAbsoluteFile().getParent(), deployLogger);
    }

    /**
     * Import search definition.
     *
     * @param fileName The name of the file to import.
     * @return The name of the imported object.
     * @throws IOException    Thrown if the file can not be read for some reason.
     * @throws ParseException Thrown if the file does not contain a valid search definition.
     */
    public String importFile(String fileName) throws IOException, ParseException {
        return importFile(fileName, new BaseDeployLogger());
    }
    public String importFile(Path file) throws IOException, ParseException {
        return importFile(file.toString(), new BaseDeployLogger());
    }

    /**
     * Reads and parses the search definition string provided by the given reader. Once all search definitions have been
     * imported, call {@link #build()}.
     *
     * @param reader       The reader whose content to import.
     * @param searchDefDir The path to use when resolving file references.
     * @return The name of the imported object.
     * @throws ParseException Thrown if the file does not contain a valid search definition.
     */
    public String importReader(NamedReader reader, String searchDefDir, DeployLogger deployLogger) throws IOException, ParseException {
        return importString(IOUtils.readAll(reader), searchDefDir, deployLogger);
    }

    /**
     * See #{@link #importReader}
     *
     * Convenience, should only be used for testing as logs will be swallowed.
     */
    public String importReader(NamedReader reader, String searchDefDir) throws IOException, ParseException {
        return importString(IOUtils.readAll(reader), searchDefDir, new BaseDeployLogger());
    }

    /**
     * Import search definition.
     *
     * @param str the string to parse.
     * @return the name of the imported object.
     * @throws ParseException thrown if the file does not contain a valid search definition.
     */
    public String importString(String str) throws ParseException {
        return importString(str, null, new BaseDeployLogger());
    }

    /**
     * Import search definition.
     *
     * @param str the string to parse.
     * @return the name of the imported object.
     * @throws ParseException thrown if the file does not contain a valid search definition.
     */
    public String importString(String str, DeployLogger logger) throws ParseException {
        return importString(str, null, logger);
    }

    private String importString(String str, String searchDefDir, DeployLogger deployLogger) throws ParseException {
        Search search;
        SimpleCharStream stream = new SimpleCharStream(str);
        try {
            search = new SDParser(stream, deployLogger, app, rankProfileRegistry).search(docTypeMgr, searchDefDir);
        } catch (TokenMgrError e) {
            throw new ParseException("Unknown symbol: " + e.getMessage());
        } catch (ParseException pe) {
            throw new ParseException(stream.formatException(pe.getMessage()));
        }
        return importRawSearch(search);
    }

    /**
     * Registers the given search object to the internal list of objects to be processed during {@link #build()}. A
     * {@link Search} object is considered to be "raw" if it has not already been processed. This is the case for most
     * programmatically constructed search objects used in unit tests.
     *
     * @param rawSearch The object to import.
     * @return The name of the imported object.
     * @throws IllegalArgumentException Thrown if the given search object has already been processed.
     */
    public String importRawSearch(Search rawSearch) {
        if (rawSearch.getName() == null) {
            throw new IllegalArgumentException("Search has no name.");
        }
        String rawName = rawSearch.getName();
        if (rawSearch.isProcessed()) {
            throw new IllegalArgumentException("A search definition with a search section called '" + rawName +
                                               "' has already been processed.");
        }
        for (Search search : searchList) {
            if (rawName.equals(search.getName())) {
                throw new IllegalArgumentException("A search definition with a search section called '" + rawName +
                                                   "' has already been added.");
            }
        }
        searchList.add(rawSearch);
        return rawName;
    }

    /**
     * Only for testing.
     *
     * Processes and finalizes the imported search definitions so that they become available through the {@link
     * #getSearch(String)} method.
     *
     * @throws IllegalStateException Thrown if this method has already been called.
     */
    public void build() {
        build(new BaseDeployLogger(), new QueryProfiles());
    }

    public void build(DeployLogger logger) {
        build(logger, new QueryProfiles());
    }

    /**
     * Processes and finalizes the imported search definitions so that they become available through the {@link
     * #getSearch(String)} method.
     *
     * @throws IllegalStateException Thrown if this method has already been called.
     * @param deployLogger The logger to use during build
     * @param queryProfiles The query profiles contained in the application this search is part of.
     */
    public void build(DeployLogger deployLogger, QueryProfiles queryProfiles) {
        if (isBuilt) {
            throw new IllegalStateException("Searches already built.");
        }
        List<Search> built = new ArrayList<>();
        List<SDDocumentType> sdocs = new ArrayList<>();
        sdocs.add(SDDocumentType.VESPA_DOCUMENT);
        for (Search search : searchList) {
            if (search.hasDocument()) {
                sdocs.add(search.getDocument());
            }
        }
        SDDocumentTypeOrderer orderer = new SDDocumentTypeOrderer(sdocs, deployLogger);
        orderer.process();
        for (SDDocumentType sdoc : orderer.getOrdered()) {
            new FieldOperationApplierForStructs().process(sdoc);
            new FieldOperationApplier().process(sdoc);
        }

        DocumentReferenceResolver resolver = new DocumentReferenceResolver(searchList);
        sdocs.forEach(resolver::resolveReferences);

        DocumentGraphValidator validator = new DocumentGraphValidator();
        validator.validateDocumentGraph(sdocs);

        DocumentModelBuilder builder = new DocumentModelBuilder(model);
        for (Search search : new SearchOrderer().order(searchList)) {
            new FieldOperationApplierForSearch().process(search);
            // These two needed for a couple of old unit tests, ideally these are just read from app
            process(search, deployLogger, queryProfiles);
            built.add(search);
        }
        builder.addToModel(searchList);
        if ( ! builder.valid() ) {
            throw new IllegalArgumentException("Impossible to build a correct model.");
        }
        searchList = built;
        isBuilt = true;
    }

    /**
     * Processes and returns the given {@link Search} object. This method has been factored out of the {@link
     * #build()} method so that subclasses can choose not to build anything.
     *
     * @param search The object to build.
     */
    protected void process(Search search, DeployLogger deployLogger, QueryProfiles queryProfiles) {
        Processing.process(search, deployLogger, rankProfileRegistry, queryProfiles);
    }

    /**
     * Convenience method to call {@link #getSearch(String)} when there is only a single {@link Search} object
     * built. This method will never return null.
     *
     * @return The build object.
     * @throws IllegalStateException Thrown if there is not exactly one search.
     */
    public Search getSearch() {
        if ( ! isBuilt)  throw new IllegalStateException("Searches not built.");
        if (searchList.size() != 1)
            throw new IllegalStateException("This call only works if we have 1 search definition. Search definitions: " + searchList);

        return searchList.get(0);
    }

    public DocumentModel getModel() {
        return model;
    }

    /**
     * Returns the built {@link Search} object that has the given name. If the name is unknown, this method will simply
     * return null.
     *
     * @param name the name of the search definition to return,
     *             or null to return the only one or throw an exception if there are multiple to choose from
     * @return the built object, or null if none with this name
     * @throws IllegalStateException if {@link #build()} has not been called.
     */
    public Search getSearch(String name) {
        if ( ! isBuilt)  throw new IllegalStateException("Searches not built.");
        if (name == null) return getSearch();

        for (Search search : searchList)
            if (search.getName().equals(name)) return search;
        return null;
    }

    /**
     * Convenience method to return a list of all built {@link Search} objects.
     *
     * @return The list of built searches.
     */
    public List<Search> getSearchList() {
        return new ArrayList<>(searchList);
    }

    /**
     * Convenience factory method to import and build a {@link Search} object from a string.
     *
     * @param sd   The string to build from.
     * @return The built {@link SearchBuilder} object.
     * @throws ParseException Thrown if there was a problem parsing the string.
     */
    public static SearchBuilder createFromString(String sd) throws ParseException {
        SearchBuilder builder = new SearchBuilder(MockApplicationPackage.createEmpty());
        builder.importString(sd);
        builder.build();
        return builder;
    }

    /**
     * Convenience factory method to import and build a {@link Search} object from a file. Only for testing.
     *
     * @param fileName the file to build from
     * @return the built {@link SearchBuilder} object
     * @throws IOException    if there was a problem reading the file.
     * @throws ParseException if there was a problem parsing the file content.
     */
    public static SearchBuilder createFromFile(String fileName) throws IOException, ParseException {
        return createFromFile(fileName, new BaseDeployLogger(), new RankProfileRegistry(), new QueryProfileRegistry());
    }

    /**
     * Convenience factory method to import and build a {@link Search} object from a file.
     *
     * @param fileName the file to build from.
     * @param deployLogger logger for deploy messages.
     * @param rankProfileRegistry registry for rank profiles.
     * @return the built {@link SearchBuilder} object.
     * @throws IOException    if there was a problem reading the file.
     * @throws ParseException if there was a problem parsing the file content.
     */
    public static SearchBuilder createFromFile(String fileName,
                                               DeployLogger deployLogger,
                                               RankProfileRegistry rankProfileRegistry,
                                               QueryProfileRegistry queryprofileRegistry)
            throws IOException, ParseException {
        SearchBuilder builder = new SearchBuilder(MockApplicationPackage.createEmpty(),
                                                  rankProfileRegistry,
                                                  queryprofileRegistry);
        builder.importFile(fileName);
        builder.build(deployLogger, new QueryProfiles());
        return builder;
    }

    public static SearchBuilder createFromDirectory(String dir) throws IOException, ParseException {
        return createFromDirectory(dir, new RankProfileRegistry(), new QueryProfileRegistry());
    }
    public static SearchBuilder createFromDirectory(String dir,
                                                    RankProfileRegistry rankProfileRegistry,
                                                    QueryProfileRegistry queryProfileRegistry) throws IOException, ParseException {
        SearchBuilder builder = new SearchBuilder(MockApplicationPackage.fromSearchDefinitionDirectory(dir),
                                                  rankProfileRegistry,
                                                  queryProfileRegistry);
        for (Iterator<Path> i = Files.list(new File(dir).toPath()).filter(p -> p.getFileName().toString().endsWith(".sd")).iterator(); i.hasNext(); ) {
            builder.importFile(i.next());
        }
        builder.build(new BaseDeployLogger(), new QueryProfiles());
        return builder;
    }

    // TODO: The build methods below just call the create methods above - remove

    /**
     * Convenience factory method to import and build a {@link Search} object from a file. Only for testing.
     *
     * @param fileName The file to build from.
     * @return The built {@link Search} object.
     * @throws IOException    Thrown if there was a problem reading the file.
     * @throws ParseException Thrown if there was a problem parsing the file content.
     */
    public static Search buildFromFile(String fileName) throws IOException, ParseException {
        return buildFromFile(fileName, new BaseDeployLogger(), new RankProfileRegistry(), new QueryProfileRegistry());
    }

    /**
     * Convenience factory method to import and build a {@link Search} object from a file.
     *
     * @param fileName The file to build from.
     * @param rankProfileRegistry Registry for rank profiles.
     * @return The built {@link Search} object.
     * @throws IOException    Thrown if there was a problem reading the file.
     * @throws ParseException Thrown if there was a problem parsing the file content.
     */
    public static Search buildFromFile(String fileName,
                                       RankProfileRegistry rankProfileRegistry,
                                       QueryProfileRegistry queryProfileRegistry)
            throws IOException, ParseException {
        return buildFromFile(fileName, new BaseDeployLogger(), rankProfileRegistry, queryProfileRegistry);
    }

    /**
     * Convenience factory method to import and build a {@link Search} object from a file.
     *
     * @param fileName The file to build from.
     * @param deployLogger Logger for deploy messages.
     * @param rankProfileRegistry Registry for rank profiles.
     * @return The built {@link Search} object.
     * @throws IOException    Thrown if there was a problem reading the file.
     * @throws ParseException Thrown if there was a problem parsing the file content.
     */
    public static Search buildFromFile(String fileName,
                                       DeployLogger deployLogger,
                                       RankProfileRegistry rankProfileRegistry,
                                       QueryProfileRegistry queryProfileRegistry)
            throws IOException, ParseException {
        return createFromFile(fileName, deployLogger, rankProfileRegistry, queryProfileRegistry).getSearch();
    }

    /**
     * Convenience factory method to import and build a {@link Search} object from a raw object.
     *
     * @param rawSearch the raw object to build from.
     * @return the built {@link SearchBuilder} object.
     * @see #importRawSearch(Search)
     */
    public static SearchBuilder createFromRawSearch(Search rawSearch,
                                                    RankProfileRegistry rankProfileRegistry,
                                                    QueryProfileRegistry queryProfileRegistry) {
        SearchBuilder builder = new SearchBuilder(rankProfileRegistry, queryProfileRegistry);
        builder.importRawSearch(rawSearch);
        builder.build();
        return builder;
    }

    /**
     * Convenience factory method to import and build a {@link Search} object from a raw object.
     *
     * @param rawSearch The raw object to build from.
     * @return The built {@link Search} object.
     * @see #importRawSearch(Search)
     */
    public static Search buildFromRawSearch(Search rawSearch,
                                            RankProfileRegistry rankProfileRegistry,
                                            QueryProfileRegistry queryProfileRegistry) {
        return createFromRawSearch(rawSearch, rankProfileRegistry, queryProfileRegistry).getSearch();
    }

    public RankProfileRegistry getRankProfileRegistry() {
        return rankProfileRegistry;
    }

    public QueryProfileRegistry getQueryProfileRegistry() {
        return queryProfileRegistry;
    }

}