aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/searcher/test/ValidatePredicateSearcherTestCase.java
blob: a64d7fe8c11306dec1d0e1ff1683130a347e20c7 (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
// 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.google.common.util.concurrent.MoreExecutors;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.SearchDefinition;
import com.yahoo.prelude.searcher.ValidatePredicateSearcher;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.query.QueryTree;
import com.yahoo.search.query.parser.Parsable;
import com.yahoo.search.query.parser.ParserEnvironment;
import com.yahoo.search.rendering.RendererRegistry;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.yql.YqlParser;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
 * @author Magnar Nedland
 */
public class ValidatePredicateSearcherTestCase {

    @Test
    void testValidQuery() {
        ValidatePredicateSearcher searcher = new ValidatePredicateSearcher();
        String q = "select * from sources * where predicate(predicate_field,0,{\"age\":20L})";
        Result r = doSearch(searcher, q, "predicate-bounds [0..99]");
        assertNull(r.hits().getError());
    }

    @Test
    void testQueryOutOfBounds() {
        ValidatePredicateSearcher searcher = new ValidatePredicateSearcher();
        String q = "select * from sources * where predicate(predicate_field,0,{\"age\":200L})";
        Result r = doSearch(searcher, q, "predicate-bounds [0..99]");
        assertEquals(ErrorMessage.createIllegalQuery("age=200 outside configured predicate bounds."), r.hits().getError());
    }

    @Test
    void queryFailsWhenPredicateFieldIsUsedInTermSearch() {
        ValidatePredicateSearcher searcher = new ValidatePredicateSearcher();
        String q = "select * from sources * where predicate_field CONTAINS \"true\"";
        Result r = doSearch(searcher, q, "predicate-bounds [0..99]");
        assertEquals(ErrorMessage.createIllegalQuery("Index 'predicate_field' is predicate attribute and can only be used in conjunction with a predicate query operator."), r.hits().getError());
    }

    private static Result doSearch(ValidatePredicateSearcher searcher, String yqlQuery, String command) {
        QueryTree queryTree = new YqlParser(new ParserEnvironment()).parse(new Parsable().setQuery(yqlQuery));
        Query query = new Query();
        query.getModel().getQueryTree().setRoot(queryTree.getRoot());

        SearchDefinition searchDefinition = new SearchDefinition("document");
        Index index = new Index("predicate_field");
        index.setPredicate(true);
        index.addCommand(command);
        searchDefinition.addIndex(index);
        IndexFacts indexFacts = new IndexFacts(new IndexModel(searchDefinition));
        return new Execution(searcher, Execution.Context.createContextStub(indexFacts)).search(query);
    }

}