Skip to content

Commit d763543

Browse files
committed
HSEARCH-3319 WIP: DRAFT: IDEA: TEST: Type-safe field references
1 parent f3bb0ec commit d763543

35 files changed

+1505
-90
lines changed

documentation/src/test/java/org/hibernate/search/documentation/search/converter/ProjectionConverterIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434

3535
class ProjectionConverterIT {
3636
@RegisterExtension
37-
public DocumentationSetupHelper setupHelper = DocumentationSetupHelper.withSingleBackend( BackendConfigurations.simple() );
37+
public DocumentationSetupHelper setupHelper = DocumentationSetupHelper.withSingleBackend(
38+
BackendConfigurations.simple() );
3839

3940
private EntityManagerFactory entityManagerFactory;
4041

engine/src/main/java/org/hibernate/search/engine/search/predicate/dsl/CommonQueryStringPredicateFieldMoreStep.java

+45
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
*/
77
package org.hibernate.search.engine.search.predicate.dsl;
88

9+
import org.hibernate.search.engine.search.reference.TypedFieldReference;
10+
import org.hibernate.search.util.common.annotation.Incubating;
11+
912
/**
1013
* The step in a query string predicate definition, where the query string to match can be set
1114
* (see the superinterface {@link CommonQueryStringPredicateMatchingStep}),
@@ -54,4 +57,46 @@ default S field(String fieldPath) {
5457
*/
5558
S fields(String... fieldPaths);
5659

60+
/**
61+
* Target the given field in the query string predicate,
62+
* as an alternative to the already-targeted fields.
63+
* <p>
64+
* Only text fields are supported.
65+
* <p>
66+
* See {@link CommonQueryStringPredicateFieldStep#field(String)} for more information on targeted fields.
67+
*
68+
* @param field The field reference representing a <a href="SearchPredicateFactory.html#field-paths">path</a> to the index field
69+
* to apply the predicate on.
70+
* @return The next step.
71+
*
72+
* @see CommonQueryStringPredicateFieldStep#field(String)
73+
*/
74+
@Incubating
75+
default S field(TypedFieldReference<?> field) {
76+
return fields( field );
77+
}
78+
79+
/**
80+
* Target the given fields in the query string predicate,
81+
* as an alternative to the already-targeted fields.
82+
* <p>
83+
* Only text fields are supported.
84+
* <p>
85+
* See {@link CommonQueryStringPredicateFieldStep#fields(String...)} for more information on targeted fields.
86+
*
87+
* @param fields The field reference representing <a href="SearchPredicateFactory.html#field-paths">paths</a> to the index fields
88+
* to apply the predicate on.
89+
* @return The next step.
90+
*
91+
* @see CommonQueryStringPredicateFieldStep#fields(String...)
92+
*/
93+
@Incubating
94+
default S fields(TypedFieldReference<?>... fields) {
95+
String[] paths = new String[fields.length];
96+
for ( int i = 0; i < fields.length; i++ ) {
97+
paths[i] = fields[i].absolutePath();
98+
}
99+
return fields( paths );
100+
}
101+
57102
}

engine/src/main/java/org/hibernate/search/engine/search/predicate/dsl/CommonQueryStringPredicateFieldStep.java

+49
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
*/
77
package org.hibernate.search.engine.search.predicate.dsl;
88

9+
import org.hibernate.search.engine.search.reference.TypedFieldReference;
10+
import org.hibernate.search.util.common.annotation.Incubating;
11+
912
/**
1013
* The initial step in a query string predicate definition, where the target field can be set.
1114
*
@@ -51,4 +54,50 @@ default N field(String fieldPath) {
5154
*/
5255
N fields(String... fieldPaths);
5356

