-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathInvertedIndexAsyncTest.java
208 lines (183 loc) · 9.92 KB
/
InvertedIndexAsyncTest.java
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
package com.arangodb;
import com.arangodb.entity.*;
import com.arangodb.entity.arangosearch.*;
import com.arangodb.entity.arangosearch.analyzer.DelimiterAnalyzer;
import com.arangodb.entity.arangosearch.analyzer.DelimiterAnalyzerProperties;
import com.arangodb.model.InvertedIndexOptions;
import com.arangodb.model.PersistentIndexOptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
public class InvertedIndexAsyncTest extends BaseJunit5 {
private static final String COLLECTION_NAME = "InvertedIndexTest_collection";
private static Stream<Arguments> asyncCols() {
return asyncDbsStream().map(mapNamedPayload(db -> db.collection(COLLECTION_NAME))).map(Arguments::of);
}
@BeforeAll
static void init() {
initCollections(COLLECTION_NAME);
}
private void createAnalyzer(String analyzerName, ArangoDatabaseAsync db) throws ExecutionException, InterruptedException {
Set<AnalyzerFeature> features = new HashSet<>();
features.add(AnalyzerFeature.frequency);
features.add(AnalyzerFeature.norm);
features.add(AnalyzerFeature.position);
DelimiterAnalyzer da = new DelimiterAnalyzer();
da.setName(analyzerName);
da.setFeatures(features);
DelimiterAnalyzerProperties props = new DelimiterAnalyzerProperties();
props.setDelimiter("-");
da.setProperties(props);
db.createSearchAnalyzer(da).get();
}
private InvertedIndexOptions createOptions(String analyzerName) {
Boolean cache = isEnterprise() ? true : null;
Boolean fieldCache = cache != null ? false : null;
InvertedIndexField field = new InvertedIndexField()
.name("foo")
.analyzer(AnalyzerType.identity.toString())
.includeAllFields(true)
.searchField(false)
.trackListPositions(false)
.cache(fieldCache)
.features(
AnalyzerFeature.position,
AnalyzerFeature.frequency,
AnalyzerFeature.norm,
AnalyzerFeature.offset
);
if (isEnterprise()) {
field.nested(
new InvertedIndexField()
.name("bar")
.analyzer(analyzerName)
.searchField(true)
.features(AnalyzerFeature.position, AnalyzerFeature.frequency)
.nested(
new InvertedIndexField()
.name("baz")
.analyzer(AnalyzerType.identity.toString())
.searchField(false)
.features(AnalyzerFeature.frequency)
)
);
}
return new InvertedIndexOptions()
.name(rndName())
.inBackground(true)
.parallelism(5)
.primarySort(new InvertedIndexPrimarySort()
.fields(
new InvertedIndexPrimarySort.Field("f1", InvertedIndexPrimarySort.Field.Direction.asc),
new InvertedIndexPrimarySort.Field("f2", InvertedIndexPrimarySort.Field.Direction.desc)
)
.compression(ArangoSearchCompression.lz4)
.cache(cache)
)
.storedValues(new StoredValue(Arrays.asList("f3", "f4"), ArangoSearchCompression.none, cache))
.optimizeTopK("BM25(@doc) DESC", "TFIDF(@doc) DESC")
.analyzer(analyzerName)
.features(AnalyzerFeature.position, AnalyzerFeature.frequency)
.includeAllFields(false)
.trackListPositions(true)
.searchField(true)
.fields(field)
.consolidationIntervalMsec(11L)
.commitIntervalMsec(22L)
.cleanupIntervalStep(33L)
.consolidationPolicy(ConsolidationPolicy.of(ConsolidationType.TIER)
.segmentsMin(3L)
.segmentsMax(44L)
.segmentsBytesMax(55555L)
.segmentsBytesFloor(666L)
.minScore(77L)
)
.writebufferIdle(44L)
.writebufferActive(55L)
.writebufferSizeMax(66L)
.cache(cache)
.primaryKeyCache(cache);
}
private void assertCorrectIndexEntity(InvertedIndexEntity indexResult, InvertedIndexOptions options) {
assertThat(indexResult).isNotNull();
assertThat(indexResult.getId()).isNotNull().isNotEmpty();
// FIXME: in single server this is null
// assertThat(indexResult.getIsNewlyCreated()).isTrue();
assertThat(indexResult.getUnique()).isFalse();
assertThat(indexResult.getSparse()).isTrue();
assertThat(indexResult.getVersion()).isNotNull();
assertThat(indexResult.getCode()).isNotNull();
assertThat(indexResult.getType()).isEqualTo(IndexType.inverted);
assertThat(indexResult.getName()).isEqualTo(options.getName());
assertThat(indexResult.getFields()).containsExactlyElementsOf(options.getFields());
assertThat(indexResult.getSearchField()).isEqualTo(options.getSearchField());
assertThat(indexResult.getStoredValues()).containsExactlyElementsOf(options.getStoredValues());
assertThat(indexResult.getPrimarySort()).isEqualTo(options.getPrimarySort());
assertThat(indexResult.getAnalyzer()).isEqualTo(options.getAnalyzer());
assertThat(indexResult.getFeatures()).hasSameElementsAs(options.getFeatures());
assertThat(indexResult.getIncludeAllFields()).isEqualTo(options.getIncludeAllFields());
assertThat(indexResult.getTrackListPositions()).isEqualTo(options.getTrackListPositions());
assertThat(indexResult.getCleanupIntervalStep()).isEqualTo(options.getCleanupIntervalStep());
assertThat(indexResult.getCommitIntervalMsec()).isEqualTo(options.getCommitIntervalMsec());
assertThat(indexResult.getConsolidationIntervalMsec()).isEqualTo(options.getConsolidationIntervalMsec());
assertThat(indexResult.getConsolidationPolicy()).isEqualTo(options.getConsolidationPolicy());
assertThat(indexResult.getWritebufferIdle()).isEqualTo(options.getWritebufferIdle());
assertThat(indexResult.getWritebufferActive()).isEqualTo(options.getWritebufferActive());
assertThat(indexResult.getWritebufferSizeMax()).isEqualTo(options.getWritebufferSizeMax());
assertThat(indexResult.getCache()).isEqualTo(options.getCache());
assertThat(indexResult.getPrimaryKeyCache()).isEqualTo(options.getPrimaryKeyCache());
if (isEnterprise() && isAtLeastVersion(3, 12)) {
assertThat(indexResult.getOptimizeTopK()).containsExactlyElementsOf(options.getOptimizeTopK());
}
}
@ParameterizedTest
@MethodSource("asyncCols")
void createAndGetInvertedIndex(ArangoCollectionAsync collection) throws ExecutionException, InterruptedException {
assumeTrue(isAtLeastVersion(3, 10));
String analyzerName = "delimiter-" + UUID.randomUUID();
createAnalyzer(analyzerName, collection.db());
InvertedIndexOptions options = createOptions(analyzerName);
InvertedIndexEntity created = collection.ensureInvertedIndex(options).get();
assertCorrectIndexEntity(created, options);
InvertedIndexEntity loadedIndex = collection.getInvertedIndex(created.getName()).get();
assertCorrectIndexEntity(loadedIndex, options);
}
@ParameterizedTest
@MethodSource("asyncCols")
void getInvertedIndexesShouldNotReturnOtherIndexTypes(ArangoCollectionAsync collection) throws ExecutionException, InterruptedException {
assumeTrue(isAtLeastVersion(3, 10));
// create persistent index
collection.ensurePersistentIndex(Collections.singletonList("foo"), new PersistentIndexOptions().name("persistentIndex"));
// create inverted index
String analyzerName = "delimiter-" + UUID.randomUUID();
createAnalyzer(analyzerName, collection.db());
InvertedIndexOptions options = createOptions(analyzerName);
InvertedIndexEntity created = collection.ensureInvertedIndex(options).get();
Collection<InvertedIndexEntity> loadedIndexes = collection.getInvertedIndexes().get();
assertThat(loadedIndexes).map(InvertedIndexEntity::getName)
.doesNotContain("persistentIndex")
.contains(created.getName());
}
@ParameterizedTest
@MethodSource("asyncCols")
void getIndexesShouldNotReturnInvertedIndexes(ArangoCollectionAsync collection) throws ExecutionException, InterruptedException {
assumeTrue(isAtLeastVersion(3, 10));
// create persistent index
collection.ensurePersistentIndex(Collections.singletonList("foo"), new PersistentIndexOptions().name("persistentIndex"));
// create inverted index
String analyzerName = "delimiter-" + UUID.randomUUID();
createAnalyzer(analyzerName, collection.db());
InvertedIndexOptions options = createOptions(analyzerName);
InvertedIndexEntity created = collection.ensureInvertedIndex(options).get();
Collection<IndexEntity> loadedIndexes = collection.getIndexes().get();
assertThat(loadedIndexes).map(IndexEntity::getName)
.doesNotContain(created.getName())
.contains("persistentIndex");
}
}