Search in sources :

Example 1 with GeoPoint

use of org.hibernate.search.engine.spatial.GeoPoint in project hibernate-search by hibernate.

the class DistanceSearchProjectionComplexCasesIT method withDistanceSort.

/**
 * See also {@link DistanceSearchProjectionSingleValuedBaseIT#sortable_withSort()}.
 * <p>
 * The main difference is that we're composing multiple sorts here.
 */
@Test
public void withDistanceSort() {
    StubMappingScope scope = mainIndex.createScope();
    GeoPoint center = GeoPoint.of(45.749828, 4.854172);
    assertThatQuery(scope.query().select(f -> f.distance("geoPoint", center)).where(f -> f.matchAll()).sort(f -> f.distance("geoPoint", GeoPoint.of(43.749828, 1.854172)).then().distance("geoPoint", center)).toQuery()).hits().asIs().usingElementComparator(APPROX_M_COMPARATOR).containsExactly(1300d, 430d, 2730d, null);
}
Also used : List(java.util.List) DistanceUnit(org.hibernate.search.engine.spatial.DistanceUnit) GeoPoint(org.hibernate.search.engine.spatial.GeoPoint) ListAssert(org.assertj.core.api.ListAssert) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) Test(org.junit.Test) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) DistanceSearchProjectionSingleValuedBaseIT(org.hibernate.search.integrationtest.backend.tck.search.projection.DistanceSearchProjectionSingleValuedBaseIT) TestComparators(org.hibernate.search.util.impl.integrationtest.common.assertion.TestComparators) Comparator(java.util.Comparator) AbstractSpatialWithinSearchPredicateIT(org.hibernate.search.integrationtest.backend.tck.search.predicate.AbstractSpatialWithinSearchPredicateIT) GeoPoint(org.hibernate.search.engine.spatial.GeoPoint) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) Test(org.junit.Test)

Example 2 with GeoPoint

use of org.hibernate.search.engine.spatial.GeoPoint in project hibernate-search by hibernate.

the class DistanceSearchProjectionComplexCasesIT method several.

/**
 * See also {@link DistanceSearchProjectionSingleValuedBaseIT#several()}.
 * <p>
 * The main difference is that we're targeting multiple fields here.
 */
@Test
public void several() {
    StubMappingScope scope = mainIndex.createScope();
    ListAssert<List<?>> hitsAssert = assertThatQuery(scope.query().select(f -> f.composite(f.distance("geoPoint", GeoPoint.of(45.749828, 4.854172)), f.distance("geoPoint", GeoPoint.of(45.763363, 4.833527)), f.distance("geoPoint_1", GeoPoint.of(45.749828, 4.854172)).unit(DistanceUnit.KILOMETERS))).where(f -> f.matchAll()).sort(f -> f.field("string").missing().last().asc()).toQuery()).hits().asIs();
    hitsAssert.extracting(tuple -> (Double) tuple.get(0)).usingElementComparator(APPROX_M_COMPARATOR).containsExactlyInAnyOrder(430d, 1300d, 2730d, null);
    hitsAssert.extracting(tuple -> (Double) tuple.get(1)).usingElementComparator(APPROX_M_COMPARATOR).containsExactlyInAnyOrder(1780d, 1095d, 812d, null);
    hitsAssert.extracting(tuple -> (Double) tuple.get(2)).usingElementComparator(APPROX_KM_COMPARATOR).containsExactlyInAnyOrder(135.834, 136.294, 134.967, null);
}
Also used : List(java.util.List) DistanceUnit(org.hibernate.search.engine.spatial.DistanceUnit) GeoPoint(org.hibernate.search.engine.spatial.GeoPoint) ListAssert(org.assertj.core.api.ListAssert) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) Test(org.junit.Test) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) DistanceSearchProjectionSingleValuedBaseIT(org.hibernate.search.integrationtest.backend.tck.search.projection.DistanceSearchProjectionSingleValuedBaseIT) TestComparators(org.hibernate.search.util.impl.integrationtest.common.assertion.TestComparators) Comparator(java.util.Comparator) AbstractSpatialWithinSearchPredicateIT(org.hibernate.search.integrationtest.backend.tck.search.predicate.AbstractSpatialWithinSearchPredicateIT) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) List(java.util.List) Test(org.junit.Test)

