Search in sources :

Example 1 with ValueConvert

use of org.hibernate.search.engine.search.common.ValueConvert in project hibernate-search by hibernate.

the class TermsAggregationDescriptor method getSingleFieldAggregationExpectations.

@Override
public <F> ExpectationsAlternative<SupportedSingleFieldAggregationExpectations<F>, UnsupportedSingleFieldAggregationExpectations> getSingleFieldAggregationExpectations(FieldTypeDescriptor<F> typeDescriptor) {
    if (AnalyzedStringFieldTypeDescriptor.class.equals(typeDescriptor.getClass()) || GeoPoint.class.equals(typeDescriptor.getJavaType())) {
        // Terms aggregations are not supported on analyzed or GeoPoint fields
        return ExpectationsAlternative.unsupported(unsupportedExpectations(typeDescriptor));
    }
    List<F> uniqueTermValues = new ArrayList<>(typeDescriptor.getAscendingUniqueTermValues().getSingle());
    // Mess with the value order, because it should not matter
    uniqueTermValues.add(uniqueTermValues.get(0));
    uniqueTermValues.remove(0);
    uniqueTermValues.add(uniqueTermValues.get(0));
    uniqueTermValues.remove(0);
    List<F> mainIndexDocumentFieldValues = new ArrayList<>();
    List<F> otherIndexDocumentFieldValues = new ArrayList<>();
    List<List<F>> multiValuedIndexDocumentFieldValues = new ArrayList<>();
    Map<F, Long> mainIndexExpected = new LinkedHashMap<>();
    Map<F, Long> mainAndOtherIndexExpected = new LinkedHashMap<>();
    Map<F, Long> multiValuedIndexExpected = new LinkedHashMap<>();
    // Simple dataset for the main index: strictly decreasing number of documents for each term
    long numberOfDocuments = uniqueTermValues.size();
    for (F uniqueTermValue : uniqueTermValues) {
        for (int i = 0; i < numberOfDocuments; i++) {
            mainIndexDocumentFieldValues.add(uniqueTermValue);
        }
        mainIndexExpected.put(typeDescriptor.toExpectedDocValue(uniqueTermValue), numberOfDocuments);
        --numberOfDocuments;
    }
    // For the other index, make sure not to break the "strictly decreasing" property of the map.
    // Just add one term for the terms with the highest and second-highest counts.
    F termWithHighestCount = uniqueTermValues.get(0);
    F termWithSecondHighestCount = uniqueTermValues.get(1);
    otherIndexDocumentFieldValues.add(termWithHighestCount);
    otherIndexDocumentFieldValues.add(termWithSecondHighestCount);
    mainAndOtherIndexExpected.putAll(mainIndexExpected);
    mainAndOtherIndexExpected.compute(typeDescriptor.toExpectedDocValue(termWithHighestCount), (key, count) -> count + 1);
    mainAndOtherIndexExpected.compute(typeDescriptor.toExpectedDocValue(termWithSecondHighestCount), (key, count) -> count + 1);
    // Dataset and expectations for the multi-valued index
    // Single-valued documents
    multiValuedIndexDocumentFieldValues.add(Arrays.asList(uniqueTermValues.get(0)));
    multiValuedIndexDocumentFieldValues.add(Arrays.asList(uniqueTermValues.get(1)));
    multiValuedIndexDocumentFieldValues.add(// Document matching two different buckets
    Arrays.asList(uniqueTermValues.get(0), uniqueTermValues.get(1)));
    multiValuedIndexDocumentFieldValues.add(// Document matching the same bucket twice
    Arrays.asList(uniqueTermValues.get(0), uniqueTermValues.get(0)));
    multiValuedIndexExpected.put(typeDescriptor.toExpectedDocValue(uniqueTermValues.get(0)), 3L);
    multiValuedIndexExpected.put(typeDescriptor.toExpectedDocValue(uniqueTermValues.get(1)), 2L);
    return ExpectationsAlternative.supported(new SupportedSingleFieldAggregationExpectations<F>(typeDescriptor, "terms", mainIndexDocumentFieldValues, otherIndexDocumentFieldValues, multiValuedIndexDocumentFieldValues) {

        @Override
        public <T> AggregationScenario<Map<T, Long>> withFieldType(TypeAssertionHelper<F, T> helper) {
            return doCreate(mainIndexExpected, helper);
        }

        @Override
        public <T> AggregationScenario<Map<T, Long>> withFieldTypeOnMainAndOtherIndex(TypeAssertionHelper<F, T> helper) {
            return doCreate(mainAndOtherIndexExpected, helper);
        }

        @Override
        public AggregationScenario<?> withoutMatch() {
            return doCreate(Collections.emptyMap(), TypeAssertionHelper.identity(fieldType()));
        }

        @Override
        public AggregationScenario<?> onMultiValuedIndex() {
            return doCreate(multiValuedIndexExpected, TypeAssertionHelper.identity(fieldType()));
        }

        private <T> AggregationScenario<Map<T, Long>> doCreate(Map<F, Long> expectedResult, TypeAssertionHelper<F, T> helper) {
            return new AggregationScenario<Map<T, Long>>() {

                @Override
                public AggregationFinalStep<Map<T, Long>> setup(SearchAggregationFactory factory, String fieldPath, Function<? super SearchPredicateFactory, ? extends PredicateFinalStep> filterOrNull) {
                    TermsAggregationOptionsStep<?, ?, ?, Map<T, Long>> optionsStep = factory.terms().field(fieldPath, helper.getJavaClass());
                    if (filterOrNull == null) {
                        return optionsStep;
                    } else {
                        return optionsStep.filter(filterOrNull);
                    }
                }

                @Override
                public AggregationFinalStep<Map<T, Long>> setupWithConverterSetting(SearchAggregationFactory factory, String fieldPath, ValueConvert convert) {
                    return factory.terms().field(fieldPath, helper.getJavaClass(), convert);
                }

                @Override
                public void check(Map<T, Long> aggregationResult) {
                    @SuppressWarnings("unchecked") Map.Entry<Range<T>, Long>[] expectedEntries = NormalizationUtils.normalize(expectedResult).entrySet().stream().map(e -> entry(helper.create(e.getKey()), e.getValue())).toArray(Map.Entry[]::new);
                    @SuppressWarnings("unchecked") Map.Entry<Range<T>, Long>[] actualEntries = NormalizationUtils.normalize(aggregationResult).entrySet().toArray(new Map.Entry[0]);
                    // Don't check the order, this is tested separately
                    assertThat(actualEntries).containsOnly(expectedEntries);
                }
            };
        }
    });
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) GeoPoint(org.hibernate.search.engine.spatial.GeoPoint) AggregationScenario(org.hibernate.search.integrationtest.backend.tck.testsupport.operations.expectations.AggregationScenario) ArrayList(java.util.ArrayList) List(java.util.List) TermsAggregationOptionsStep(org.hibernate.search.engine.search.aggregation.dsl.TermsAggregationOptionsStep) AggregationFinalStep(org.hibernate.search.engine.search.aggregation.dsl.AggregationFinalStep) Range(org.hibernate.search.util.common.data.Range) GeoPoint(org.hibernate.search.engine.spatial.GeoPoint) AnalyzedStringFieldTypeDescriptor(org.hibernate.search.integrationtest.backend.tck.testsupport.types.AnalyzedStringFieldTypeDescriptor) SearchAggregationFactory(org.hibernate.search.engine.search.aggregation.dsl.SearchAggregationFactory) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ValueConvert(org.hibernate.search.engine.search.common.ValueConvert)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 AggregationFinalStep (org.hibernate.search.engine.search.aggregation.dsl.AggregationFinalStep)1 SearchAggregationFactory (org.hibernate.search.engine.search.aggregation.dsl.SearchAggregationFactory)1 TermsAggregationOptionsStep (org.hibernate.search.engine.search.aggregation.dsl.TermsAggregationOptionsStep)1 ValueConvert (org.hibernate.search.engine.search.common.ValueConvert)1 GeoPoint (org.hibernate.search.engine.spatial.GeoPoint)1 AggregationScenario (org.hibernate.search.integrationtest.backend.tck.testsupport.operations.expectations.AggregationScenario)1 AnalyzedStringFieldTypeDescriptor (org.hibernate.search.integrationtest.backend.tck.testsupport.types.AnalyzedStringFieldTypeDescriptor)1 Range (org.hibernate.search.util.common.data.Range)1