aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/main/java/ai/vespa/client/dsl/FixedQuery.java
blob: df7666d536b4427f7fa2860a40b30d0c2b58b351 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.client.dsl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * FixedQuery contains a 'Query'.
 * This object holds vespa or user defined parameters
 * https://docs.vespa.ai/en/reference/query-api-reference.html
 */
public class FixedQuery {

    private final EndQuery endQuery;
    private final Map<String, String> others = new HashMap<>();
    private Map<String, String> queryMap;

    FixedQuery(EndQuery endQuery) {
        this.endQuery = endQuery;
    }

    public FixedQuery hits(int hits) {
        this.param("hits", hits);
        return this;
    }

    public FixedQuery offset(int offset) {
        this.param("offset", offset);
        return this;
    }

    public FixedQuery queryProfile(String queryProfile) {
        this.param("queryProfile", queryProfile);
        return this;
    }

    public FixedQuery groupingSessionCache(boolean enable) {
        this.param("groupingSessionCache", enable);
        return this;
    }

    public FixedQuery searchChain(String searchChain) {
        this.param("searchChain", searchChain);
        return this;
    }

    public FixedQuery timeout(int second) {
        this.param("timeout", second);
        return this;
    }

    public FixedQuery timeoutInMs(int milli) {
        this.param("timeout", milli + "ms");
        return this;
    }

    public FixedQuery tracelevel(int level) {
        this.param("tracelevel", level);
        return this;
    }

    public FixedQuery traceTimestamps(boolean enable) {
        this.param("trace.timestamps", enable);
        return this;
    }

    public FixedQuery defaultIndex(String indexName) {
        this.param("default-index", indexName);
        return this;
    }

    public FixedQuery encoding(String encoding) {
        this.param("encoding", encoding);
        return this;
    }

    public FixedQuery filter(String filter) {
        this.param("filter", filter);
        return this;
    }

    public FixedQuery locale(String locale) {
        this.param("locale", locale);
        return this;
    }

    public FixedQuery language(String language) {
        this.param("language", language);
        return this;
    }

    public FixedQuery query(String query) {
        this.param("query", query);
        return this;
    }

    public FixedQuery restrict(String commaDelimitedDocTypeNames) {
        this.param("restrict", commaDelimitedDocTypeNames);
        return this;
    }

    public FixedQuery path(String searchPath) {
        this.param("path", searchPath);
        return this;
    }

    public FixedQuery sources(String commaDelimitedSourceNames) {
        this.param("sources", commaDelimitedSourceNames);
        return this;
    }

    public FixedQuery type(String type) {
        // web, all, any, phrase, yql, adv (deprecated)
        this.param("type", type);
        return this;
    }

    public FixedQuery location(String location) {
        this.param("location", location);
        return this;
    }

    public FixedQuery rankfeature(String featureName, String featureValue) {
        this.param("rankfeature." + featureName, featureValue);
        return this;
    }

    public FixedQuery rankfeatures(boolean enable) {
        this.param("rankfeatures", enable);
        return this;
    }

    public FixedQuery ranking(String rankProfileName) {
        this.param("ranking", rankProfileName);
        return this;
    }

    public FixedQuery rankproperty(String propertyName, String propertyValue) {
        this.param("rankproperty." + propertyName, propertyValue);
        return this;
    }

    public FixedQuery rankingSoftTimeout(boolean enable) {
        this.param("ranking.softtimeout.enable", enable);
        return this;
    }

    public FixedQuery rankingSoftTimeout(boolean enable, double factor) {
        this.param("ranking.softtimeout.enable", enable);
        this.param("ranking.softtimeout.factor", factor);
        return this;
    }

    public FixedQuery sorting(String sorting) {
        this.param("sorting", sorting);
        return this;
    }

    public FixedQuery rankingFreshness(String freshness) {
        this.param("ranking.freshness", freshness);
        return this;
    }

    public FixedQuery rankingQueryCache(boolean enable) {
        this.param("ranking.queryCache", enable);
        return this;
    }

    public FixedQuery bolding(boolean enable) {
        this.param("bolding", enable);
        return this;
    }

    public FixedQuery format(String format) {
        this.param("format", format);
        return this;
    }

    public FixedQuery summary(String summaryClass) {
        this.param("summary", summaryClass);
        return this;
    }

    public FixedQuery presentationTemplate(String template) {
        this.param("presentation.template", template);
        return this;
    }

    public FixedQuery presentationTiming(boolean enable) {
        this.param("presentation.timing", enable);
        return this;
    }

    public FixedQuery select(String groupSyntax) {
        this.param("select", groupSyntax);
        return this;
    }

    public FixedQuery select(Group group) {
        this.param("select", group.toString());
        return this;
    }

    public FixedQuery collapseField(String summaryFieldName) {
        this.param("collapsefield", summaryFieldName);
        return this;
    }

    public FixedQuery collapseSummary(String summaryClass) {
        this.param("collapse.summary", summaryClass);
        return this;
    }

    public FixedQuery collapseSize(int size) {
        this.param("collapsesize", size);
        return this;
    }

    public FixedQuery posLatLong(String vespaLatLong) {
        this.param("pos.ll", vespaLatLong);
        return this;
    }

    public FixedQuery posLatLong(double lat, double lon) {
        String latlong = toVespaLatLong(lat, lon);
        return posLatLong(latlong);
    }

    private String toVespaLatLong(double lat, double lon) {
        double absLat = Math.abs(lat);
        double absLon = Math.abs(lon);
        if (absLat > 90 || absLon > 180) {
            throw new IllegalArgumentException(Text.format("invalid lat long value, lat=%f, long=%f", lat, lon));
        }

        return Text.format("%s%f;%s%f",
                             lat > 0 ? "N" : "S", absLat,
                             lon > 0 ? "E" : "W", absLon);
    }

    public FixedQuery posRadiusInKilometer(int km) {
        this.param("pos.radius", km + "km");
        return this;
    }

    public FixedQuery posRadiusInMeter(int m) {
        this.param("pos.radius", m + "m");
        return this;
    }

    public FixedQuery posRadiusInMile(int mi) {
        this.param("pos.radius", mi + "mi");
        return this;
    }

    public FixedQuery posBoundingBox(double n, double s, double e, double w) {
        this.param("pos.bb", Text.format("n=%f,s=%f,e=%f,w=%f", n, s, e, w));
        return this;
    }

    public FixedQuery streamingUserId(BigDecimal id) {
        this.param("streaming.userid", id);
        return this;
    }

    public FixedQuery streamingGroupName(String groupName) {
        this.param("streaming.groupname", groupName);
        return this;
    }

    public FixedQuery streamingSelection(String selection) {
        this.param("streaming.selection", selection);
        return this;
    }

    public FixedQuery streamingPriority(String priority) {
        this.param("streaming.priority", priority);
        return this;
    }

    public FixedQuery streamingMaxBucketsPerVisitor(int max) {
        this.param("streaming.maxbucketspervisitor", max);
        return this;
    }

    public FixedQuery rulesOff(boolean bool) {
        this.param("rules.off", bool);
        return this;
    }

    public FixedQuery rulesRulebase(String rulebase) {
        this.param("rules.rulebase", rulebase);
        return this;
    }

    public FixedQuery recall(String recall) {
        this.param("recall", recall);
        return this;
    }

    public FixedQuery user(String user) {
        this.param("user", user);
        return this;
    }

    public FixedQuery hitCountEstimate(boolean enable) {
        this.param("hitcountestimate", enable);
        return this;
    }

    public FixedQuery metricsIgnore(boolean bool) {
        this.param("metrics.ignore", bool);
        return this;
    }

    public FixedQuery param(String key, String value) {
        others.put(key, value);
        return this;
    }

    private FixedQuery param(String key, Object value) {
        this.param(key, value.toString());
        return this;
    }

    public FixedQuery params(Map<String, String> params) {
        others.putAll(params);
        return this;
    }

    /**
     * build the query map from the query
     *
     * @return the query map
     */
    public Map<String, String> buildQueryMap() {
        if (queryMap != null) {
            return queryMap;
        }

        StringBuilder sb = new StringBuilder();
        sb.append("select ")
            .append(endQuery.queryChain.getSelect())
            .append(" from ")
            .append(endQuery.queryChain.getSources())
            .append(" where ")
            .append(endQuery.queryChain);

        if (!"".equals(endQuery.toString())) {
            sb.append(' ').append(endQuery);
        }

        queryMap = new LinkedHashMap<>(); // for the order
        queryMap.put("yql", sb.toString());
        queryMap.putAll(others);
        queryMap.putAll(getUserInputs());
        return queryMap;
    }

    /**
     * Builds the vespa query string joined by '&amp;'
     *
     * @return the query string
     */
    public String build() {
        return buildQueryMap().entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue())
            .collect(Collectors.joining("&"));
    }

    private Map<String, String> getUserInputs() {
        return getUserInputs(endQuery.queryChain.getQuery());
    }

    private Map<String, String> getUserInputs(Query q) {
        Map<String, String> param = new HashMap<>();
        q.queries.forEach(qu -> {
            if (qu instanceof Query) {
                param.putAll(getUserInputs((Query) qu));
            }
        });
        return param;
    }

    public boolean hasPositiveSearchField(String fieldName) {
        return endQuery.queryChain.hasPositiveSearchField(fieldName);
    }

    public boolean hasPositiveSearchField(String fieldName, Object value) {
        return endQuery.queryChain.hasPositiveSearchField(fieldName, value);
    }

    public boolean hasNegativeSearchField(String fieldName) {
        return endQuery.queryChain.hasNegativeSearchField(fieldName);
    }

    public boolean hasNegativeSearchField(String fieldName, Object value) {
        return endQuery.queryChain.hasNegativeSearchField(fieldName, value);
    }

}