-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathHelper.java
More file actions
295 lines (241 loc) · 10.8 KB
/
Copy pathHelper.java
File metadata and controls
295 lines (241 loc) · 10.8 KB
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
package org.typesense.api;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
import org.typesense.model.ApiKey;
import org.typesense.model.ApiKeySchema;
import org.typesense.model.ApiKeysResponse;
import org.typesense.model.CollectionAlias;
import org.typesense.model.CollectionAliasSchema;
import org.typesense.model.CollectionAliasesResponse;
import org.typesense.model.CollectionResponse;
import org.typesense.model.CollectionSchema;
import org.typesense.model.Field;
import org.typesense.model.SearchSynonymSchema;
import org.typesense.model.StemmingDictionaryWords;
import org.typesense.model.StopwordsSetSchema;
import org.typesense.model.StopwordsSetUpsertSchema;
import org.typesense.model.StopwordsSetsRetrieveAllSchema;
import org.typesense.model.SynonymItemSchema;
import org.typesense.model.SynonymSetCreateSchema;
import org.typesense.model.SynonymSetSchema;
import org.typesense.model.SynonymSetsRetrieveSchema;
import org.typesense.model.CurationItemCreateSchema;
import org.typesense.model.CurationSetCreateSchema;
import org.typesense.model.CurationRule;
import org.typesense.model.CurationInclude;
import org.typesense.model.CurationExclude;
import org.typesense.resources.Node;
import org.typesense.model.AnalyticsRuleCreate;
import org.typesense.model.AnalyticsRuleCreateParams;
import org.typesense.model.AnalyticsRule;
import org.typesense.model.AnalyticsRuleType;
import org.typesense.api.AnalyticsRules;
public class Helper {
private final Client client;
public static boolean isV30OrAbove(Client client) {
try {
Map<String, Object> debugResponse = client.debug.retrieve();
if (debugResponse == null) {
return false;
}
Object version = debugResponse.get("version");
if (version == null) {
return false;
}
String versionStr = version.toString();
if ("nightly".equals(versionStr)) {
return true;
}
String numberedVersion = versionStr;
if (versionStr.startsWith("v")) {
numberedVersion = versionStr.substring(1);
}
String[] parts = numberedVersion.split("\\.");
if (parts.length == 0) {
return false;
}
int majorVersion = Integer.parseInt(parts[0]);
return majorVersion >= 30;
} catch (Exception e) {
return false;
}
}
Helper() {
List<Node> nodes = new ArrayList<>();
nodes.add(new Node("http", "localhost", "8108"));
Configuration configuration = new Configuration(nodes, Duration.ofSeconds(3), "xyz");
this.client = new Client(configuration);
}
public void createTestCollection() throws Exception {
List<Field> fields = new ArrayList<>();
fields.add(new Field().name(".*").type(FieldTypes.AUTO).optional(true));
CollectionSchema collectionSchema = new CollectionSchema();
collectionSchema.name("books").fields(fields);
client.collections().create(collectionSchema);
}
public void createTestQueryCollection() throws Exception {
List<Field> fields = new ArrayList<>();
fields.add(new Field().name("q").type((FieldTypes.STRING)));
fields.add(new Field().name("count").type((FieldTypes.INT32)));
CollectionSchema collectionSchema = new CollectionSchema();
collectionSchema.name("queries").fields(fields);
client.collections().create(collectionSchema);
}
public void createTestQueryDocument() throws Exception {
HashMap<String, Object> hmap = new HashMap<>();
hmap.put("q", "running shoes");
hmap.put("count", 42);
hmap.put("id", "query1");
client.collections("queries").documents().create(hmap);
}
public void createTestDocument() throws Exception {
String[] authors = { "shakspeare", "william" };
HashMap<String, Object> hmap = new HashMap<>();
hmap.put("title", "Romeo and juliet");
hmap.put("authors", authors);
hmap.put("publication_year", 1666);
hmap.put("ratings_count", 124);
hmap.put("average_rating", 3.2);
hmap.put("id", "1");
client.collections("books").documents().create(hmap);
}
public Client getClient() {
return this.client;
}
public void createTestAlias() throws Exception {
CollectionAliasSchema collectionAliasSchema = new CollectionAliasSchema();
collectionAliasSchema.collectionName("books_june11");
client.aliases().upsert("books", collectionAliasSchema);
}
public ApiKey createTestKey() throws Exception {
ApiKeySchema apiKeySchema = new ApiKeySchema();
List<String> actionValues = new ArrayList<>();
List<String> collectionValues = new ArrayList<>();
actionValues.add("*");
collectionValues.add("*");
apiKeySchema.description("Admin Key").actions(actionValues).collections(collectionValues);
return client.keys().create(apiKeySchema);
}
public void createTestSynonym() throws Exception {
SearchSynonymSchema synonym = new SearchSynonymSchema();
synonym.addSynonymsItem("blazer").addSynonymsItem("coat").addSynonymsItem("jacket");
client.collections("books").synonyms().upsert("coat-synonyms", synonym);
}
public void createTestAnalyticsCollection() throws Exception {
List<Field> fields = new ArrayList<>();
fields.add(new Field().name("rule_name").type(FieldTypes.STRING));
fields.add(new Field().name("event_type").type(FieldTypes.STRING));
fields.add(new Field().name("counter_value").type(FieldTypes.INT32));
fields.add(new Field().name("timestamp").type(FieldTypes.INT64));
CollectionSchema collectionSchema = new CollectionSchema();
collectionSchema.name("analytics_data").fields(fields);
client.collections().create(collectionSchema);
}
public String createTestAnalyticsRule() throws Exception {
String ruleName = "analytics-rule-" + System.currentTimeMillis();
AnalyticsRuleCreate analyticsRule = new AnalyticsRuleCreate()
.name(ruleName)
.type(AnalyticsRuleType.COUNTER)
.collection("analytics_data")
.eventType("click")
.params(new AnalyticsRuleCreateParams()
.counterField("num_employees")
.weight(1));
List<AnalyticsRuleCreate> rules = new ArrayList<>();
rules.add(analyticsRule);
client.analytics().rules().create(rules);
return ruleName;
}
public void createTestStopwordsSet() throws Exception {
List<String> stopwords = new ArrayList<>();
stopwords.add("the");
stopwords.add("of");
stopwords.add("and");
StopwordsSetUpsertSchema stopwordsSetSchema = new StopwordsSetUpsertSchema();
stopwordsSetSchema.stopwords(stopwords);
client.stopwords().upsert("common-words", stopwordsSetSchema);
}
public void createStemmingDictionary() throws Exception{
List<StemmingDictionaryWords> stemmingDictionaryWords = new ArrayList<>();
stemmingDictionaryWords.add(new StemmingDictionaryWords().word("ran").root("run"));
stemmingDictionaryWords.add(new StemmingDictionaryWords().word("running").root("run"));
client.stemming().dictionaries().upsert("irregular-plurals", stemmingDictionaryWords);
}
public SynonymSetCreateSchema createTestSynonymSetData() {
SynonymItemSchema synonymItem = new SynonymItemSchema();
synonymItem.setId("dummy");
synonymItem.setSynonyms(Arrays.asList("foo", "bar", "baz"));
SynonymSetCreateSchema synonymSetData = new SynonymSetCreateSchema();
synonymSetData.setItems(Arrays.asList(synonymItem));
return synonymSetData;
}
public CurationSetCreateSchema createTestCurationSetData() {
CurationRule rule = new CurationRule();
rule.setQuery("apple");
rule.setMatch(CurationRule.MatchEnum.EXACT);
CurationInclude include1 = new CurationInclude();
include1.setId("422");
include1.setPosition(1);
CurationInclude include2 = new CurationInclude();
include2.setId("54");
include2.setPosition(2);
CurationExclude exclude1 = new CurationExclude();
exclude1.setId("287");
CurationItemCreateSchema curationItem = new CurationItemCreateSchema();
curationItem.setId("dummy");
curationItem.setRule(rule);
curationItem.setIncludes(Arrays.asList(include1, include2));
curationItem.setExcludes(Arrays.asList(exclude1));
curationItem.setRemoveMatchedTokens(true);
curationItem.setFilterBy("category:=Electronics");
curationItem.setStopProcessing(true);
CurationSetCreateSchema curationSetData = new CurationSetCreateSchema();
curationSetData.setItems(Arrays.asList(curationItem));
curationSetData.setDescription("Test curation set");
return curationSetData;
}
public void teardown() throws Exception {
CollectionResponse[] collectionResponses = client.collections().retrieve();
for (CollectionResponse c : collectionResponses) {
if (c.getName() != null) {
client.collections(c.getName()).delete();
}
}
CollectionAliasesResponse collectionAliasesResponse = client.aliases().retrieve();
for (CollectionAlias a : collectionAliasesResponse.getAliases()) {
if (a.getName() != null) {
client.aliases(a.getName()).delete();
}
}
AnalyticsRules analyticsRules = client.analytics().rules();
List<AnalyticsRule> rules = analyticsRules.retrieve();
if (rules != null) {
for (AnalyticsRule r : rules) {
if (r.getName() != null) {
client.analytics().rules(r.getName()).delete();
}
}
}
ApiKeysResponse apiKeysResponse = client.keys().retrieve();
for (ApiKey k : apiKeysResponse.getKeys()) {
client.keys(k.getId()).delete();
}
StopwordsSetsRetrieveAllSchema stopwords = client.stopwords().retrieve();
for (StopwordsSetSchema s : stopwords.getStopwords()) {
client.stopwords(s.getId()).delete();
}
try {
SynonymSetSchema[] synonymSets = client.synonymSets().retrieve();
for (SynonymSetSchema s : synonymSets) {
if (s.getName() != null) {
client.synonymSet(s.getName()).delete();
}
}
} catch (Exception e) {
}
}
}