-
Notifications
You must be signed in to change notification settings - Fork 246
HSEARCH-3319 Type-safe field references / HSEARCH-5300 Annotation processor for the Search static metamodel #4156
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
base: main
Are you sure you want to change the base?
Conversation
de353a5
to
aaf4fff
Compare
...e/src/main/java/org/hibernate/search/engine/search/predicate/dsl/SearchPredicateFactory.java
Outdated
Show resolved
Hide resolved
mapper/orm/src/main/java/org/hibernate/search/mapper/orm/scope/SearchScope.java
Outdated
Show resolved
Hide resolved
8555146
to
296c173
Compare
|
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.
Hey @yrodiere 😃 the approach we discussed last Friday seems to work well so far. (see the new inline comments or just take a look at the FieldReferenceIT
)
One thing I'm not sure how to deal with is named predicates. They are defined in binders, where there's no notion of search scope. Since binders would define the index structure, does it make sense to use the generated metamodel in them? as its generation depends on the binder ... 🤔 🙈
...tion/src/test/java/org/hibernate/search/documentation/search/predicate/FieldReferenceIT.java
Outdated
Show resolved
Hide resolved
...tion/src/test/java/org/hibernate/search/documentation/search/predicate/FieldReferenceIT.java
Show resolved
Hide resolved
...tion/src/test/java/org/hibernate/search/documentation/search/predicate/FieldReferenceIT.java
Outdated
Show resolved
Hide resolved
So, in the new commit, I've tried adding a Maven plugin that would generate things. The approach I've taken here is to have model+ search config classes in their own jar that is passed as a dependency to the Maven plugin. The plugin then starts the Search. I've used some of our test helpers to start things since, well, it's just to test how it'll work. It generates something like: package org.hibernate.search.metamodel.generator.model;
class MyEntity__ {
public String text;
} package org.hibernate.search.metamodel.generator.model;
class MyProgrammaticEntity__ {
public String number;
public String text;
} And since it happens on the generate sources phase, these are getting compiled automatically by Maven itself. As for the plugin configuration: <configuration>
<annotatedTypes>
<type>org.hibernate.search.metamodel.generator.model.MyEntity</type>
<type>org.hibernate.search.metamodel.generator.model.MyProgrammaticEntity</type>
</annotatedTypes>
<properties>
<hibernate.search.mapping.configurer>org.hibernate.search.metamodel.generator.model.MyConfigurer</hibernate.search.mapping.configurer>
</properties>
</configuration> There's a way to pass any properties that should be "forwarded" to the Search through now.... next thing I'd want to try is to run the plugin in the same module that the model is. From what I've seen so far, it is easy to get the source root, and the dependencies (things that were missing in the AP approach). |
0c8366e
to
30f9d6a
Compare
5789bc7
to
159347c
Compare
baae623
to
b8ad37a
Compare
b8ad37a
to
b5a16ea
Compare
Thanks for your pull request! This pull request appears to follow the contribution rules. › This message was automatically generated. |
b5a16ea
to
cbfcec9
Compare
|
f04af13
to
e3a9d5a
Compare
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.
Hey @yrodiere,
I've added the AP experiments to this branch as in the end we'd want to use the interfaces introduced here to generate that model.... as if the PR wasn't big enough already 😄, but the experiments are all in a single commit, and I've added comments to the places I think are of interest. There's a lot of boring code similar to the one we have in the annotation processors, as these just work with a different model.
the next steps, as I see it, are:
- keep adding the missing bits (e.g. type level binders etc.). As I encounter some new code example from the docs, or just when I remember some missing part, I add it to the test and then see what has to be added to the processor to get it working.
- write a simple class from the model.
- take care of "various backends". I think that we'd just add all backends (lucene,elasticsearch) and have a config property for the annotation processor to let the user specify the backend to use (or even both).
- deal with the ORM mapper. I don't think that it would be feasible to make ORM somehow generate the model from
javax.lang.model.element
types. Hence, my thinking here is to add a specific processor that reads the ORM's@Id
and treats them as@DocumentId
; ignore the access type for now 🙈
...org/hibernate/search/metamodel/processor/impl/IndexedEntityMetamodelAnnotationProcessor.java
Outdated
Show resolved
Hide resolved
...org/hibernate/search/metamodel/processor/impl/IndexedEntityMetamodelAnnotationProcessor.java
Outdated
Show resolved
Hide resolved
...essor/src/main/java/org/hibernate/search/metamodel/processor/impl/ProcessorElementUtils.java
Outdated
Show resolved
Hide resolved
...g/hibernate/search/metamodel/processor/mapping/ProcessorPojoModelsBootstrapIntrospector.java
Outdated
Show resolved
Hide resolved
...te/search/metamodel/processor/annotation/processing/impl/ProcessorKeywordFieldProcessor.java
Outdated
Show resolved
Hide resolved
...search/metamodel/processor/annotation/processing/impl/ProcessorPropertyBindingProcessor.java
Outdated
Show resolved
Hide resolved
...search/metamodel/processor/annotation/processing/impl/ProcessorPropertyBindingProcessor.java
Outdated
Show resolved
Hide resolved
...ate/search/metamodel/processor/annotation/processing/impl/ProcessorVectorFieldProcessor.java
Outdated
Show resolved
Hide resolved
Hey. I'm finally having a look, but first some answers
Backends should be in the user classpath already, no? If you rely on that, you can make the annotation processor work without any configuration for the most common case (only one backend type in the classpath).
Makes sense. Except...
As stated, I agree. But:
Critically, this task can even be extracted to a separate issue, and we can ask someone who knows this AccessType stuff to help with a first solution that just implements this stuff directly in Hibernate Search :)
That's a very reasonable temporary solution. |
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.
No complaints, everything seems very reasonable :)
Maybe now would be the time to start merging things? It looks like we're going to go ahead with this solution, so it's not like we're going to revert it entirely. You'll just keep adding/updating code from now on, I believe?
@@ -354,7 +373,7 @@ PredicateFinalStep withParameters( | |||
* @return A new predicate factory using the given object field as root. | |||
*/ | |||
@Incubating | |||
SearchPredicateFactory withRoot(String objectFieldPath); | |||
SearchPredicateFactory<SR> withRoot(String objectFieldPath); |
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.
Shouldn't this be...
SearchPredicateFactory<SR> withRoot(String objectFieldPath); | |
SearchPredicateFactory<?> withRoot(String objectFieldPath); |
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.
Uh, this one had been pending on your PR for quite some time. Sorry about that.
...org/hibernate/search/metamodel/processor/impl/IndexedEntityMetamodelAnnotationProcessor.java
Outdated
Show resolved
Hide resolved
...org/hibernate/search/metamodel/processor/impl/IndexedEntityMetamodelAnnotationProcessor.java
Outdated
Show resolved
Hide resolved
...essor/src/main/java/org/hibernate/search/metamodel/processor/impl/ProcessorElementUtils.java
Outdated
Show resolved
Hide resolved
...g/hibernate/search/metamodel/processor/mapping/ProcessorPojoModelsBootstrapIntrospector.java
Outdated
Show resolved
Hide resolved
...te/search/metamodel/processor/annotation/processing/impl/ProcessorKeywordFieldProcessor.java
Outdated
Show resolved
Hide resolved
...search/metamodel/processor/annotation/processing/impl/ProcessorPropertyBindingProcessor.java
Outdated
Show resolved
Hide resolved
...search/metamodel/processor/annotation/processing/impl/ProcessorPropertyBindingProcessor.java
Outdated
Show resolved
Hide resolved
...ate/search/metamodel/processor/annotation/processing/impl/ProcessorVectorFieldProcessor.java
Outdated
Show resolved
Hide resolved
0cd1cc0
to
3981dcb
Compare
6bbabbd
to
3c6ad42
Compare
9c9ccd1
to
1dd55a0
Compare
1dd55a0
to
5120cbd
Compare
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Red Hat Inc. and Hibernate Authors | ||
[[static-metamodel-overview]] | ||
= Overview of the 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.
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 couldn't review it all unfortunately, but here are a few comments on the documentation.
Great stuff :)
Hibernate Search static metamodel represents the index structure | ||
that provides a type-safe way of creating search queries through the <<search-dsl,Search DSL>>. |
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.
Maybe this introduction should be a bit more extensive?
E.g.:
Hibernate Search static metamodel represents the index structure | |
that provides a type-safe way of creating search queries through the <<search-dsl,Search DSL>>. | |
Hibernate Search's static metamodel is a set of generated classes that represents the structure of each entity's index, | |
thereby allowing to reference index fields in a type-safe way to create search queries through the <<search-dsl,Search DSL>>. | |
The basic principles are very similar to the link:{hibernateDocUrl}#tooling-modelgen[JPA static metamodel available in Hibernate ORM], but in the case of Search the metamodel is about indexes rather than entities. |
e.g. `MyOuterClasspass:[_]MySearchEntitypass:[__]`. | ||
These classes are created in the same package where the search entity they represent is located. | ||
|
||
The root metamodel class that describes the indexed entity has a static field `INDEX` that users can use to interact with the 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.
The root metamodel class that describes the indexed entity has a static field `INDEX` that users can use to interact with the metamodel. | |
The root metamodel class that describes the indexed entity has a static field `INDEX` that you can use to interact with the metamodel. |
|
||
A field reference type describes the set of search capabilities a particular index field has, in particular, | ||
what kind of projections/aggregations/predicates/sorts are allowed, if any. It does so by implementing a | ||
subset of search traits interfaces defined in `org.hibernate.search.engine.search.reference.pass:[*]`, which in turn allows |
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.
subset of search traits interfaces defined in `org.hibernate.search.engine.search.reference.pass:[*]`, which in turn allows | |
subset of search "trait" interfaces defined in `org.hibernate.search.engine.search.reference.pass:[*]`, which in turn allows |
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.
Maybe also link to whatever section of the documentation talks about "traits"? It's quite a specific term.
---- | ||
include::{sourcedir}/org/hibernate/search/documentation/search/metamodel/MetamodelIT.java[tags=compileCheck-pass] | ||
---- | ||
<1> If the title field is projectable then this compiles works fine, |
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.
<1> If the title field is projectable then this compiles works fine, | |
<1> If the title field is projectable then this compiles fine, |
---- | ||
include::{sourcedir}/org/hibernate/search/documentation/search/metamodel/MetamodelIT.java[tags=valueModel] | ||
---- | ||
<1> Calling `string()` on a field reference is an eqivalent to `.field( "genre" ).matching( <search value>, ValueModel.STRING )` |
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.
<1> Calling `string()` on a field reference is an eqivalent to `.field( "genre" ).matching( <search value>, ValueModel.STRING )` | |
<1> Calling `string()` on a field reference is an equivalent to `.field( "genre" ).matching( <search value>, ValueModel.STRING )` |
include::../components/_incubating-warning.adoc[] | ||
|
||
The static metamodel class describes the index structure. Each indexed entity (index) is represented by a single class | ||
that may contain inner classes describing the embeddables (i.e. nested/flattened objects). These classes contain <<static-metamodel-processor-field-reference-types,field references>> |
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.
that may contain inner classes describing the embeddables (i.e. nested/flattened objects). These classes contain <<static-metamodel-processor-field-reference-types,field references>> | |
that may contain inner classes describing object fields (e.g. from <<TODO LINK,indexed-embedded properties>>). These classes contain <<static-metamodel-processor-field-reference-types,field references>> |
|
||
/** | ||
* Note: this class was created by an annotation processor and copied to the sources, | ||
* so that we do not run the AP on an entire documentation module. |
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.
Can't you run the annotation processor on a subset of the sources?
I'm pretty sure you can add a separate execution of the maven-compiler-plugin that only performs annotation processing, in which case you could possibly filter the sources to include only this package.
List<String> titles = searchSession.search( scope ) | ||
.select( f -> f.field( Book__.INDEX.title ) ) // <1> | ||
.where( f -> f.matchAll() ) | ||
.fetchHits( 20 ); |
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.
Awesome ❤️
/** | ||
* Target the given field in the range aggregation. | ||
* | ||
* @param fieldReference The field reference representing a <a href="SearchAggregationFactory.html#field-paths">path</a> to the index field to aggregate. |
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'm afraid linking to #field-paths
won't be very helpful here.
Wouldn't it be better to add a section there about "static metamodel field references", with some generic info about what the static metamodel is and a recommendation to check it out in the reference documentation?
SearchScope<?, Book> scope = searchSession.scope( Book.class ); // <1> | ||
SearchScope<Book, Book> scope = searchSession.scope( Book.class ); // <2> | ||
SearchScope<ReadingMaterial, ReadingMaterial> scope = searchSession.scope( List.of( Book.class, Magazine.class ) ); // <3> | ||
SearchScope<Book__, Book> scope = searchSession.scope( Book.class ); // <4> | ||
SearchScope<SomeRandomClass, Book> scope = searchSession.scope( Book.class ); // <5> | ||
---- | ||
<1> Use the `?` wildcard. | ||
<2> Use the same type as your search entity. | ||
<3> Use the common supertype for a scope of multiple search entities. | ||
<4> Use the class generated for the static metamodel of this search entity. | ||
<5> Use any class. |
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.
Or... Better option, use var
? That's what people should do when using DSL classes anyway.
|
https://hibernate.atlassian.net/browse/HSEARCH-3319
https://hibernate.atlassian.net/browse/HSEARCH-5300
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.