Skip to content

Commit ad153bd

Browse files
committed
Add Indexor aggregation tests
1 parent ad8ba2e commit ad153bd

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

cfg/src/main/java/asaintsever/tinyworld/cfg/Configuration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public class UI {
3838
public class Deps {
3939
public Map<String, String> logging;
4040
}
41-
41+
4242
@ToString
4343
public class PhotoTree {
4444
public Filter filter;
45-
45+
4646
@ToString
4747
public class Filter {
4848
public String template;

indexor/src/main/java/asaintsever/tinyworld/indexor/IPhoto.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
package asaintsever.tinyworld.indexor;
2121

2222
import java.io.IOException;
23+
import java.util.List;
2324

2425
import asaintsever.tinyworld.indexor.search.results.IndexPage;
26+
import asaintsever.tinyworld.indexor.search.results.TermsAggregation;
2527
import asaintsever.tinyworld.metadata.extractor.PhotoMetadata;
2628

2729
public interface IPhoto {
@@ -32,6 +34,8 @@ public interface IPhoto {
3234

3335
long count() throws IOException;
3436

37+
List<TermsAggregation> getAggregations(String searchTemplateId) throws IOException;
38+
3539
IndexPage<PhotoMetadata> search(String query, int from, int size) throws IOException;
3640

3741
IndexPage<PhotoMetadata> next(IndexPage<PhotoMetadata> page) throws IOException;

indexor/src/main/java/asaintsever/tinyworld/indexor/Indexor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.net.URISyntaxException;
2525
import java.text.SimpleDateFormat;
26+
import java.util.List;
2627
import java.util.Map;
2728

2829
import org.apache.commons.codec.digest.DigestUtils;
@@ -34,6 +35,7 @@
3435
import asaintsever.tinyworld.indexor.opensearch.Cluster;
3536
import asaintsever.tinyworld.indexor.opensearch.Cluster.ClusterNodeException;
3637
import asaintsever.tinyworld.indexor.search.results.IndexPage;
38+
import asaintsever.tinyworld.indexor.search.results.TermsAggregation;
3739
import asaintsever.tinyworld.indexor.opensearch.ClusterClient;
3840
import asaintsever.tinyworld.indexor.opensearch.Document;
3941
import asaintsever.tinyworld.indexor.opensearch.DocumentAlreadyExistsException;
@@ -267,6 +269,11 @@ public long count() throws IOException {
267269
return this.document.count();
268270
}
269271

272+
@Override
273+
public List<TermsAggregation> getAggregations(String searchTemplateId) throws IOException {
274+
return this.document.getAggregations(searchTemplateId);
275+
}
276+
270277
@Override
271278
public IndexPage<PhotoMetadata> search(String query, int from, int size) throws IOException {
272279
return this.document.search(query, from, size, PhotoMetadata.class);

indexor/src/main/java/asaintsever/tinyworld/indexor/opensearch/utils/TermsAggregationBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import org.opensearch.search.aggregations.Aggregation;
2626
import org.opensearch.search.aggregations.Aggregations;
27-
import org.opensearch.search.aggregations.bucket.terms.ParsedStringTerms;
27+
import org.opensearch.search.aggregations.bucket.terms.ParsedTerms;
2828
import org.opensearch.search.aggregations.bucket.terms.Terms;
2929

3030
import asaintsever.tinyworld.indexor.search.results.TermsAggregation;
@@ -41,10 +41,10 @@ public static List<TermsAggregation> from(Aggregations aggregations) {
4141
for (Aggregation aggregation : aggrList) {
4242
// We only support Terms aggregation
4343
// (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)
44-
if (ParsedStringTerms.class.isInstance(aggregation)) {
44+
if (ParsedTerms.class.isInstance(aggregation)) {
4545
TermsAggregation buckAggr = new TermsAggregation();
4646

47-
ParsedStringTerms aggr = (ParsedStringTerms) aggregation;
47+
ParsedTerms aggr = (ParsedTerms) aggregation;
4848
buckAggr.setName(aggr.getName());
4949
buckAggr.setSum_other_doc_count(aggr.getSumOfOtherDocCounts());
5050

indexor/src/test/java/asaintsever/tinyworld/indexor/IndexorTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.IOException;
2828
import java.time.LocalDate;
29+
import java.util.List;
2930
import java.util.Random;
3031

3132
import org.jeasy.random.EasyRandom;
@@ -40,6 +41,7 @@
4041
import org.opensearch.rest.RestStatus;
4142

4243
import asaintsever.tinyworld.indexor.search.results.IndexPage;
44+
import asaintsever.tinyworld.indexor.search.results.TermsAggregation;
4345
import asaintsever.tinyworld.metadata.extractor.PhotoMetadata;
4446

4547
public class IndexorTest {
@@ -166,4 +168,34 @@ void insertThenSearchMetadata() throws IOException, InterruptedException {
166168
System.out.println(
167169
"Total=" + mtdList.total() + ", Size=" + mtdList.size() + ", Result=" + mtdList.get().toString());
168170
}
171+
172+
@Test
173+
void insertThenAggregateMetadata() throws IOException, InterruptedException {
174+
assertTrue(indexor.metadataIndex().create()); // Index must be explicitly created as our mapping is set here
175+
176+
for (int i = 0; i < 100; i++) {
177+
assertTrue(() -> {
178+
try {
179+
// Insert photo metadata
180+
PhotoMetadata mtd = easyRandom.nextObject(PhotoMetadata.class);
181+
182+
String id = indexor.photos().add(mtd, false);
183+
return ((id != null) && !id.isEmpty());
184+
} catch (IOException e) {
185+
throw new RuntimeException(e);
186+
}
187+
});
188+
}
189+
190+
// Pause before asking # of photos in index
191+
Thread.sleep(2000);
192+
assertEquals(indexor.photos().count(), 100);
193+
194+
// Now, run template
195+
List<TermsAggregation> aggr = indexor.photos().getAggregations("year_country_month");
196+
System.out.println(aggr);
197+
198+
assertTrue(aggr.size() > 0);
199+
}
200+
169201
}

indexor/src/test/java/asaintsever/tinyworld/indexor/opensearch/ClusterClientTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ void runSearchTemplate() throws IOException, InterruptedException {
320320
// Now, run template
321321
List<TermsAggregation> aggr = doc.getAggregations(TEST_SEARCH_TEMPLATE_ID);
322322
System.out.println(aggr);
323+
324+
assertTrue(aggr.size() > 0);
323325
}
324326

325327
assertTrue(client.deleteSearchTemplate(TEST_SEARCH_TEMPLATE_ID));

0 commit comments

Comments
 (0)