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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;

/**
 * Helper class for generating Vespa search queries
 * https://docs.vespa.ai/en/reference/query-language-reference.html
 */
public final class Q {

    private static final ObjectMapper mapper = new ObjectMapper();
    static String toJson(Object o) {
        try {
            return mapper.writeValueAsString(o);
        }
        catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    private static final Sources SELECT_ALL_FROM_SOURCES_ALL = new Sources(new Select("*"), "*");

    public static Select select(String fieldName) { return new Select(fieldName);
    }

    public static Select select(String fieldName, String... others) {
        return new Select(fieldName, others);
    }

    /**
     * P represents "parentheses", used for generated a query in the parentheses.
     *
     * @param fieldName the field name
     * @return the field
     */
    public static Field p(String fieldName) {
        return SELECT_ALL_FROM_SOURCES_ALL.where(fieldName);
    }

    /**
     * P represents "parentheses", used for generated a query in the parentheses.
     *
     * @param query the query
     * @return the query
     */
    public static Query p(QueryChain query) {
        return new Query(SELECT_ALL_FROM_SOURCES_ALL, query);
    }

    /**
     * P represents "parentheses", used for generated a query in the parentheses.
     * This method generates an empty query
     *
     * @return the empty query
     */
    public static Query p() {
        return new Query(SELECT_ALL_FROM_SOURCES_ALL);
    }

    /**
     * Rank rank.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#rank
     *
     * @param query the query
     * @param ranks the ranks
     * @return the rank query
     */
    public static Rank rank(Query query, QueryChain... ranks) {
        return new Rank(query, ranks);
    }

    /**
     * UI represents "userInput".
     * https://docs.vespa.ai/en/reference/query-language-reference.html#userinput
     *
     * @param value the value
     * @return the user input query
     */
    public static UserInput ui(String value) {
        return new UserInput(value);
    }

    /**
     * userInput with an annotation.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#userinput
     *
     * @param a     the a
     * @param value the value
     * @return the user input query
     */
    public static UserInput ui(Annotation a, String value) {
        return new UserInput(a, value);
    }

    /**
     * A convenience method to generate userInput with default index annotation.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#userinput
     *
     * @param index the index
     * @param value the value
     * @return the user input query
     */
    public static UserInput ui(String index, String value) {
        return ui(A.defaultIndex(index), value);
    }

    /**
     * dotPdt represents "dotProduct".
     * https://docs.vespa.ai/en/reference/query-language-reference.html#dotproduct
     *
     * @param field       the field
     * @param weightedSet the weighted set
     * @return the dot product query
     */
    public static DotProduct dotPdt(String field, Map<String, Integer> weightedSet) {
        return new DotProduct(field, weightedSet);
    }

    /**
     * wtdSet represents "weightedSet".
     * https://docs.vespa.ai/en/reference/query-language-reference.html#weightedset
     *
     * @param field       the field
     * @param weightedSet the weighted set
     * @return the weighted set query
     */
    public static WeightedSet wtdSet(String field, Map<String, Integer> weightedSet) {
        return new WeightedSet(field, weightedSet);
    }

    /**
     * NonEmpty non empty.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#nonempty
     *
     * @param query the query
     * @return the non empty query
     */
    public static NonEmpty nonEmpty(Query query) {
        return new NonEmpty(query);
    }

    /**
     * Wand wand.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#wand
     *
     * @param field       the field
     * @param weightedSet the weighted set
     * @return the wand query
     */
    public static Wand wand(String field, Map<String, Integer> weightedSet) {
        return new Wand(field, weightedSet);
    }

    /**
     * Wand wand.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#wand
     *
     * @param field        the field
     * @param numericRange the numeric range
     * @return the wand query
     */
    public static Wand wand(String field, List<List<Integer>> numericRange) {
        return new Wand(field, numericRange);
    }

    /**
     * Weakand weak and.
     * https://docs.vespa.ai/en/reference/query-language-reference.html#weakand
     *
     * @param query the query
     * @return the weak and query
     */
    public static WeakAnd weakand(Query query) {
        return new WeakAnd(query);
    }

    /**
     * GeoLocation geo location
     * https://docs.vespa.ai/en/reference/query-language-reference.html#geolocation
     *
     * @param field the field
     * @param longitude longitude
     * @param latitude latitude
     * @param radius a string specifying the radius and it's unit
     * @return the geo-location query
     */
    public static GeoLocation geoLocation(String field, Double longitude, Double latitude, String radius) {
        return new GeoLocation(field, longitude, latitude, radius);
    }

    /**
     * NearestNeighbor nearest neighbor
     * https://docs.vespa.ai/en/reference/query-language-reference.html#nearestneighbor
     *
     * @param docVectorName the vector name defined in the vespa schema
     * @param queryVectorName the vector name in this query
     * @return the nearest neighbor query
     */
    public static NearestNeighbor nearestNeighbor(String docVectorName, String queryVectorName) {
        return new NearestNeighbor(docVectorName, queryVectorName);
    }
}