Example 3 with GeoPoint

use of org.hibernate.search.engine.spatial.GeoPoint in project hibernate-search by hibernate.

the class ElasticsearchDistanceToFieldProjection method computeDistanceWithUnit.

private Double computeDistanceWithUnit(JsonElement geoPoint) {
    GeoPoint decoded = CODEC.decode(geoPoint);
    if (decoded == null) {
        return null;
    }
    double distanceInMeters = SloppyMath.haversinMeters(center.latitude(), center.longitude(), decoded.latitude(), decoded.longitude());
    return unit.fromMeters(distanceInMeters);
}
Also used : GeoPoint(org.hibernate.search.engine.spatial.GeoPoint)

Example 4 with GeoPoint

use of org.hibernate.search.engine.spatial.GeoPoint in project hibernate-search by hibernate.

the class SearchQueryResultLoadingOrTransformingIT method initData.

private void initData() {
    index.bulkIndexer().add(MAIN_ID, document -> {
        document.addValue(index.binding().string, STRING_VALUE);
        document.addValue(index.binding().string_analyzed, STRING_ANALYZED_VALUE);
        document.addValue(index.binding().integer, INTEGER_VALUE);
        document.addValue(index.binding().localDate, LOCAL_DATE_VALUE);
        document.addValue(index.binding().geoPoint, GEO_POINT_VALUE);
        // Note: this object must be single-valued for these tests
        DocumentElement flattenedObject = document.addObject(index.binding().flattenedObject.self);
        flattenedObject.addValue(index.binding().flattenedObject.string, FLATTENED_OBJECT_STRING_VALUE);
        flattenedObject.addValue(index.binding().flattenedObject.integer, FLATTENED_OBJECT_INTEGER_VALUE);
        // Note: this object must be single-valued for these tests
        DocumentElement nestedObject = document.addObject(index.binding().nestedObject.self);
        nestedObject.addValue(index.binding().nestedObject.string, NESTED_OBJECT_STRING_VALUE);
        nestedObject.addValue(index.binding().nestedObject.integer, NESTED_OBJECT_INTEGER_VALUE);
    }).add(EMPTY_ID, document -> {
    }).join();
}
Also used : Strictness(org.mockito.quality.Strictness) MockUtils.projectionMatcher(org.hibernate.search.util.impl.integrationtest.common.MockUtils.projectionMatcher) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) GeoPoint(org.hibernate.search.engine.spatial.GeoPoint) StubDocumentReferenceConverter(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubDocumentReferenceConverter) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) Function(java.util.function.Function) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) StubHitTransformer(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubHitTransformer) StubTransformedReference(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubTransformedReference) CALLS_REAL_METHODS(org.mockito.Mockito.CALLS_REAL_METHODS) NormalizationUtils.reference(org.hibernate.search.util.impl.integrationtest.common.NormalizationUtils.reference) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) MockitoJUnit(org.mockito.junit.MockitoJUnit) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) Before(org.junit.Before) SearchScroll(org.hibernate.search.engine.search.query.SearchScroll) MapperMockUtils.expectHitMapping(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.MapperMockUtils.expectHitMapping) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IndexSchemaElement(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement) IndexSchemaObjectField(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField) Mockito.verify(org.mockito.Mockito.verify) GenericStubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.GenericStubMappingScope) TimeUnit(java.util.concurrent.TimeUnit) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) StubTransformedHit(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubTransformedHit) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Rule(org.junit.Rule) DocumentReferenceConverter(org.hibernate.search.engine.backend.common.spi.DocumentReferenceConverter) StubLoadedObject(org.hibernate.search.integrationtest.backend.tck.testsupport.stub.StubLoadedObject) LocalDate(java.time.LocalDate) SearchHitsAssert.assertThatHits(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) MockitoRule(org.mockito.junit.MockitoRule) Mockito.reset(org.mockito.Mockito.reset) Assert.assertEquals(org.junit.Assert.assertEquals) Projectable(org.hibernate.search.engine.backend.types.Projectable) DefaultAnalysisDefinitions(org.hibernate.search.integrationtest.backend.tck.testsupport.configuration.DefaultAnalysisDefinitions) Mockito.mock(org.mockito.Mockito.mock) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement)

