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

import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.query.*;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.text.MapParser;

import java.util.LinkedHashMap;
import java.util.Map;

import static com.yahoo.container.protect.Error.UNSPECIFIED;
import com.yahoo.yolean.Exceptions;

/**
 * Searcher that will create a Vespa WAND item from a list of tokens with weights.
 * IndexFacts is used to determine which WAND to create.
 *
 * @author geirst
 * @author bratseth
 */
public class WandSearcher extends Searcher {

    /**
     * Enum used to represent which "wand" this searcher should produce.
     */
    private enum WandType {
        VESPA("vespa"),
        OR("or"),
        PARALLEL("parallel"),
        DOT_PRODUCT("dotProduct");

        private final String type;

        WandType(String type) {
            this.type = type;
        }

        public static WandType create(String type) {
            for (WandType enumType : WandType.values()) {
                if (enumType.type.equals(type)) {
                    return enumType;
                }
            }
            return WandType.VESPA;
        }
    }

    /**
     * Class to resolve the inputs used by this searcher.
     */
    private static class InputResolver {

        private static final CompoundName WAND_FIELD = new CompoundName("wand.field");
        private static final CompoundName WAND_TOKENS = new CompoundName("wand.tokens");
        private static final CompoundName WAND_HEAP_SIZE = new CompoundName("wand.heapSize");
        private static final CompoundName WAND_TYPE = new CompoundName("wand.type");
        private static final CompoundName WAND_SCORE_THRESHOLD = new CompoundName("wand.scoreThreshold");
        private static final CompoundName WAND_THRESHOLD_BOOST_FACTOR = new CompoundName("wand.thresholdBoostFactor");
        private final String fieldName;
        private final WandType wandType;
        private final Map<String, Integer> tokens;
        private final int heapSize;
        private final double scoreThreshold;
        private final double thresholdBoostFactor;

        public InputResolver(Query query, Execution execution) {
            fieldName = query.properties().getString(WAND_FIELD);
            if (fieldName != null) {
                String tokens = query.properties().getString(WAND_TOKENS);
                if (tokens != null) {
                    wandType = resolveWandType(execution.context().getIndexFacts().newSession(query), query);
                    this.tokens = new IntegerMapParser().parse(tokens, new LinkedHashMap<>());
                    heapSize = resolveHeapSize(query);
                    scoreThreshold = resolveScoreThreshold(query);
                    thresholdBoostFactor = resolveThresholdBoostFactor(query);
                    return;
                }
            }
            wandType = null;
            tokens = null;
            heapSize = 0;
            scoreThreshold = 0;
            thresholdBoostFactor = 1;
        }

        private WandType resolveWandType(IndexFacts.Session indexFacts, Query query) {
            Index index = indexFacts.getIndex(fieldName);
            if (index.isNull()) {
                throw new IllegalArgumentException("Field '" + fieldName + "' was not found in " + indexFacts);
            } else {
                return WandType.create(query.properties().getString(WAND_TYPE, "vespa"));
            }
        }

        private int resolveHeapSize(Query query) {
            String defaultHeapSize = "100";
            return Integer.valueOf(query.properties().getString(WAND_HEAP_SIZE, defaultHeapSize));
        }

        private double resolveScoreThreshold(Query query) {
            return Double.valueOf(query.properties().getString(WAND_SCORE_THRESHOLD, "0"));
        }

        private double resolveThresholdBoostFactor(Query query) {
            return Double.valueOf(query.properties().getString(WAND_THRESHOLD_BOOST_FACTOR, "1"));
        }

        public boolean hasValidData() {
            return tokens != null && !tokens.isEmpty();
        }

        public String getFieldName() {
            return fieldName;
        }

        public Map<String, Integer> getTokens() {
            return tokens;
        }

        public WandType getWandType() {
            return wandType;
        }

        public Integer getHeapSize() {
            return heapSize;
        }

        public Double getScoreThreshold() {
            return scoreThreshold;
        }

        public Double getThresholdBoostFactor() {
            return thresholdBoostFactor;
        }
    }

    @Override
    public Result search(Query query, Execution execution) {
        try {
            InputResolver inputs = new InputResolver(query, execution);
            if ( ! inputs.hasValidData()) return execution.search(query);

            QueryTreeUtil.andQueryItemWithRoot(query, createWandQueryItem(inputs));
            query.trace("WandSearcher: Added WAND operator", true, 4);
            return execution.search(query);
        }
        catch (IllegalArgumentException e) {
            return new Result(query,ErrorMessage.createInvalidQueryParameter(Exceptions.toMessageString(e)));
        }
    }

    private Item createWandQueryItem(InputResolver inputs) {
        if (inputs.getWandType().equals(WandType.VESPA)) {
            return populate(new WeakAndItem(inputs.getHeapSize()), inputs.getFieldName(), inputs.getTokens());
        } else if (inputs.getWandType().equals(WandType.OR)) {
            return populate(new OrItem(), inputs.getFieldName(), inputs.getTokens());
        } else if (inputs.getWandType().equals(WandType.PARALLEL)) {
            return populate(new WandItem(inputs.getFieldName(), inputs.getHeapSize()),
                    inputs.getScoreThreshold(), inputs.getThresholdBoostFactor(), inputs.getTokens());
        } else if (inputs.getWandType().equals(WandType.DOT_PRODUCT)) {
            return populate(new DotProductItem(inputs.getFieldName()), inputs.getTokens());
        }
        throw new IllegalArgumentException("Unknown type '" + inputs.getWandType() + "'");
    }

    private CompositeItem populate(CompositeItem parent, String fieldName, Map<String,Integer> tokens) {
        for (Map.Entry<String,Integer> entry : tokens.entrySet()) {
            WordItem wordItem = new WordItem(entry.getKey(), fieldName);
            wordItem.setWeight(entry.getValue());
            wordItem.setStemmed(true);
            wordItem.setNormalizable(false);
            parent.addItem(wordItem);
        }
        return parent;
    }

    private WeightedSetItem populate(WeightedSetItem item, Map<String,Integer> tokens) {
        for (Map.Entry<String,Integer> entry : tokens.entrySet()) {
            item.addToken(entry.getKey(), entry.getValue());
        }
        return item;
    }

    private WandItem populate(WandItem item, double scoreThreshold, double thresholdBoostFactor, Map<String,Integer> tokens) {
        populate(item, tokens);
        item.setScoreThreshold(scoreThreshold);
        item.setThresholdBoostFactor(thresholdBoostFactor);
        return item;
    }

    private static class IntegerMapParser extends MapParser<Integer> {
        @Override
        protected Integer parseValue(String s) {
            return Integer.parseInt(s);
        }
    }

}