57+
58+
/**
59+
* Target the given field in the query string predicate.
60+
* <p>
61+
* Only text fields are supported.
62+
* <p>
63+
* Multiple fields may be targeted by the same predicate:
64+
* the predicate will match if <em>any</em> targeted field matches.
65+
* <p>
66+
* When targeting multiple fields, those fields must have compatible types.
67+
* Please refer to the reference documentation for more information.
68+
*
69+
* @param field The field reference representing a <a href="SearchPredicateFactory.html#field-paths">path</a> to the index field
70+
* to apply the predicate on.
71+
* @return The next step.
72+
*/
73+
@Incubating
74+
default N field(TypedFieldReference<?> field) {
75+
return fields( field );
76+
}
77+
78+
/**
79+
* Target the given fields in the query string predicate.
80+
* <p>
81+
* Only text fields are supported.
82+
* <p>
83+
* Equivalent to {@link #field(String)} followed by multiple calls to
84+
* {@link #field(String)},
85+
* the only difference being that calls to {@link CommonQueryStringPredicateFieldMoreStep#boost(float)}
86+
* and other field-specific settings on the returned step will only need to be done once
87+
* and will apply to all the fields passed to this method.
88+
*
89+
* @param fields The field reference representing <a href="SearchPredicateFactory.html#field-paths">paths</a> to the index fields
90+
* to apply the predicate on.
91+
* @return The next step.
92+
*
93+
* @see #field(String)
94+
*/
95+
@Incubating
96+
default N fields(TypedFieldReference<?>... fields) {
97+
String[] paths = new String[fields.length];
98+
for ( int i = 0; i < fields.length; i++ ) {
99+
paths[i] = fields[i].absolutePath();
100+
}
101+
return fields( paths );
102+
}
54103
}

engine/src/main/java/org/hibernate/search/engine/search/predicate/dsl/ExistsPredicateFieldStep.java

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.hibernate.search.engine.search.predicate.dsl;
88

9+
import org.hibernate.search.engine.search.reference.TypedFieldReference;
910

1011
/**
1112
* The initial step in an "exists" predicate definition, where the target field can be set.
@@ -23,4 +24,15 @@ public interface ExistsPredicateFieldStep<N extends ExistsPredicateOptionsStep<?
2324
*/
2425
N field(String fieldPath);
2526

27+
/**
28+
* Target the given field in the "exists" predicate.
29+
*
30+
* @param field The field reference representing a <a href="SearchPredicateFactory.html#field-paths">path</a> to the index field
31+
* to apply the predicate on.
32+
* @return The next step.
33+
*/
34+
default N field(TypedFieldReference<?> field) {
35+
return field( field.absolutePath() );
36+
}
37+
2638
}

engine/src/main/java/org/hibernate/search/engine/search/predicate/dsl/KnnPredicateFieldStep.java

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.hibernate.search.engine.search.predicate.dsl;
88

