|
| 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.documentation.search.predicate; |
| 8 | + |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | +import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import java.util.function.Consumer; |
| 14 | + |
| 15 | +import jakarta.persistence.Entity; |
| 16 | +import jakarta.persistence.EntityManagerFactory; |
| 17 | +import jakarta.persistence.Id; |
| 18 | + |
| 19 | +import org.hibernate.search.documentation.testsupport.BackendConfigurations; |
| 20 | +import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper; |
| 21 | +import org.hibernate.search.engine.backend.types.Projectable; |
| 22 | +import org.hibernate.search.engine.search.common.ValueConvert; |
| 23 | +import org.hibernate.search.engine.search.predicate.SearchPredicate; |
| 24 | +import org.hibernate.search.engine.search.reference.traits.predicate.MatchPredicateFieldReference; |
| 25 | +import org.hibernate.search.engine.search.reference.traits.projection.FieldProjectionFieldReference; |
| 26 | +import org.hibernate.search.mapper.orm.Search; |
| 27 | +import org.hibernate.search.mapper.orm.scope.SearchScope; |
| 28 | +import org.hibernate.search.mapper.orm.session.SearchSession; |
| 29 | +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField; |
| 30 | +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; |
| 31 | + |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 35 | + |
| 36 | +class FieldReferenceIT { |
| 37 | + |
| 38 | + @RegisterExtension |
| 39 | + public DocumentationSetupHelper setupHelper = DocumentationSetupHelper.withSingleBackend( BackendConfigurations.simple() ); |
| 40 | + |
| 41 | + private EntityManagerFactory entityManagerFactory; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + void setup() { |
| 45 | + entityManagerFactory = setupHelper.start().setup( EntityA.class, EntityB.class, EntityC.class ); |
| 46 | + initData(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void smoke() { |
| 51 | + withinSearchSession( searchSession -> { |
| 52 | + assertThat( |
| 53 | + searchSession.search( EntityA.class ) |
| 54 | + .select( f -> f.field( EntityA_.stringA ) ) |
| 55 | + .where( f -> f.match().field( EntityA_.stringA ).matching( "a" ) ) |
| 56 | + .fetchHits( 20 ) |
| 57 | + ).containsOnly( "a" ); |
| 58 | + |
| 59 | + assertThat( |
| 60 | + searchSession.search( EntityA.class ) |
| 61 | + .select( f -> f.field( EntityC_.stringA ) ) |
| 62 | + .where( f -> f.match().field( EntityC_.stringC ).matching( "c" ) ) |
| 63 | + .fetchHits( 20 ) |
| 64 | + ).containsOnly( "c" ); |
| 65 | + |
| 66 | + SearchScope<EntityB> scope = searchSession.scope( List.of( EntityB.class, EntityC.class ) ); |
| 67 | + SearchPredicate searchPredicate = scope.predicate().match().field( EntityB_.stringA ).matching( "b" ).toPredicate(); |
| 68 | + SearchPredicate searchPredicate2 = |
| 69 | + scope.predicate().match().field( EntityC_.stringC ).matching( "c" ).toPredicate(); |
| 70 | + |
| 71 | + assertThat( |
| 72 | + searchSession.search( scope ) |
| 73 | + .select( f -> f.field( EntityC_.stringA ) ) |
| 74 | + .where( searchPredicate ) |
| 75 | + .fetchHits( 20 ) |
| 76 | + ).containsOnly( "b" ); |
| 77 | + |
| 78 | + assertThat( |
| 79 | + searchSession.search( scope ) |
| 80 | + .select( f -> f.field( EntityC_.stringA ) ) |
| 81 | + .where( searchPredicate2 ) |
| 82 | + .fetchHits( 20 ) |
| 83 | + ).containsOnly( "c" ); |
| 84 | + |
| 85 | + } ); |
| 86 | + } |
| 87 | + |
| 88 | + private void initData() { |
| 89 | + with( entityManagerFactory ).runInTransaction( entityManager -> { |
| 90 | + EntityA a = new EntityA(); |
| 91 | + a.id = 1L; |
| 92 | + a.stringA = "a"; |
| 93 | + |
| 94 | + EntityB b = new EntityB(); |
| 95 | + b.id = 10L; |
| 96 | + b.stringA = "b"; |
| 97 | + b.stringB = "b"; |
| 98 | + |
| 99 | + EntityC c = new EntityC(); |
| 100 | + c.id = 100L; |
| 101 | + c.stringA = "c"; |
| 102 | + c.stringB = "c"; |
| 103 | + c.stringC = "c"; |
| 104 | + |
| 105 | + entityManager.persist( a ); |
| 106 | + entityManager.persist( b ); |
| 107 | + entityManager.persist( c ); |
| 108 | + } ); |
| 109 | + } |
| 110 | + |
| 111 | + @Indexed |
| 112 | + @Entity |
| 113 | + public static class EntityA { |
| 114 | + @Id |
| 115 | + Long id; |
| 116 | + |
| 117 | + @FullTextField(projectable = Projectable.YES) |
| 118 | + String stringA; |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + @Entity |
| 123 | + public static class EntityB extends EntityA { |
| 124 | + |
| 125 | + @FullTextField(projectable = Projectable.YES) |
| 126 | + String stringB; |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + @Entity |
| 131 | + public static class EntityC extends EntityB { |
| 132 | + |
| 133 | + @FullTextField(projectable = Projectable.YES) |
| 134 | + String stringC; |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + public static class EntityA_ { |
| 140 | + public static ValueFieldReference1<EntityA, String, String, String> stringA; |
| 141 | + |
| 142 | + static { |
| 143 | + stringA = ValueFieldReference1.of( "stringA", EntityA.class, String.class, String.class, String.class ); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public static class EntityB_ { |
| 148 | + public static ValueFieldReference1<EntityB, String, String, String> stringA; |
| 149 | + public static ValueFieldReference1<EntityB, String, String, String> stringB; |
| 150 | + |
| 151 | + static { |
| 152 | + stringA = ValueFieldReference1.of( "stringA", EntityB.class, String.class, String.class, String.class ); |
| 153 | + stringB = ValueFieldReference1.of( "stringB", EntityB.class, String.class, String.class, String.class ); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public static class EntityC_ { |
| 158 | + public static ValueFieldReference1<EntityC, String, String, String> stringA; |
| 159 | + public static ValueFieldReference1<EntityC, String, String, String> stringB; |
| 160 | + public static ValueFieldReference1<EntityC, String, String, String> stringC; |
| 161 | + |
| 162 | + static { |
| 163 | + stringA = ValueFieldReference1.of( "stringA", EntityC.class, String.class, String.class, String.class ); |
| 164 | + stringB = ValueFieldReference1.of( "stringB", EntityC.class, String.class, String.class, String.class ); |
| 165 | + stringC = ValueFieldReference1.of( "stringC", EntityC.class, String.class, String.class, String.class ); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + public static class ValueFieldReference1<E, T, V, P> extends TypedFieldReference1<E, T, P> { |
| 171 | + |
| 172 | + public static <E, T, V, P> ValueFieldReference1<E, T, V, P> of( |
| 173 | + String path, |
| 174 | + Class<E> documentReferenceClass, |
| 175 | + Class<T> t, |
| 176 | + Class<V> v, |
| 177 | + Class<P> p) { |
| 178 | + return new ValueFieldReference1<>( path, documentReferenceClass, t, v, p ); |
| 179 | + } |
| 180 | + |
| 181 | + private final TypedFieldReference1<E, V, V> noConverter; |
| 182 | + private final TypedFieldReference1<E, String, String> string; |
| 183 | + |
| 184 | + public ValueFieldReference1(String absolutePath, Class<E> containing, Class<T> inputType, Class<V> indexType, |
| 185 | + Class<P> projectionType) { |
| 186 | + super( absolutePath, ValueConvert.YES, containing, inputType, projectionType ); |
| 187 | + this.noConverter = new TypedFieldReference1<>( absolutePath, ValueConvert.NO, containing, indexType, indexType ); |
| 188 | + this.string = |
| 189 | + new TypedFieldReference1<>( absolutePath, ValueConvert.PARSE, containing, String.class, String.class ); |
| 190 | + } |
| 191 | + |
| 192 | + public TypedFieldReference1<E, V, V> noConverter() { |
| 193 | + return noConverter; |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + public TypedFieldReference1<E, String, String> asString() { |
| 198 | + return string; |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + public static class TypedFieldReference1<E, T, P> |
| 204 | + implements FieldProjectionFieldReference<E, P>, |
| 205 | + MatchPredicateFieldReference<E, T> { |
| 206 | + |
| 207 | + private final String absolutePath; |
| 208 | + private final ValueConvert valueConvert; |
| 209 | + private final Class<E> containing; |
| 210 | + private final Class<T> input; |
| 211 | + private final Class<P> projection; |
| 212 | + |
| 213 | + public TypedFieldReference1(String absolutePath, ValueConvert valueConvert, Class<E> containing, Class<T> input, |
| 214 | + Class<P> projection) { |
| 215 | + this.absolutePath = absolutePath; |
| 216 | + this.valueConvert = valueConvert; |
| 217 | + this.containing = containing; |
| 218 | + this.input = input; |
| 219 | + this.projection = projection; |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public String absolutePath() { |
| 224 | + return absolutePath; |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public Class<T> predicateType() { |
| 229 | + return input; |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public ValueConvert valueConvert() { |
| 234 | + return valueConvert; |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public Class<P> projectionType() { |
| 239 | + return projection; |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public Class<E> containing() { |
| 244 | + return containing; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + private void withinSearchSession(Consumer<SearchSession> action) { |
| 249 | + with( entityManagerFactory ).runInTransaction( entityManager -> { |
| 250 | + SearchSession searchSession = Search.session( entityManager ); |
| 251 | + action.accept( searchSession ); |
| 252 | + } ); |
| 253 | + } |
| 254 | +} |
0 commit comments