-
Notifications
You must be signed in to change notification settings - Fork 246
HSEARCH-3319 WIP: DRAFT: IDEA: TEST: Type-safe field references #4113
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
Draft
marko-bekhta
wants to merge
3
commits into
hibernate:main
Choose a base branch
from
marko-bekhta:feat/HSEARCH-3319-Type-safe-field-references
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
254 changes: 254 additions & 0 deletions
254
...n/src/test/java/org/hibernate/search/documentation/search/predicate/FieldReferenceIT.java
This file contains hidden or 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,254 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.search.predicate; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with; | ||
|
||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import jakarta.persistence.Id; | ||
|
||
import org.hibernate.search.documentation.testsupport.BackendConfigurations; | ||
import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper; | ||
import org.hibernate.search.engine.backend.types.Projectable; | ||
import org.hibernate.search.engine.search.common.ValueConvert; | ||
import org.hibernate.search.engine.search.predicate.SearchPredicate; | ||
import org.hibernate.search.engine.search.reference.traits.predicate.MatchPredicateFieldReference; | ||
import org.hibernate.search.engine.search.reference.traits.projection.FieldProjectionFieldReference; | ||
import org.hibernate.search.mapper.orm.Search; | ||
import org.hibernate.search.mapper.orm.scope.SearchScope; | ||
import org.hibernate.search.mapper.orm.session.SearchSession; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class FieldReferenceIT { | ||
|
||
@RegisterExtension | ||
public DocumentationSetupHelper setupHelper = DocumentationSetupHelper.withSingleBackend( BackendConfigurations.simple() ); | ||
|
||
private EntityManagerFactory entityManagerFactory; | ||
|
||
@BeforeEach | ||
void setup() { | ||
entityManagerFactory = setupHelper.start().setup( EntityA.class, EntityB.class, EntityC.class ); | ||
initData(); | ||
} | ||
|
||
@Test | ||
void smoke() { | ||
withinSearchSession( searchSession -> { | ||
assertThat( | ||
searchSession.search( EntityA.class ) | ||
.select( f -> f.field( EntityA_.stringA ) ) | ||
.where( f -> f.match().field( EntityA_.stringA ).matching( "a" ) ) | ||
.fetchHits( 20 ) | ||
).containsOnly( "a" ); | ||
|
||
assertThat( | ||
searchSession.search( EntityA.class ) | ||
.select( f -> f.field( EntityC_.stringA ) ) | ||
.where( f -> f.match().field( EntityC_.stringC ).matching( "c" ) ) | ||
.fetchHits( 20 ) | ||
).containsOnly( "c" ); | ||
|
||
SearchScope<EntityB> scope = searchSession.scope( List.of( EntityB.class, EntityC.class ) ); | ||
SearchPredicate searchPredicate = scope.predicate().match().field( EntityB_.stringA ).matching( "b" ).toPredicate(); | ||
SearchPredicate searchPredicate2 = | ||
scope.predicate().match().field( EntityC_.stringC ).matching( "c" ).toPredicate(); | ||
|
||
assertThat( | ||
searchSession.search( scope ) | ||
.select( f -> f.field( EntityC_.stringA ) ) | ||
.where( searchPredicate ) | ||
.fetchHits( 20 ) | ||
).containsOnly( "b" ); | ||
|
||
assertThat( | ||
searchSession.search( scope ) | ||
.select( f -> f.field( EntityC_.stringA ) ) | ||
.where( searchPredicate2 ) | ||
.fetchHits( 20 ) | ||
).containsOnly( "c" ); | ||
|
||
} ); | ||
} | ||
|
||
private void initData() { | ||
with( entityManagerFactory ).runInTransaction( entityManager -> { | ||
EntityA a = new EntityA(); | ||
a.id = 1L; | ||
a.stringA = "a"; | ||
|
||
EntityB b = new EntityB(); | ||
b.id = 10L; | ||
b.stringA = "b"; | ||
b.stringB = "b"; | ||
|
||
EntityC c = new EntityC(); | ||
c.id = 100L; | ||
c.stringA = "c"; | ||
c.stringB = "c"; | ||
c.stringC = "c"; | ||
|
||
entityManager.persist( a ); | ||
entityManager.persist( b ); | ||
entityManager.persist( c ); | ||
} ); | ||
} | ||
|
||
@Indexed | ||
@Entity | ||
public static class EntityA { | ||
@Id | ||
Long id; | ||
|
||
@FullTextField(projectable = Projectable.YES) | ||
String stringA; | ||
|
||
} | ||
|
||
@Entity | ||
public static class EntityB extends EntityA { | ||
|
||
@FullTextField(projectable = Projectable.YES) | ||
String stringB; | ||
|
||
} | ||
|
||
@Entity | ||
public static class EntityC extends EntityB { | ||
|
||
@FullTextField(projectable = Projectable.YES) | ||
String stringC; | ||
|
||
} | ||
|
||
|
||
public static class EntityA_ { | ||
public static ValueFieldReference1<EntityA, String, String, String> stringA; | ||
|
||
static { | ||
stringA = ValueFieldReference1.of( "stringA", EntityA.class, String.class, String.class, String.class ); | ||
} | ||
} | ||
|
||
public static class EntityB_ { | ||
public static ValueFieldReference1<EntityB, String, String, String> stringA; | ||
public static ValueFieldReference1<EntityB, String, String, String> stringB; | ||
|
||
static { | ||
stringA = ValueFieldReference1.of( "stringA", EntityB.class, String.class, String.class, String.class ); | ||
stringB = ValueFieldReference1.of( "stringB", EntityB.class, String.class, String.class, String.class ); | ||
} | ||
} | ||
|
||
public static class EntityC_ { | ||
public static ValueFieldReference1<EntityC, String, String, String> stringA; | ||
public static ValueFieldReference1<EntityC, String, String, String> stringB; | ||
public static ValueFieldReference1<EntityC, String, String, String> stringC; | ||
|
||
static { | ||
stringA = ValueFieldReference1.of( "stringA", EntityC.class, String.class, String.class, String.class ); | ||
stringB = ValueFieldReference1.of( "stringB", EntityC.class, String.class, String.class, String.class ); | ||
stringC = ValueFieldReference1.of( "stringC", EntityC.class, String.class, String.class, String.class ); | ||
} | ||
} | ||
|
||
|
||
public static class ValueFieldReference1<E, T, V, P> extends TypedFieldReference1<E, T, P> { | ||
|
||
public static <E, T, V, P> ValueFieldReference1<E, T, V, P> of( | ||
String path, | ||
Class<E> documentReferenceClass, | ||
Class<T> t, | ||
Class<V> v, | ||
Class<P> p) { | ||
return new ValueFieldReference1<>( path, documentReferenceClass, t, v, p ); | ||
} | ||
|
||
private final TypedFieldReference1<E, V, V> noConverter; | ||
private final TypedFieldReference1<E, String, String> string; | ||
|
||
public ValueFieldReference1(String absolutePath, Class<E> containing, Class<T> inputType, Class<V> indexType, | ||
Class<P> projectionType) { | ||
super( absolutePath, ValueConvert.YES, containing, inputType, projectionType ); | ||
this.noConverter = new TypedFieldReference1<>( absolutePath, ValueConvert.NO, containing, indexType, indexType ); | ||
this.string = | ||
new TypedFieldReference1<>( absolutePath, ValueConvert.PARSE, containing, String.class, String.class ); | ||
} | ||
|
||
public TypedFieldReference1<E, V, V> noConverter() { | ||
return noConverter; | ||
} | ||
|
||
|
||
public TypedFieldReference1<E, String, String> asString() { | ||
return string; | ||
} | ||
|
||
} | ||
|
||
public static class TypedFieldReference1<E, T, P> | ||
implements FieldProjectionFieldReference<E, P>, | ||
MatchPredicateFieldReference<E, T> { | ||
|
||
private final String absolutePath; | ||
private final ValueConvert valueConvert; | ||
private final Class<E> containing; | ||
private final Class<T> input; | ||
private final Class<P> projection; | ||
|
||
public TypedFieldReference1(String absolutePath, ValueConvert valueConvert, Class<E> containing, Class<T> input, | ||
Class<P> projection) { | ||
this.absolutePath = absolutePath; | ||
this.valueConvert = valueConvert; | ||
this.containing = containing; | ||
this.input = input; | ||
this.projection = projection; | ||
} | ||
|
||
@Override | ||
public String absolutePath() { | ||
return absolutePath; | ||
} | ||
|
||
@Override | ||
public Class<T> predicateType() { | ||
return input; | ||
} | ||
|
||
@Override | ||
public ValueConvert valueConvert() { | ||
return valueConvert; | ||
} | ||
|
||
@Override | ||
public Class<P> projectionType() { | ||
return projection; | ||
} | ||
|
||
@Override | ||
public Class<E> containing() { | ||
return containing; | ||
} | ||
} | ||
|
||
private void withinSearchSession(Consumer<SearchSession> action) { | ||
with( entityManagerFactory ).runInTransaction( entityManager -> { | ||
SearchSession searchSession = Search.session( entityManager ); | ||
action.accept( searchSession ); | ||
} ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This particular example worked nice, but ....
if we'd have a
then the best scope limit we'd have would be an
Object
:and with that, anything can be applied:
I was thinking about using something else instead of entity type, maybe the generated root type
EntityA_
but for that I haven't found a nice entry point to pass the type. Unless we also introduce:and similar things for the scope methods receiving this generated "root" (which should also work with those unions we've been talking about on the call yesterday)...
WDYT @yrodiere ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd have expected
field(...)
to accept a*Reference<? super E>
, see my other comment; but maybe I forgot what our conclusions were yesterday.In any case,
<? super E>
instead of<? extends E>
would solve your specific problem here.For targeting multiple types in the same query, we would address that as a follow-up with the "generated union types".
For targeting fields that are only present in some subtypes of the target type, we would address that as part of https://hibernate.atlassian.net/browse/HSEARCH-3434 (though it would be about much more than predicates, obviously).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking a look at this😃 !
+1 on
? super E
Yeah, that's what I've started to think about, and how we could do it, so we wouldn't need to go through all these interfaces one more time 🙈 😈
That's why I was thinking if we do this:
And then the "generated union type" is e.g.:
and for non-union:
with that, we won't even need
? super R
it'll all just be strictly limited toR
itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, but using the word
scope
is likely to cause confusion... We'll need to think about it.LGTM.
What about when an entity extends another, though? We may want to use fields from the supertype's static metamodel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4156
I've tried out this idea in this other PR ^.
yeah ... that's true 😃
so maybe instead of
EntityB_union_EntityC_.scope.scope( searchSession );
doEntityB_union_EntityC_.scope.create( searchSession );
In that other PR everything is tied to the generated class, e.g.
EntityC_
, and I was thinking that if we start with theEntityC_
there's no reason to switch to a different type as thisEntityC_
should contain all that is accessible within the scope created by that same generated classEntityC_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There definitely is a reason: reuse. Think of people having entities that extend e.g. some
AuditableEntity
with acreationDate
andlastUpdateDate
. I can definitely see them define a util to add a predicate on any subtype to restrict by last update date, and that util will not be able to take advantage of the metamodel of the specific subtype, it'll have to rely onAuditableEntity_
.... Which I guess raises the question of
@MappedSuperclass
xD Not today, though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, ok 😃 we probably can make it work for the "root" types, the inheritance part, I mean.. but not for any embeddable. (I'll give it a try in that PR)