Example 5 with GeoPoint

use of org.hibernate.search.engine.spatial.GeoPoint in project hibernate-search by hibernate.

the class DistanceProjectionComplexCasesIT method several.

/**
 * See also {@link DistanceProjectionSingleValuedBaseIT#several()}.
 * <p>
 * The main difference is that we're targeting multiple fields here.
 */
@Test
public void several() {
    StubMappingScope scope = mainIndex.createScope();
    ListAssert<List<?>> hitsAssert = assertThatQuery(scope.query().select(f -> f.composite(f.distance("geoPoint", GeoPoint.of(45.749828, 4.854172)), f.distance("geoPoint", GeoPoint.of(45.763363, 4.833527)), f.distance("geoPoint_1", GeoPoint.of(45.749828, 4.854172)).unit(DistanceUnit.KILOMETERS))).where(f -> f.matchAll()).sort(f -> f.field("string").missing().last().asc()).toQuery()).hits().asIs();
    hitsAssert.extracting(tuple -> (Double) tuple.get(0)).usingElementComparator(APPROX_M_COMPARATOR).containsExactlyInAnyOrder(430d, 1300d, 2730d, null);
    hitsAssert.extracting(tuple -> (Double) tuple.get(1)).usingElementComparator(APPROX_M_COMPARATOR).containsExactlyInAnyOrder(1780d, 1095d, 812d, null);
    hitsAssert.extracting(tuple -> (Double) tuple.get(2)).usingElementComparator(APPROX_KM_COMPARATOR).containsExactlyInAnyOrder(135.834, 136.294, 134.967, null);
}
Also used : List(java.util.List) DistanceUnit(org.hibernate.search.engine.spatial.DistanceUnit) GeoPoint(org.hibernate.search.engine.spatial.GeoPoint) ListAssert(org.assertj.core.api.ListAssert) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) Test(org.junit.Test) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) TestComparators(org.hibernate.search.util.impl.integrationtest.common.assertion.TestComparators) Comparator(java.util.Comparator) DistanceProjectionSingleValuedBaseIT(org.hibernate.search.integrationtest.backend.tck.search.projection.DistanceProjectionSingleValuedBaseIT) AbstractSpatialWithinPredicateIT(org.hibernate.search.integrationtest.backend.tck.search.predicate.AbstractSpatialWithinPredicateIT) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) List(java.util.List) Test(org.junit.Test)

Aggregations

GeoPoint (org.hibernate.search.engine.spatial.GeoPoint)29 Test (org.junit.Test)21 List (java.util.List)15 SearchResultAssert.assertThatQuery (org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery)13 StubMappingScope (org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope)13 Before (org.junit.Before)13 Rule (org.junit.Rule)13 DocumentReference (org.hibernate.search.engine.backend.common.DocumentReference)11 IndexFieldReference (org.hibernate.search.engine.backend.document.IndexFieldReference)9 IndexSchemaElement (org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement)9 SearchQuery (org.hibernate.search.engine.search.query.SearchQuery)9 SearchSetupHelper (org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper)9 SimpleMappedIndex (org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex)9 Consumer (java.util.function.Consumer)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 Projectable (org.hibernate.search.engine.backend.types.Projectable)7 SearchHitsAssert.assertThatHits (org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits)7 ArrayList (java.util.ArrayList)6 DocumentElement (org.hibernate.search.engine.backend.document.DocumentElement)6 IndexObjectFieldReference (org.hibernate.search.engine.backend.document.IndexObjectFieldReference)6