diff --git a/integrationtest/backend/elasticsearch/src/test/java/org/hibernate/search/integrationtest/backend/elasticsearch/tmp/ElasticsearchMetricTemporalFieldsAggregationsIT.java b/integrationtest/backend/elasticsearch/src/test/java/org/hibernate/search/integrationtest/backend/elasticsearch/tmp/ElasticsearchMetricTemporalFieldsAggregationsIT.java new file mode 100644 index 00000000000..de8bd39e047 --- /dev/null +++ b/integrationtest/backend/elasticsearch/src/test/java/org/hibernate/search/integrationtest/backend/elasticsearch/tmp/ElasticsearchMetricTemporalFieldsAggregationsIT.java @@ -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 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 query = scope.query() + .where( f -> f.match().field( "style" ).matching( "bla" ) ) + .toQuery(); + + SearchResult result = query.fetch( 0 ); + assertThat( result ).isNotNull(); + } + + @Test + public void test_allResults() { + StubMappingScope scope = mainIndex.createScope(); + SearchQuery query = scope.query() + .where( f -> f.matchAll() ) + .toQuery(); + + SearchResult 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 date; + final IndexFieldReference converted; + final IndexFieldReference style; + final IndexObjectFieldReference object; + final IndexFieldReference 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(); + } + } +}