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

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Vespa query object
 */
public class Query extends QueryChain {

    final List<QueryChain> queries = new ArrayList<>();
    private Annotation annotation;

    Query(Sources sources, QueryChain queryChain) {
        this.sources = sources;
        this.queries.add(queryChain);
        this.nonEmpty = queryChain.nonEmpty;
    }

    Query(Sources sources) {
        this.sources = sources;
    }

    String toCommaSeparatedAndQueries() {
        return queries.stream()
            .filter(qc -> "and".equals(qc.getOp()))
            .map(Objects::toString)
            .collect(Collectors.joining(", "));
    }

    @Override
    public String toString() {
        // TODO: need to refactor
        if (!nonEmpty) {
            return "";
        }

        boolean hasAnnotation = A.hasAnnotation(annotation);

        StringBuilder sb = new StringBuilder();

        if (hasAnnotation) {
            sb.append("([").append(annotation).append("](");
        }

        boolean firstQuery = true;
        for (int i = 0; i < queries.size(); i++) {
            QueryChain qc = queries.get(i);
            if (!qc.nonEmpty) {
                continue;
            }

            boolean isNotAnd = "andnot".equals(qc.getOp());

            if (!firstQuery) {
                sb.append(" ");
                if (isNotAnd) {
                    sb.append("and !");
                } else {
                    sb.append(qc.getOp()).append(' ');
                }
            } else {
                firstQuery = false;
            }

            boolean appendBrackets =
                (qc instanceof Query && ((Query) qc).queries.size() > 1 && !A.hasAnnotation(((Query) qc).annotation))
                || isNotAnd;
            if (appendBrackets) {
                sb.append("(");
            }

            sb.append(qc);

            if (appendBrackets) {
                sb.append(")");
            }

        }

        if (hasAnnotation) {
            sb.append("))");
        }
        return sb.toString().trim();
    }

    /**
     * And.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#and
     *
     * @param fieldName the field name
     * @return the field
     */
    public Field and(String fieldName) {
        Field f = new Field(this, fieldName);
        f.setOp("and");
        queries.add(f);
        nonEmpty = true;
        return f;
    }

    /**
     * Andnot.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#andnot
     *
     * @param fieldName the field name
     * @return the field
     */
    public Field andnot(String fieldName) {
        Field f = new Field(this, fieldName);
        f.setOp("andnot");
        queries.add(f);
        nonEmpty = true;
        return f;
    }

    /**
     * Or.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#or
     *
     * @param fieldName the field name
     * @return the field
     */
    public Field or(String fieldName) {
        Field f = new Field(this, fieldName);
        f.setOp("or");
        queries.add(f);
        nonEmpty = true;
        return f;
    }

    /**
     * And.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#and
     *
     * @param query the query
     * @return the query
     */
    public Query and(QueryChain query) {
        query.setOp("and");
        queries.add(query);
        nonEmpty = nonEmpty || query.nonEmpty;
        return this;
    }

    /**
     * Andnot.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#andnot
     *
     * @param query the query
     * @return the query
     */
    public Query andnot(QueryChain query) {
        query.setOp("andnot");
        queries.add(query);
        nonEmpty = nonEmpty || query.nonEmpty;
        return this;
    }

    /**
     * Or.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#or
     *
     * @param query the query
     * @return the query
     */
    public Query or(QueryChain query) {
        query.setOp("or");
        queries.add(query);
        nonEmpty = nonEmpty || query.nonEmpty;
        return this;
    }

    /**
     * Annotate a query (sub-expression).
     * https://docs.vespa.ai/en/reference/query-language-reference.html#annotations-of-sub-expressions
     *
     * @param annotation the annotation
     * @return the query
     */
    public Query annotate(Annotation annotation) {
        this.annotation = annotation;
        return this;
    }

    /**
     * Offset.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#limit-offset
     *
     * @param offset the offset
     * @return the end query
     */
    public EndQuery offset(int offset) {
        return new EndQuery(this).offset(offset);
    }

    /**
     * Limit.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#limit-offset
     *
     * @param hits the hits
     * @return the end query
     */
    public EndQuery limit(int hits) {
        return new EndQuery(this).limit(hits);
    }

    /**
     * Timeout.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#timeout
     *
     * @param timeout the timeout
     * @return the end query
     */
    public EndQuery timeout(int timeout) {
        return new EndQuery(this).timeout(timeout);
    }

    /**
     * Group.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#grouping
     *
     * @param group the group
     * @return the end query
     */
    public EndQuery group(Group group) {
        return new EndQuery(this).group(group);
    }

    /**
     * Group.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#grouping
     *
     * @param groupStr the group str
     * @return the end query
     */
    public EndQuery group(String groupStr) {
        return new EndQuery(this).group(groupStr);
    }

    /**
     * Order by asc.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#order-by
     *
     * @param fieldName the field name
     * @return the end query
     */
    public EndQuery orderByAsc(String fieldName) {
        return new EndQuery(this).orderByAsc(fieldName);
    }

    /**
     * Order by asc.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#order-by
     *
     * @param annotation the annotation
     * @param fieldName the field name
     * @return the end query
     */
    public EndQuery orderByAsc(Annotation annotation, String fieldName) {
        return new EndQuery(this).orderByAsc(annotation, fieldName);
    }

    /**
     * Order by desc.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#order-by
     *
     * @param fieldName the field name
     * @return the end query
     */
    public EndQuery orderByDesc(String fieldName) {
        return new EndQuery(this).orderByDesc(fieldName);
    }


    /**
     * Order by desc.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#order-by
     *
     * @param annotation the annotation
     * @param fieldName the field name
     * @return the end query
     */
    public EndQuery orderByDesc(Annotation annotation, String fieldName) {
        return new EndQuery(this).orderByDesc(annotation, fieldName);
    }

    /**
     * Calls fix()
     *
     * @return the fixed query
     * @deprecated use {@link #fix()}, {@link #end()} or {@link #build} instead
     */
    @Deprecated // TODO: Remove on Vespa 9
    public FixedQuery semicolon() { return fix(); }

    /** Returns this as an ended query. */
    public EndQuery end() {
        return new EndQuery(this);
    }

    /** Calls end().fix(). */
    public FixedQuery fix() {
        return end().fix();
    }

    /** Calls fix().build(). */
    public String build() {
        return fix().build();
    }

    @Override
    public Sources getSources() {
        return sources;
    }

    @Override
    public void setSources(Sources sources) {
        this.sources = sources;
    }

    @Override
    public Query getQuery() {
        return this;
    }

    @Override
    public Select getSelect() {
        return sources.select;
    }

    @Override
    public boolean hasPositiveSearchField(String fieldName) {
        boolean hasPositiveInSubqueries = queries.stream().anyMatch(q -> q.hasPositiveSearchField(fieldName));
        boolean hasNegativeInSubqueries = queries.stream().anyMatch(q -> q.hasNegativeSearchField(fieldName));
        return nonEmpty
               && ((!"andnot".equals(this.op) && hasPositiveInSubqueries)
                   || ("andnot".equals(this.op) && hasNegativeInSubqueries));
    }


    @Override
    public boolean hasPositiveSearchField(String fieldName, Object value) {
        boolean hasPositiveInSubqueries = queries.stream().anyMatch(q -> q.hasPositiveSearchField(fieldName, value));
        boolean hasNegativeInSubqueries = queries.stream().anyMatch(q -> q.hasNegativeSearchField(fieldName, value));
        return nonEmpty &&
               (!"andnot".equals(this.op) && hasPositiveInSubqueries)
               || ("andnot".equals(this.op) && hasNegativeInSubqueries);
    }

    @Override
    public boolean hasNegativeSearchField(String fieldName) {
        boolean hasPositiveInSubqueries = queries.stream().anyMatch(q -> q.hasPositiveSearchField(fieldName));
        boolean hasNegativeInSubqueries = queries.stream().anyMatch(q -> q.hasNegativeSearchField(fieldName));
        return nonEmpty
               && (!"andnot".equals(this.op) && hasNegativeInSubqueries)
               || ("andnot".equals(this.op) && hasPositiveInSubqueries);
    }

    @Override
    public boolean hasNegativeSearchField(String fieldName, Object value) {
        boolean hasPositiveInSubqueries = queries.stream().anyMatch(q -> q.hasPositiveSearchField(fieldName, value));
        boolean hasNegativeInSubqueries = queries.stream().anyMatch(q -> q.hasNegativeSearchField(fieldName, value));
        return nonEmpty
               && (!"andnot".equals(this.op) && hasNegativeInSubqueries)
               || ("andnot".equals(this.op) && hasPositiveInSubqueries);
    }

}