9+
import org.hibernate.search.engine.search.reference.TypedFieldReference;
10+
911
/**
1012
* The initial step in a "knn" predicate definition, where the target field can be set.
1113
*/
@@ -18,4 +20,6 @@ public interface KnnPredicateFieldStep {
1820
* @return The next step in the knn predicate DSL.
1921
*/
2022
KnnPredicateVectorStep field(String fieldPath);
23+
24+
<T> KnnPredicateVectorGenericStep<T> field(TypedFieldReference<T> field);
2125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.engine.search.predicate.dsl;
8+
9+
/**
10+
* The step in a "knn" predicate definition where the vector to match is defined.
11+
*/
12+
public interface KnnPredicateVectorGenericStep<T> {
13+
/**
14+
* @param vector The vector from which to compute the distance to vectors in the indexed field.
15+
* @return The next step in the knn predicate DSL.
16+
*/
17+
KnnPredicateOptionsStep matching(T vector);
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.engine.search.predicate.dsl;
8+
9+
/**
10+
* The step in a "match" predicate definition where the value to match can be set
11+
* (see the superinterface {@link MatchPredicateMatchingStep}),
12+
* or optional parameters for the last targeted field(s) can be set,
13+
* or more target fields can be added.
14+
*
15+
* @param <S> The "self" type (the actual exposed type of this step).
16+
* @param <N> The type of the next step.
17+
* @param <T> The type of the match value.
18+
* @param <V> The type representing the fields.
19+
*/
20+
public interface MatchPredicateFieldMoreGenericStep<
21+
S extends MatchPredicateFieldMoreGenericStep<?, N, T, V>,
22+
N extends MatchPredicateOptionsStep<?>,
23+
T,
24+
V>
25+
extends MatchPredicateMatchingGenericStep<N, T>, MultiFieldPredicateFieldBoostStep<S> {
26+
27+
/**
28+
* Target the given field in the match predicate,
29+
* as an alternative to the already-targeted fields.
30+
* <p>
31+
* See {@link MatchPredicateFieldStep#field(String)} for more information about targeting fields.
32+
*
33+
* @param field The field with a <a href="SearchPredicateFactory.html#field-paths">path</a> to the index field
34+
* to apply the predicate on.
35+
* @return The next step.
36+
*
37+
* @see MatchPredicateFieldStep#field(String)
38+
*/
39+
S field(V field);
40+
41+
/**
42+
* Target the given fields in the match predicate,
43+
* as an alternative to the already-targeted fields.
44+
* <p>
45+
* See {@link MatchPredicateFieldStep#fields(String...)} for more information about targeting fields.
46+
*
47+
* @param fieldPaths The fields with <a href="SearchPredicateFactory.html#field-paths">paths</a> to the index fields
48+
* to apply the predicate on.
49+
* @return The next step.
50+
*
51+
* @see MatchPredicateFieldStep#fields(String...)
52+
*/
53+
S fields(V... fieldPaths);
54+
55+
}

engine/src/main/java/org/hibernate/search/engine/search/predicate/dsl/MatchPredicateFieldMoreStep.java

+1-31
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,6 @@
1818
public interface MatchPredicateFieldMoreStep<
1919
S extends MatchPredicateFieldMoreStep<?, N>,
2020
N extends MatchPredicateOptionsStep<?>>
21-
extends MatchPredicateMatchingStep<N>, MultiFieldPredicateFieldBoostStep<S> {
22-
23-
/**
24-
* Target the given field in the match predicate,
25-
* as an alternative to the already-targeted fields.
26-
* <p>
27-
* See {@link MatchPredicateFieldStep#field(String)} for more information about targeting fields.
28-
*
29-
* @param fieldPath The <a href="SearchPredicateFactory.html#field-paths">path</a> to the index field
30-
* to apply the predicate on.
31-
* @return The next step.
32-
*
33-
* @see MatchPredicateFieldStep#field(String)
34-
*/
35-
default S field(String fieldPath) {
36-
return fields( fieldPath );
37-
}
38-
39-
/**
40-
* Target the given fields in the match predicate,
41-
* as an alternative to the already-targeted fields.
42-
* <p>
43-
* See {@link MatchPredicateFieldStep#fields(String...)} for more information about targeting fields.
44-
*
45-
* @param fieldPaths The <a href="SearchPredicateFactory.html#field-paths">paths</a> to the index fields
46-
* to apply the predicate on.
47-
* @return The next step.
48-
*
49-
* @see MatchPredicateFieldStep#fields(String...)
50-
*/
51-
S fields(String... fieldPaths);
21+
extends MatchPredicateMatchingStep<N>, MatchPredicateFieldMoreGenericStep<S, N, Object, String> {
5222

5323
}

engine/src/main/java/org/hibernate/search/engine/search/predicate/dsl/MatchPredicateFieldStep.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.hibernate.search.engine.search.predicate.dsl;
88

9+
import org.hibernate.search.engine.search.reference.TypedFieldReference;
910

1011
/**
1112
* The initial step in a "match" predicate definition, where the target field can be set.
@@ -33,7 +34,7 @@ default N field(String fieldPath) {
3334
* Target the given fields in the match predicate.
3435
* <p>
3536
* Equivalent to {@link #field(String)} followed by multiple calls to
36-
* {@link MatchPredicateFieldMoreStep#field(String)},
37+
* {@link MatchPredicateFieldMoreStep#field(Object)},
3738
* the only difference being that calls to {@link MatchPredicateFieldMoreStep#boost(float)}
3839
* and other field-specific settings on the returned step will only need to be done once
3940
* and will apply to all the fields passed to this method.
@@ -45,4 +46,16 @@ default N field(String fieldPath) {
4546
* @see #field(String)
4647
*/
4748
N fields(String... fieldPaths);
49+
50+
/**
51+
* TODO
52+
*/
53+
default <T> MatchPredicateFieldMoreGenericStep<?, ?, T, TypedFieldReference<T>> field(TypedFieldReference<T> field) {
54+
return fields( field );
55+
}
56+
57+
/**
58+
* TODO
59+
*/
60+
<T> MatchPredicateFieldMoreGenericStep<?, ?, T, TypedFieldReference<T>> fields(TypedFieldReference<T>... fields);
4861
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.engine.search.predicate.dsl;
8+
9+
/**
10+
* The step in a "match" predicate definition where the value to match can be set.
11+
*
12+
* @param <N> The type of the next step.
13+
* @param <T> The type of the match value.
14+
*/
15+
public interface MatchPredicateMatchingGenericStep<N extends MatchPredicateOptionsStep<?>, T> {
16+
17+
/**
18+
* Require at least one of the targeted fields to match the given value.
19+
*
20+
* @param value The value to match.
21+
* The signature of this method defines this parameter as an {@code T},
22+
* but a specific type is expected depending on the targeted field.
23+
* @return The next step.
24+
*/
25+
N matching(T value);
26+
}

engine/src/main/java/org/hibernate/search/engine/search/predicate/dsl/MatchPredicateMatchingStep.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
*
1414
* @param <N> The type of the next step.
1515
*/
16-
public interface MatchPredicateMatchingStep<N extends MatchPredicateOptionsStep<?>> {
16+
public interface MatchPredicateMatchingStep<N extends MatchPredicateOptionsStep<?>>
17+
extends MatchPredicateMatchingGenericStep<N, Object> {
1718

1819
/**
1920
* Require at least one of the targeted fields to match the given value.
@@ -22,7 +23,7 @@ public interface MatchPredicateMatchingStep<N extends MatchPredicateOptionsStep<
2223
* See {@link ValueConvert#YES}.
2324
*
2425
* @param value The value to match.
25-
* The signature of this method defines this parameter as an {@link Object},
26+
* The signature of this method defines this parameter as an {@code T},
2627
* but a specific type is expected depending on the targeted field.
2728
* See {@link ValueConvert#YES} for more information.
2829
* @return The next step.
@@ -47,5 +48,4 @@ default N matching(Object value) {
4748
* @see ValueConvert
4849
*/
4950
N matching(Object value, ValueConvert convert);
50-
5151
}

engine/src/main/java/org/hibernate/search/engine/search/predicate/dsl/NestedPredicateFieldStep.java

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hibernate.search.engine.search.predicate.dsl;
88

99
import org.hibernate.search.engine.backend.types.ObjectStructure;
10+
import org.hibernate.search.engine.search.reference.NestedObjectFieldReference;
1011

1112
/**
1213
* The initial step in a "nested" predicate definition, where the target field can be set.
@@ -27,4 +28,17 @@ public interface NestedPredicateFieldStep<N extends NestedPredicateNestStep<?>>
2728
*/
2829
N objectField(String fieldPath);
2930

31+
/**
32+
* Set the object field to "nest" on.
33+
* <p>
34+
* The selected field must have a {@link ObjectStructure#NESTED nested structure} in the targeted indexes.
35+
*
36+
* @param field The field reference representing a <a href="SearchPredicateFactory.html#field-paths">path</a> to the object field
37+
* to apply the predicate on.
38+
* @return The next step.
39+
*/
40+
default N objectField(NestedObjectFieldReference field) {
41+
return objectField( field.absolutePath() );
42+
}
43+
3044
}

0 commit comments

Comments
 (0)