aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/searcher/ValidatePredicateSearcher.java
blob: 2e2c73b6707ad1588a63fcd7a36a1ded586902f6 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher;

import java.util.Optional;
import com.yahoo.component.chain.dependencies.After;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.PredicateQueryItem;
import com.yahoo.prelude.query.ToolBox;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.querytransform.BooleanSearcher;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;

import java.util.Collection;

/**
 * Checks that predicate queries don't use values outside the defined upper/lower bounds.
 *
 * @author <a href="mailto:magnarn@yahoo-inc.com">Magnar Nedland</a>
 */
@After(BooleanSearcher.PREDICATE)
public class ValidatePredicateSearcher extends Searcher {

    @Override
    public Result search(Query query, Execution execution) {
        Optional<ErrorMessage> e = validate(query, execution.context().getIndexFacts().newSession(query));
        if (e.isPresent()) {
            Result r = new Result(query);
            r.hits().addError(e.get());
            return r;
        }
        return execution.search(query);
    }

    private Optional<ErrorMessage> validate(Query query, IndexFacts.Session indexFacts) {
        ValidatePredicateVisitor visitor = new ValidatePredicateVisitor(indexFacts);
        ToolBox.visit(visitor, query.getModel().getQueryTree().getRoot());
        return visitor.errorMessage;
    }

    private static class ValidatePredicateVisitor extends ToolBox.QueryVisitor {

        private final IndexFacts.Session indexFacts;

        public Optional<ErrorMessage> errorMessage = Optional.empty();

        public ValidatePredicateVisitor(IndexFacts.Session indexFacts) {
            this.indexFacts = indexFacts;
        }

        @Override
        public boolean visit(Item item) {
            if (item instanceof PredicateQueryItem) {
                visit((PredicateQueryItem) item);
            }
            return true;
        }

        private void visit(PredicateQueryItem item) {
            Index index = getIndexFromUnionOfDocumentTypes(item);
            for (PredicateQueryItem.RangeEntry entry : item.getRangeFeatures()) {
                long value = entry.getValue();
                if (value < index.getPredicateLowerBound() || value > index.getPredicateUpperBound()) {
                    errorMessage = Optional.of(ErrorMessage.createIllegalQuery(
                            String.format("%s=%d outside configured predicate bounds.", entry.getKey(), value)));
                }
            }
        }

        private Index getIndexFromUnionOfDocumentTypes(PredicateQueryItem item) {
            return indexFacts.getIndex(item.getIndexName());
        }

        @Override
        public void onExit() {}
    }
}