Skip to content

Commit 1a4cc6d

Browse files
committed
add new tests
Signed-off-by: Anthony Leong <[email protected]>
1 parent a2090dd commit 1a4cc6d

File tree

2 files changed

+61
-46
lines changed
  • client/rest-high-level/src/test/java/org/opensearch/client
  • rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation

2 files changed

+61
-46
lines changed

client/rest-high-level/src/test/java/org/opensearch/client/SearchIT.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
import org.opensearch.search.aggregations.bucket.composite.CompositeAggregation;
8585
import org.opensearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
8686
import org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder;
87+
import org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
88+
import org.opensearch.search.aggregations.bucket.histogram.DateHistogramInterval;
89+
import org.opensearch.search.aggregations.bucket.histogram.ParsedDateHistogram;
8790
import org.opensearch.search.aggregations.bucket.range.Range;
8891
import org.opensearch.search.aggregations.bucket.range.RangeAggregationBuilder;
8992
import org.opensearch.search.aggregations.bucket.terms.MultiTermsAggregationBuilder;
@@ -237,6 +240,32 @@ public void indexDocuments() throws IOException {
237240
client().performRequest(createFilteredAlias);
238241
}
239242

243+
{
244+
Request create = new Request(HttpPut.METHOD_NAME, "/index5");
245+
create.setJsonEntity(
246+
"{"
247+
+ " \"mappings\": {"
248+
+ " \"properties\": {"
249+
+ " \"date_created\": {"
250+
+ " \"type\": \"date\""
251+
+ " },"
252+
+ " \"distribution\": {"
253+
+ " \"properties\": {"
254+
+ " \"number_events\": {"
255+
+ " \"type\": \"unsigned_long\""
256+
+ " }"
257+
+ " }"
258+
+ " }"
259+
+ " }"
260+
+ " }"
261+
+ "}"
262+
);
263+
client().performRequest(create);
264+
Request doc1 = new Request(HttpPut.METHOD_NAME, "/index5/_doc/1");
265+
doc1.setJsonEntity("{\"date_created\":\"2024\", \"distribution\":{\"number_events\": 1000000}}");
266+
client().performRequest(doc1);
267+
}
268+
240269
client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh"));
241270
}
242271

@@ -514,6 +543,38 @@ public void testSearchWithTermsAndRangeAgg() throws IOException {
514543
}
515544
}
516545

546+
public void testSearchWithDateAndRangeAgg() throws IOException {
547+
SearchRequest searchRequest = new SearchRequest("index5");
548+
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
549+
DateHistogramAggregationBuilder yearAgg = new DateHistogramAggregationBuilder("year").field("date_created")
550+
.calendarInterval(DateHistogramInterval.YEAR)
551+
.format("yyyy");
552+
553+
RangeAggregationBuilder rangeAgg = new RangeAggregationBuilder("number_events").field("distribution.number_events")
554+
.addRange("0--999", 0.0, 1000.0)
555+
.addRange("999--", 1000.0, 10_000_000_000_00L);
556+
searchSourceBuilder.aggregation(yearAgg).aggregation(rangeAgg);
557+
searchSourceBuilder.size(0);
558+
searchRequest.source(searchSourceBuilder);
559+
SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync);
560+
assertSearchHeader(searchResponse);
561+
ParsedDateHistogram yearHistogram = searchResponse.getAggregations().get("year");
562+
assertEquals(1, yearHistogram.getBuckets().size());
563+
assertEquals("2024", yearHistogram.getBuckets().get(0).getKeyAsString());
564+
Range rangeAggregation = searchResponse.getAggregations().get("number_events");
565+
assertEquals(2, rangeAggregation.getBuckets().size());
566+
{
567+
Range.Bucket bucket = rangeAggregation.getBuckets().get(0);
568+
assertEquals("0--999", bucket.getKeyAsString());
569+
assertEquals(0, bucket.getDocCount());
570+
}
571+
{
572+
Range.Bucket bucket = rangeAggregation.getBuckets().get(1);
573+
assertEquals("999--", bucket.getKeyAsString());
574+
assertEquals(1, bucket.getDocCount());
575+
}
576+
}
577+
517578
public void testSearchWithTermsAndWeightedAvg() throws IOException {
518579
SearchRequest searchRequest = new SearchRequest("index");
519580
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/40_range.yml

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -738,49 +738,3 @@ setup:
738738
- match: { hits.total.value: 3 }
739739
- match: { aggregations.HalloweenAgg.buckets.to-october.doc_count: 3 }
740740
- match: { aggregations.HalloweenAgg.buckets.from-september.doc_count: 0 }
741-
742-
---
743-
"Date range and range aggregation test":
744-
- do:
745-
indices.create:
746-
index: test_date_range_and_range_agg
747-
body:
748-
settings:
749-
number_of_replicas: 0
750-
number_of_shards: 1
751-
refresh_interval: -1
752-
mappings:
753-
properties:
754-
date_created:
755-
type: date
756-
distribution:
757-
properties:
758-
number_events:
759-
type: unsigned_long
760-
- do:
761-
index:
762-
index: test_date_range_and_range_agg
763-
id: 1
764-
body: { "date_created": "2024", "distribution": { "number_events": 1000000}}
765-
- do:
766-
search:
767-
index: test_date_range_and_range_agg
768-
body:
769-
size: 0
770-
aggregations:
771-
year:
772-
date_histogram:
773-
field: date_created
774-
interval: "year"
775-
format: "yyyy"
776-
number_events:
777-
range:
778-
field: distribution.number_events
779-
ranges:
780-
- to: 1000
781-
from: 0
782-
key: "0--999"
783-
- to: 10000000000000
784-
from: 1000
785-
key: "999--"
786-
_source: false

0 commit comments

Comments
 (0)