aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/searchers/ValidateNearestNeighborSearcher.java
blob: 54c8055a2cefda0b5cc7ea02ff773f3802c1c8a1 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.search.searchers;

import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.NearestNeighborItem;
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.grouping.vespa.GroupingExecutor;
import com.yahoo.search.query.ranking.RankProperties;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.vespa.config.search.AttributesConfig;
import com.yahoo.yolean.chain.Before;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Validates any NearestNeighborItem query items.
 *
 * @author arnej
 */
@Before(GroupingExecutor.COMPONENT_NAME) // Must happen before query.prepare()
public class ValidateNearestNeighborSearcher extends Searcher {

    private final Map<String, List<TensorType>> validAttributes = new HashMap<>();

    public ValidateNearestNeighborSearcher(AttributesConfig attributesConfig) {
        for (AttributesConfig.Attribute a : attributesConfig.attribute()) {
            if (! validAttributes.containsKey(a.name())) {
                validAttributes.put(a.name(), new ArrayList<>());
            }
            if (a.datatype() == AttributesConfig.Attribute.Datatype.TENSOR) {
                TensorType tt = TensorType.fromSpec(a.tensortype());
                validAttributes.get(a.name()).add(tt);
            }
        }
    }

    @Override
    public Result search(Query query, Execution execution) {
        Optional<ErrorMessage> e = validate(query);
        return e.isEmpty() ? execution.search(query) : new Result(query, e.get());
    }

    private Optional<ErrorMessage> validate(Query query) {
        NNVisitor visitor = new NNVisitor(query.getRanking().getProperties(), validAttributes, query);
        ToolBox.visit(visitor, query.getModel().getQueryTree().getRoot());
        return visitor.errorMessage;
    }

    private static class NNVisitor extends ToolBox.QueryVisitor {

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

        private final Map<String, List<TensorType>> validAttributes;
        private final Query query;

        public NNVisitor(RankProperties rankProperties, Map<String, List<TensorType>> validAttributes, Query query) {
            this.validAttributes = validAttributes;
            this.query = query;
        }

        @Override
        public boolean visit(Item item) {
            if (item instanceof NearestNeighborItem) {
                String error = validate((NearestNeighborItem)item);
                if (error != null)
                    errorMessage = Optional.of(ErrorMessage.createIllegalQuery(error));
            }
            return true;
        }

        private static boolean isCompatible(TensorType fieldTensorType, TensorType queryTensorType) {
            // Precondition: isTensorTypeThatSupportsHnswIndex(fieldTensorType)
            var queryDimensions = queryTensorType.dimensions();
            if (queryDimensions.size() == 1) {
                var queryDimension = queryDimensions.get(0);
                var fieldDimensions = fieldTensorType.dimensions();
                for (var fieldDimension : fieldDimensions) {
                    if (fieldDimension.isIndexed()) {
                        return fieldDimension.equals(queryDimension);
                    }
                }
            }
            return false;
        }

        private static boolean badQueryTensorType(TensorType queryTensorType) {
            var queryDimensions = queryTensorType.dimensions();
            if (queryDimensions.size() != 1) {
                return true;
            }
            var dim = queryDimensions.get(0);
            return ! dim.isIndexed();
        }

        private static boolean isTensorTypeThatSupportsHnswIndex(TensorType tt) {
            List<TensorType.Dimension> dims = tt.dimensions();
            if (dims.size() == 1) {
                return dims.get(0).isIndexed();
            }
            if (dims.size() == 2) {
                var dims0 = dims.get(0);
                var dims1 = dims.get(1);
                return ((dims0.isMapped() && dims1.isIndexed()) || (dims0.isIndexed() && dims1.isMapped()));
            }
            return false;
        }

        /** Returns an error message if this is invalid, or null if it is valid */
        private String validate(NearestNeighborItem item) {
            if (item.getTargetNumHits() < 1)
                return item + " has invalid targetHits " + item.getTargetNumHits() + ": Must be >= 1";

            String queryFeatureName = "query(" + item.getQueryTensorName() + ")";
            Optional<Tensor> queryTensor = query.getRanking().getFeatures().getTensor(queryFeatureName);
            if (queryTensor.isEmpty())
                return item + " requires a tensor rank feature named '" + queryFeatureName + "' but this is not present";

            if (badQueryTensorType(queryTensor.get().type())) {
                return item + " tensor " + queryFeatureName + " must have exactly 1, indexed dimension, but was: " + queryTensor.get().type();
            }
            if ( ! validAttributes.containsKey(item.getIndexName())) {
                return item + " field is not an attribute";
            }
            List<TensorType> allTensorTypes = validAttributes.get(item.getIndexName());
            for (TensorType fieldType : allTensorTypes) {
                if (isTensorTypeThatSupportsHnswIndex(fieldType) && isCompatible(fieldType, queryTensor.get().type())) {
                    return null;
                }
            }
            for (TensorType fieldType : allTensorTypes) {
                if (isTensorTypeThatSupportsHnswIndex(fieldType) && ! isCompatible(fieldType, queryTensor.get().type())) {
                    return item + " field type " + fieldType + " does not match query type " + queryTensor.get().type();
                }
            }
            for (TensorType fieldType : allTensorTypes) {
                if (! isTensorTypeThatSupportsHnswIndex(fieldType)) {
                    return item + " field type " + fieldType + " is not supported by nearest neighbor searcher";
                }
            }
            return item + " field is not a tensor";
        }

        @Override
        public void onExit() {}

    }

}