-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* search: loading from json into search request
Signed-off-by: neo <1100909+neowu@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
238 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...est/src/test/java/core/framework/search/impl/ElasticSearchAggregationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package core.framework.search.impl; | ||
|
||
import co.elastic.clients.elasticsearch._types.aggregations.DateHistogramBucket; | ||
import co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket; | ||
import co.elastic.clients.elasticsearch._types.query_dsl.Query; | ||
import core.framework.inject.Inject; | ||
import core.framework.search.DeleteByQueryRequest; | ||
import core.framework.search.ElasticSearch; | ||
import core.framework.search.ElasticSearchType; | ||
import core.framework.search.IntegrationTest; | ||
import core.framework.search.SearchRequest; | ||
import core.framework.search.SearchResponse; | ||
import core.framework.util.ClasspathResources; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
|
||
import static core.framework.search.query.Aggregations.sum; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class ElasticSearchAggregationIntegrationTest extends IntegrationTest { | ||
@Inject | ||
ElasticSearch elasticSearch; | ||
@Inject | ||
ElasticSearchType<TestAggregationDocument> documentType; | ||
|
||
@BeforeAll | ||
void initialize() { | ||
documentType.index("1", document(ZonedDateTime.parse("2024-08-09T00:00:00Z"), "a1", "b1", 1)); | ||
documentType.index("2", document(ZonedDateTime.parse("2024-08-09T00:00:00Z"), "a2", "b2", 2)); | ||
documentType.index("3", document(ZonedDateTime.parse("2024-08-10T00:00:00Z"), "a1", "b2", 3)); | ||
documentType.index("4", document(ZonedDateTime.parse("2024-08-10T00:00:00Z"), "a2", "b2", 4)); | ||
documentType.index("5", document(ZonedDateTime.parse("2024-08-11T00:00:00Z"), "a1", "b1", 5)); | ||
documentType.index("6", document(ZonedDateTime.parse("2024-08-11T00:00:00Z"), "a1", "b2", 6)); | ||
elasticSearch.refreshIndex("aggregation_document"); | ||
} | ||
|
||
@AfterAll | ||
void cleanup() { | ||
var request = new DeleteByQueryRequest(); | ||
request.query = new Query.Builder().matchAll(b -> b).build(); | ||
request.refresh = true; | ||
documentType.deleteByQuery(request); | ||
} | ||
|
||
@Test | ||
void aggregate() { | ||
var request = new SearchRequest(); | ||
request.limit = 1; | ||
request.aggregations.put("total_value", sum("value")); | ||
SearchResponse<TestAggregationDocument> response = documentType.search(request); | ||
|
||
assertThat(response.totalHits).isEqualTo(6); | ||
assertThat(response.hits).hasSize(1); | ||
assertThat(response.aggregations).containsKeys("total_value"); | ||
|
||
int sum = (int) response.aggregations.get("total_value").sum().value(); | ||
assertThat(sum).isEqualTo(21); | ||
} | ||
|
||
@Test | ||
void subAggregate() { | ||
var request = new SearchRequest(); | ||
request.withJSON(ClasspathResources.text("search-test/sub-aggregation.json")); | ||
request.limit = 0; | ||
|
||
SearchResponse<TestAggregationDocument> response = documentType.search(request); | ||
List<DateHistogramBucket> dates = response.aggregations.get("date").dateHistogram().buckets().array(); | ||
assertThat(dates.getFirst().keyAsString()) | ||
.isEqualTo("2024-08-09T00:00:00.000Z"); | ||
assertThat(dates.getFirst().aggregations().get("key_1").sterms().buckets().array().getFirst().key().stringValue()) | ||
.isEqualTo("a2"); | ||
assertThat(dates.getFirst().aggregations().get("key_1").sterms().buckets().array().getFirst().aggregations().get("total_value").sum().value()) | ||
.isEqualTo(2); | ||
assertThat(dates.getFirst().aggregations().get("key_1").sterms().buckets().array().get(1).key().stringValue()) | ||
.isEqualTo("a1"); | ||
assertThat(dates.getFirst().aggregations().get("key_1").sterms().buckets().array().get(1).aggregations().get("total_value").sum().value()) | ||
.isEqualTo(1); | ||
|
||
assertThat(dates.get(2).keyAsString()) | ||
.isEqualTo("2024-08-11T00:00:00.000Z"); | ||
assertThat(dates.get(2).aggregations().get("key_1").sterms().buckets().array().getFirst().key().stringValue()) | ||
.isEqualTo("a1"); | ||
assertThat(dates.get(2).aggregations().get("key_1").sterms().buckets().array().getFirst().aggregations().get("total_value").sum().value()) | ||
.isEqualTo(11); | ||
} | ||
|
||
@Test | ||
void subAggregateWithRuntimeField() { | ||
var request = new SearchRequest(); | ||
request.withJSON(ClasspathResources.text("search-test/sub-aggregation-with-runtime-field.json")); | ||
request.limit = 0; | ||
|
||
SearchResponse<TestAggregationDocument> response = documentType.search(request); | ||
List<StringTermsBucket> buckets = response.aggregations.get("composited_key").sterms().buckets().array(); | ||
assertThat(buckets.getFirst().key().stringValue()) | ||
.isEqualTo("a1|b2"); | ||
assertThat(buckets.getFirst().aggregations().get("total_value").sum().value()) | ||
.isEqualTo(9); | ||
} | ||
|
||
private TestAggregationDocument document(ZonedDateTime date, String key1, String key2, int value) { | ||
var document = new TestAggregationDocument(); | ||
document.date = date; | ||
document.key1 = key1; | ||
document.key2 = key2; | ||
document.value = value; | ||
return document; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
core-ng-search-test/src/test/java/core/framework/search/impl/TestAggregationDocument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.framework.search.impl; | ||
|
||
import core.framework.api.json.Property; | ||
import core.framework.api.validate.NotNull; | ||
import core.framework.search.Index; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
@Index(name = "aggregation_document") | ||
public class TestAggregationDocument { | ||
@Property(name = "date") | ||
public ZonedDateTime date; | ||
|
||
@Property(name = "key_1") | ||
public String key1; | ||
|
||
@Property(name = "key_2") | ||
public String key2; | ||
|
||
@NotNull | ||
@Property(name = "value") | ||
public Integer value; | ||
} |
19 changes: 19 additions & 0 deletions
19
core-ng-search-test/src/test/resources/search-test/aggregation-document-index.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"mappings": { | ||
"properties": { | ||
"date": { | ||
"type": "date", | ||
"format": "strict_date_optional_time" | ||
}, | ||
"key_1": { | ||
"type": "keyword" | ||
}, | ||
"key_2": { | ||
"type": "keyword" | ||
}, | ||
"value": { | ||
"type": "integer" | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
core-ng-search-test/src/test/resources/search-test/sub-aggregation-with-runtime-field.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"runtime_mappings": { | ||
"composited_key": { | ||
"type": "keyword", | ||
"script": "emit(doc['key_1'].value + '|' + doc['key_2'].value)" | ||
} | ||
}, | ||
"aggregations": { | ||
"composited_key": { | ||
"terms": { | ||
"field": "composited_key", | ||
"order": [{"total_value": "desc"}] | ||
}, | ||
"aggregations": { | ||
"total_value": { | ||
"sum": {"field": "value"} | ||
} | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
core-ng-search-test/src/test/resources/search-test/sub-aggregation.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"aggregations": { | ||
"date": { | ||
"date_histogram": { | ||
"field": "date", | ||
"calendar_interval": "day" | ||
}, | ||
"aggregations": { | ||
"key_1": { | ||
"terms": { | ||
"field": "key_1", | ||
"order": [{"total_value": "desc"}] | ||
}, | ||
"aggregations": { | ||
"total_value": { | ||
"sum": {"field": "value"} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters