forked from hibernate/hibernate-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-5133 Test temporal metric aggregations
- Loading branch information
Showing
1 changed file
with
111 additions
and
0 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
...rationtest/backend/elasticsearch/tmp/ElasticsearchMetricTemporalFieldsAggregationsIT.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,111 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.search.integrationtest.backend.elasticsearch.tmp; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Month; | ||
|
||
import org.hibernate.search.engine.backend.common.DocumentReference; | ||
import org.hibernate.search.engine.backend.document.DocumentElement; | ||
import org.hibernate.search.engine.backend.document.IndexFieldReference; | ||
import org.hibernate.search.engine.backend.document.IndexObjectFieldReference; | ||
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement; | ||
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField; | ||
import org.hibernate.search.engine.backend.types.Aggregable; | ||
import org.hibernate.search.engine.backend.types.ObjectStructure; | ||
import org.hibernate.search.engine.search.query.SearchQuery; | ||
import org.hibernate.search.engine.search.query.SearchResult; | ||
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.extension.SearchSetupHelper; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.stub.BulkIndexer; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
public class ElasticsearchMetricTemporalFieldsAggregationsIT { | ||
|
||
@RegisterExtension | ||
public static final SearchSetupHelper setupHelper = SearchSetupHelper.create(); | ||
|
||
private final SimpleMappedIndex<IndexBinding> mainIndex = SimpleMappedIndex.of( IndexBinding::new ).name( "main" ); | ||
|
||
@BeforeEach | ||
void setup() { | ||
setupHelper.start().withIndexes( mainIndex ).setup().integration(); | ||
initData(); | ||
} | ||
|
||
@Test | ||
public void test_filteringResults() { | ||
StubMappingScope scope = mainIndex.createScope(); | ||
SearchQuery<DocumentReference> query = scope.query() | ||
.where( f -> f.match().field( "style" ).matching( "bla" ) ) | ||
.toQuery(); | ||
|
||
SearchResult<DocumentReference> result = query.fetch( 0 ); | ||
assertThat( result ).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void test_allResults() { | ||
StubMappingScope scope = mainIndex.createScope(); | ||
SearchQuery<DocumentReference> query = scope.query() | ||
.where( f -> f.matchAll() ) | ||
.toQuery(); | ||
|
||
SearchResult<DocumentReference> result = query.fetch( 0 ); | ||
assertThat( result ).isNotNull(); | ||
} | ||
|
||
private void initData() { | ||
LocalDate baseDate = LocalDate.of( 2016, Month.DECEMBER, 6 ); | ||
int[] integers = new int[] { 9, 18, 3, 18, 7, -10, 3, 0, 7, 0 }; | ||
String[] styles = new String[] { "bla", "aaa" }; | ||
|
||
BulkIndexer bulkIndexer = mainIndex.bulkIndexer(); | ||
for ( int i = 0; i < integers.length; i++ ) { | ||
int value = integers[i]; | ||
String style = styles[i % 2]; | ||
String id = i + ":" + value + ":" + style; | ||
LocalDate date = baseDate.plusDays( i ); | ||
|
||
bulkIndexer.add( id, document -> { | ||
document.addValue( mainIndex.binding().date, date ); | ||
document.addValue( mainIndex.binding().converted, date ); | ||
document.addValue( mainIndex.binding().style, style ); | ||
|
||
DocumentElement object = document.addObject( mainIndex.binding().object ); | ||
object.addValue( mainIndex.binding().nestedDate, date ); | ||
} ); | ||
} | ||
bulkIndexer.add( "empty", document -> {} ) | ||
.join(); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private static class IndexBinding { | ||
final IndexFieldReference<LocalDate> date; | ||
final IndexFieldReference<LocalDate> converted; | ||
final IndexFieldReference<String> style; | ||
final IndexObjectFieldReference object; | ||
final IndexFieldReference<LocalDate> nestedDate; | ||
|
||
IndexBinding(IndexSchemaElement root) { | ||
date = root.field( "date", f -> f.asLocalDate().aggregable( Aggregable.YES ) ).toReference(); | ||
converted = root.field( "converted", f -> f.asLocalDate().aggregable( Aggregable.YES ) | ||
.projectionConverter( String.class, (value, context) -> value.toString() ) ).toReference(); | ||
style = root.field( "style", f -> f.asString() ).toReference(); | ||
|
||
IndexSchemaObjectField nested = root.objectField( "object", ObjectStructure.NESTED ); | ||
object = nested.toReference(); | ||
nestedDate = nested.field( "nestedDate", f -> f.asLocalDate().aggregable( Aggregable.YES ) ) | ||
.toReference(); | ||
} | ||
} | ||
} |