aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/main/java/ai/vespa/client/dsl/EndQuery.java
blob: 03ad6a5921d2f5e48905a752c9b82076269b1a42 (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
// 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.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * EndQuery contains a 'Query'
 * This object holds timeout, offset, limit, group and orderBy information before the semicolon
 */
public class EndQuery {

    final QueryChain queryChain;
    final Map<String, Integer> map = new LinkedHashMap<>();
    final List<Object[]> order = new ArrayList<>();
    private String groupQueryStr;

    EndQuery(QueryChain queryChain) {
        this.queryChain = queryChain;

        // make sure the order of limit, offset and timeout
        this.map.put("limit", null);
        this.map.put("offset", null);
        this.map.put("timeout", null);
    }

    EndQuery setTimeout(Integer timeout) {
        map.put("timeout", timeout);
        return this;
    }

    EndQuery setOffset(int offset) {
        map.put("offset", offset);
        return this;
    }

    EndQuery setLimit(int limit) {
        map.put("limit", limit);
        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 this.setOffset(offset);
    }

    /**
     * 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 this.setTimeout(timeout);
    }

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

    /**
     * Calls fix()
     *
     * @deprecated use {link #fix}
     */
    @Deprecated // TODO: Remove on Vespa 9
    public FixedQuery semicolon() { return fix(); }

    /** Returns a fixed query containing this. */
    public FixedQuery fix() {
        return new FixedQuery(this);
    }

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

    /**
     * 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 group(group.toString());
    }

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

    /**
     * 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) {
        order.add(new Object[]{annotation, fieldName, "asc"});
        return this;
    }

    /**
     * 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) {
        order.add(new Object[]{A.empty(), fieldName, "asc"});
        return this;
    }

    /**
     * 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) {
        order.add(new Object[]{annotation, fieldName, "desc"});
        return this;
    }

    /**
     * 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) {
        order.add(new Object[]{A.empty(), fieldName, "desc"});
        return this;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        String orderStr = order.stream().map(array -> A.empty().equals(array[0])
                                                      ? Text.format("%s %s", array[1], array[2])
                                                      : Text.format("%s%s %s", array[0], array[1], array[2]))
            .collect(Collectors.joining(", "));

        String others = map.entrySet().stream()
            .filter(entry -> entry.getValue() != null)
            .map(entry -> entry.getKey() + " " + entry.getValue())
            .collect(Collectors.joining(" "));

        if (orderStr.isEmpty()) {
            sb.append(others.isEmpty() ? "" : others);
        } else if (others.isEmpty()) {
            sb.append("order by ").append(orderStr);
        } else {
            sb.append("order by ").append(orderStr).append(" ").append(others);
        }

        if (groupQueryStr != null) {
            sb.append("| ").append(groupQueryStr);
        }

        return sb.toString();
    }

}