Skip to content

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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

marko-bekhta
Copy link
Member

@marko-bekhta marko-bekhta commented May 9, 2024

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.


@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch from de353a5 to aaf4fff Compare May 15, 2024 09:35
@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch 3 times, most recently from 8555146 to 296c173 Compare May 20, 2024 14:48
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
78.2% Coverage on New Code (required ≥ 80%)

See analysis details on SonarCloud

Copy link
Member Author

@marko-bekhta marko-bekhta left a 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 ... 🤔 🙈

@marko-bekhta
Copy link
Member Author

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 properties. In this case, I've tried passing a configurer to apply some programmatic mapping 🙈.

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).

@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch from 0c8366e to 30f9d6a Compare May 28, 2024 16:15
@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch 2 times, most recently from 5789bc7 to 159347c Compare August 21, 2024 13:19
@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch 2 times, most recently from baae623 to b8ad37a Compare December 5, 2024 17:42
@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch from b8ad37a to b5a16ea Compare December 12, 2024 21:26
@hibernate-github-bot
Copy link

hibernate-github-bot bot commented Dec 12, 2024

Thanks for your pull request!

This pull request appears to follow the contribution rules.

› This message was automatically generated.

@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch from b5a16ea to cbfcec9 Compare December 13, 2024 12:38
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
72.8% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch 2 times, most recently from f04af13 to e3a9d5a Compare February 20, 2025 10:54
Copy link
Member Author

@marko-bekhta marko-bekhta left a 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 🙈

@yrodiere
Copy link
Member

Hey. I'm finally having a look, but first some answers

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).

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).

the next steps, as I see it, are: [...]

Makes sense.

Except...

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

As stated, I agree. But:

  1. We don't need the whole ORM metamodel. Most of the things we do around the Hibernate ORM metamodel in Search are related to dependency resolution, which is pointless when generating Search's static metamodel. We would only need to infer access type, somehow, and that could reasonably be done by getting rid of our current way of doing it and directly applying the JPA spec ourselves instead.
  2. We don't need ORM to do it, we can do it ourselves. It's annoying, but possible.
  3. Perhaps Hibernate Models could help someday.

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 :)

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 🙈

That's a very reasonable temporary solution.

Copy link
Member

@yrodiere yrodiere left a 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be...

Suggested change
SearchPredicateFactory<SR> withRoot(String objectFieldPath);
SearchPredicateFactory<?> withRoot(String objectFieldPath);

Copy link
Member

@yrodiere yrodiere Feb 27, 2025

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.

@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch 2 times, most recently from 0cd1cc0 to 3981dcb Compare March 20, 2025 17:26
@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch 3 times, most recently from 6bbabbd to 3c6ad42 Compare April 17, 2025 09:33
@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch 2 times, most recently from 9c9ccd1 to 1dd55a0 Compare April 25, 2025 08:21
@marko-bekhta marko-bekhta force-pushed the feat/HSEARCH-3319-static-metamodel-field-references-v2 branch from 1dd55a0 to 5120cbd Compare April 29, 2025 07:08
// SPDX-License-Identifier: Apache-2.0
// Copyright Red Hat Inc. and Hibernate Authors
[[static-metamodel-overview]]
= Overview of the static metamodel
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yrodiere this commit (24465fa) adds the docs 😃. only pinging you here so it'll be easier to find this change 😃 🙈

Copy link
Member

@yrodiere yrodiere left a 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 :)

Comment on lines +8 to +9
Hibernate Search static metamodel represents the index structure
that provides a type-safe way of creating search queries through the <<search-dsl,Search DSL>>.
Copy link
Member

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.:

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Member

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<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 )`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<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>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

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.

Comment on lines +78 to +81
List<String> titles = searchSession.search( scope )
.select( f -> f.field( Book__.INDEX.title ) ) // <1>
.where( f -> f.matchAll() )
.fetchHits( 20 );
Copy link
Member

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.
Copy link
Member

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?

Comment on lines +138 to +148
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.
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants