Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HSEARCH-3542 + HSEARCH-3543 Spatial-related fixes #1932

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.hibernate.SessionFactory;
import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.engine.spatial.GeoPoint;
import org.hibernate.search.mapper.pojo.bridge.builtin.spatial.annotation.GeoPointBridge;
import org.hibernate.search.mapper.pojo.bridge.builtin.spatial.annotation.Latitude;
Expand Down Expand Up @@ -72,7 +73,8 @@ public BridgeUsingPropertyMarkerAccessIT(ModelPrimitives<TIndexed> modelPrimitiv
@Before
public void setup() {
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "location", GeoPoint.class,
b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
);
sessionFactory = ormSetupHelper.withBackendMock( backendMock )
.setup( modelPrimitives.getModelClass() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.integrationtest.mapper.pojo.spatial;

import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.engine.spatial.GeoPoint;
import org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper;
import org.hibernate.search.mapper.javabean.JavaBeanMapping;
Expand Down Expand Up @@ -38,16 +39,16 @@ public class AnnotationMappingGeoPointBridgeIT {
@Before
public void setup() {
backendMock.expectSchema( GeoPointOnTypeEntity.INDEX, b -> b
.field( "homeLocation", GeoPoint.class, b2 -> b2.projectable( Projectable.YES ) )
.field( "workLocation", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "homeLocation", GeoPoint.class, b2 -> b2.projectable( Projectable.YES ).sortable( Sortable.YES ) )
.field( "workLocation", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
);
backendMock.expectSchema( GeoPointOnCoordinatesPropertyEntity.INDEX, b -> b
.field( "coord", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.NO ) )
.field( "coord", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.NO ).sortable( Sortable.DEFAULT ) )
);
backendMock.expectSchema( GeoPointOnCustomCoordinatesPropertyEntity.INDEX, b -> b
.field( "coord", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "coord", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
);

mapping = setupHelper.withBackendMock( backendMock )
Expand Down Expand Up @@ -124,7 +125,7 @@ public double getLongitude() {
}

@Indexed(index = GeoPointOnTypeEntity.INDEX)
@GeoPointBridge(fieldName = "homeLocation", markerSet = "home", projectable = Projectable.YES)
@GeoPointBridge(fieldName = "homeLocation", markerSet = "home", projectable = Projectable.YES, sortable = Sortable.YES)
@GeoPointBridge(fieldName = "workLocation", markerSet = "work")
public static final class GeoPointOnTypeEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.integrationtest.mapper.pojo.spatial;

import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.mapper.javabean.JavaBeanMapping;
import org.hibernate.search.mapper.pojo.bridge.builtin.spatial.GeoPointBridgeBuilder;
import org.hibernate.search.mapper.javabean.session.SearchSession;
Expand Down Expand Up @@ -36,16 +37,16 @@ public class ProgrammaticMappingGeoPointBridgeIT {
@Before
public void setup() {
backendMock.expectSchema( GeoPointOnTypeEntity.INDEX, b -> b
.field( "homeLocation", GeoPoint.class, b2 -> b2.projectable( Projectable.YES ) )
.field( "workLocation", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "homeLocation", GeoPoint.class, b2 -> b2.projectable( Projectable.YES ).sortable( Sortable.YES ) )
.field( "workLocation", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
);
backendMock.expectSchema( GeoPointOnCoordinatesPropertyEntity.INDEX, b -> b
.field( "coord", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.NO ) )
.field( "coord", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.NO ).sortable( Sortable.DEFAULT ) )
);
backendMock.expectSchema( GeoPointOnCustomCoordinatesPropertyEntity.INDEX, b -> b
.field( "coord", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ) )
.field( "coord", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
.field( "location", GeoPoint.class, b2 -> b2.projectable( Projectable.DEFAULT ).sortable( Sortable.DEFAULT ) )
);

mapping = setupHelper.withBackendMock( backendMock )
Expand All @@ -63,6 +64,7 @@ public void setup() {
.fieldName( "homeLocation" )
.markerSet( "home" )
.projectable( Projectable.YES )
.sortable( Sortable.YES )
)
.bridge(
GeoPointBridgeBuilder.forType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
@Entity
@Indexed(index = Library.INDEX)
@GeoPointBridge(fieldName = "location")
@GeoPointBridge(fieldName = "location", sortable = Sortable.YES)
public class Library extends AbstractEntity<Integer> {

static final String INDEX = "Library";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ public List<Document<?>> searchAroundMe(String terms, String tags,
}
} ) )
// TODO facets (tag, medium, library in particular)
.sort( b -> b.byScore() )
.sort( b -> {
if ( myLocation != null ) {
// TODO HSEARCH-2254 sort by distance once we implement nested support for sorts ("copies" is a nested object field)
//b.byDistance( "copies.library.location", myLocation );
}
b.byScore();
} )
.toQuery();

return query.fetchHits( limit, offset );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ public void searchAroundMe_spatial() {
0, 10
);
// Should only include content from university
assertThat( documents ).extracting( Document::getId ).containsExactly(
// TODO HSEARCH-2254 check results are sorted by distance once we do sort them (see the query implementation)
assertThat( documents ).extracting( Document::getId ).containsExactlyInAnyOrder(
INDONESIAN_ECONOMY_ID,
JAVA_FOR_DUMMIES_ID,
ART_OF_COMPUTER_PROG_ID,
Expand All @@ -234,7 +235,8 @@ public void searchAroundMe_spatial() {
0, 10
);
// Should only include content from suburb1 or university
assertThat( documents ).extracting( Document::getId ).containsExactly(
// TODO HSEARCH-2254 check results are sorted by distance once we do sort them (see the query implementation)
assertThat( documents ).extracting( Document::getId ).containsExactlyInAnyOrder(
CALLIGRAPHY_ID,
INDONESIAN_ECONOMY_ID,
JAVA_FOR_DUMMIES_ID,
Expand All @@ -249,7 +251,8 @@ public void searchAroundMe_spatial() {
0, 10
);
// Should only include content from suburb1 or university with "calligraphy" in it
assertThat( documents ).extracting( Document::getId ).containsExactly(
// TODO HSEARCH-2254 check results are sorted by distance once we do sort them (see the query implementation)
assertThat( documents ).extracting( Document::getId ).containsExactlyInAnyOrder(
CALLIGRAPHY_ID
);

Expand All @@ -261,7 +264,8 @@ public void searchAroundMe_spatial() {
0, 10
);
// Should only include content from university
assertThat( documents ).extracting( Document::getId ).containsExactly(
// TODO HSEARCH-2254 check results are sorted by distance once we do sort them (see the query implementation)
assertThat( documents ).extracting( Document::getId ).containsExactlyInAnyOrder(
INDONESIAN_ECONOMY_ID,
JAVA_FOR_DUMMIES_ID,
ART_OF_COMPUTER_PROG_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.mapper.pojo.bridge.builtin.spatial;

import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.mapper.pojo.bridge.PropertyBridge;
import org.hibernate.search.mapper.pojo.bridge.TypeBridge;
import org.hibernate.search.mapper.pojo.bridge.builtin.spatial.impl.GeoPointBridge;
Expand All @@ -20,6 +21,8 @@ public interface GeoPointBridgeBuilder<B> extends BridgeBuilder<B> {

GeoPointBridgeBuilder<B> projectable(Projectable projectable);

GeoPointBridgeBuilder<B> sortable(Sortable sortable);

GeoPointBridgeBuilder<B> markerSet(String markerSet);

static GeoPointBridgeBuilder<? extends TypeBridge> forType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.lang.annotation.Target;

import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.mapper.pojo.bridge.declaration.PropertyBridgeMapping;
import org.hibernate.search.mapper.pojo.bridge.declaration.PropertyBridgeRef;
import org.hibernate.search.mapper.pojo.bridge.declaration.TypeBridgeMapping;
Expand Down Expand Up @@ -90,6 +91,12 @@
*/
Projectable projectable() default Projectable.DEFAULT;

/**
* @return Returns an instance of the {@link Sortable} enum, indicating whether sorts are enabled for this
* field. Defaults to {@code Sortable.DEFAULT}.
*/
Sortable sortable() default Sortable.DEFAULT;

/**
* @return The name of the marker set this spatial should look into
* when looking for the {@link Latitude} and {@link Longitude} markers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.search.engine.backend.document.IndexFieldReference;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.engine.backend.types.dsl.IndexFieldTypeFactoryContext;
import org.hibernate.search.engine.environment.bean.BeanHolder;
import org.hibernate.search.mapper.pojo.bridge.PropertyBridge;
Expand Down Expand Up @@ -46,6 +47,7 @@ public static class Builder implements GeoPointBridgeBuilder<GeoPointBridge>,

private String fieldName;
private Projectable projectable = Projectable.DEFAULT;
private Sortable sortable = Sortable.DEFAULT;
private String markerSet;

@Override
Expand All @@ -54,6 +56,7 @@ public void initialize(
fieldName( annotation.fieldName() );
markerSet( annotation.markerSet() );
projectable( annotation.projectable() );
sortable( annotation.sortable() );
}

@Override
Expand All @@ -68,6 +71,12 @@ public Builder projectable(Projectable projectable) {
return this;
}

@Override
public Builder sortable(Sortable sortable) {
this.sortable = sortable;
return this;
}

@Override
public Builder markerSet(String markerSet) {
this.markerSet = markerSet;
Expand All @@ -76,12 +85,13 @@ public Builder markerSet(String markerSet) {

@Override
public BeanHolder<? extends GeoPointBridge> build(BridgeBuildContext buildContext) {
return BeanHolder.of( new GeoPointBridge( fieldName, projectable, markerSet ) );
return BeanHolder.of( new GeoPointBridge( fieldName, projectable, sortable, markerSet ) );
}
}

private final String fieldName;
private final Projectable projectable;
private final Sortable sortable;
private final String markerSet;

private IndexFieldReference<GeoPoint> indexFieldReference;
Expand All @@ -90,9 +100,10 @@ public BeanHolder<? extends GeoPointBridge> build(BridgeBuildContext buildContex
/**
* Private constructor, use {@link GeoPointBridgeBuilder#forType()} or {@link GeoPointBridgeBuilder#forProperty()} instead.
*/
private GeoPointBridge(String fieldName, Projectable projectable, String markerSet) {
private GeoPointBridge(String fieldName, Projectable projectable, Sortable sortable, String markerSet) {
this.fieldName = fieldName;
this.projectable = projectable;
this.sortable = sortable;
this.markerSet = markerSet;
}

Expand Down Expand Up @@ -123,7 +134,7 @@ private void bind(String defaultedFieldName, IndexFieldTypeFactoryContext typeFa
PojoModelCompositeElement bridgedPojoModelElement) {
indexFieldReference = indexSchemaElement.field(
defaultedFieldName,
typeFactoryContext.asGeoPoint().projectable( projectable ).toIndexFieldType()
typeFactoryContext.asGeoPoint().projectable( projectable ).sortable( sortable ).toIndexFieldType()
)
.toReference();

Expand Down