|
18 | 18 |
|
19 | 19 | import jakarta.persistence.EntityManagerFactory;
|
20 | 20 |
|
| 21 | +import org.hibernate.search.backend.elasticsearch.ElasticsearchExtension; |
21 | 22 | import org.hibernate.search.documentation.testsupport.BackendConfigurations;
|
22 | 23 | import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper;
|
23 | 24 | import org.hibernate.search.engine.search.common.BooleanOperator;
|
|
32 | 33 | import org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer;
|
33 | 34 | import org.hibernate.search.mapper.orm.scope.SearchScope;
|
34 | 35 | import org.hibernate.search.mapper.orm.session.SearchSession;
|
| 36 | +import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep; |
35 | 37 | import org.hibernate.search.util.common.data.RangeBoundInclusion;
|
36 | 38 | import org.hibernate.search.util.impl.integrationtest.common.extension.BackendConfiguration;
|
37 | 39 |
|
@@ -64,10 +66,14 @@ void setup() {
|
64 | 66 | if ( BackendConfiguration.isLucene() ) {
|
65 | 67 | setupContext.withProperty(
|
66 | 68 | HibernateOrmMapperSettings.MAPPING_CONFIGURER,
|
67 |
| - (HibernateOrmSearchMappingConfigurer) context -> context.programmaticMapping() |
68 |
| - .type( Book.class ) |
69 |
| - .property( "coverImageEmbeddings" ) |
70 |
| - .vectorField( 128 ) |
| 69 | + (HibernateOrmSearchMappingConfigurer) context -> { |
| 70 | + TypeMappingStep book = context.programmaticMapping() |
| 71 | + .type( Book.class ); |
| 72 | + book.property( "coverImageEmbeddings" ) |
| 73 | + .vectorField( 128 ); |
| 74 | + book.property( "alternativeCoverImageEmbeddings" ) |
| 75 | + .vectorField( 128 ); |
| 76 | + } |
71 | 77 | );
|
72 | 78 | }
|
73 | 79 | entityManagerFactory = setupContext.setup( Book.class, Author.class, EmbeddableGeoPoint.class );
|
@@ -1143,6 +1149,49 @@ void knn() {
|
1143 | 1149 | .extracting( Book::getId )
|
1144 | 1150 | .containsExactlyInAnyOrder( BOOK1_ID, BOOK2_ID, BOOK3_ID );
|
1145 | 1151 | } );
|
| 1152 | + |
| 1153 | + withinSearchSession( searchSession -> { |
| 1154 | + // tag::knn-should[] |
| 1155 | + float[] coverImageEmbeddingsVector = /*...*/ |
| 1156 | + // end::knn-should[] |
| 1157 | + new float[128]; |
| 1158 | + // tag::knn-should[] |
| 1159 | + float[] alternativeCoverImageEmbeddingsVector = /*...*/ |
| 1160 | + // end::knn-should[] |
| 1161 | + new float[128]; |
| 1162 | + // tag::knn-should[] |
| 1163 | + List<Book> hits = searchSession.search( Book.class ) |
| 1164 | + .where( f -> f.bool() |
| 1165 | + .should( f.knn( 10 ).field( "coverImageEmbeddings" ).matching( coverImageEmbeddingsVector ) ) |
| 1166 | + .should( f.knn( 5 ).field( "alternativeCoverImageEmbeddings" ) |
| 1167 | + .matching( alternativeCoverImageEmbeddingsVector ) ) |
| 1168 | + ) |
| 1169 | + .fetchHits( 20 ); |
| 1170 | + // end::knn-should[] |
| 1171 | + assertThat( hits ) |
| 1172 | + .extracting( Book::getId ) |
| 1173 | + .containsExactlyInAnyOrder( BOOK1_ID, BOOK2_ID, BOOK3_ID, BOOK4_ID ); |
| 1174 | + } ); |
| 1175 | + |
| 1176 | + |
| 1177 | + if ( BackendConfiguration.isElasticsearch() ) { |
| 1178 | + withinSearchSession( searchSession -> { |
| 1179 | + // tag::knn-candidates[] |
| 1180 | + float[] coverImageEmbeddingsVector = /*...*/ |
| 1181 | + // end::knn-candidates[] |
| 1182 | + new float[128]; |
| 1183 | + // tag::knn-candidates[] |
| 1184 | + List<Book> hits = searchSession.search( Book.class ) |
| 1185 | + .where( f -> f.extension( ElasticsearchExtension.get() ) // <1> |
| 1186 | + .knn( 5 ).field( "coverImageEmbeddings" ).matching( coverImageEmbeddingsVector ) // <2> |
| 1187 | + .numberOfCandidates( 15 ) )// <3> |
| 1188 | + .fetchHits( 20 ); |
| 1189 | + // end::knn-candidates[] |
| 1190 | + assertThat( hits ) |
| 1191 | + .extracting( Book::getId ) |
| 1192 | + .containsExactlyInAnyOrder( BOOK1_ID, BOOK2_ID, BOOK3_ID, BOOK4_ID ); |
| 1193 | + } ); |
| 1194 | + } |
1146 | 1195 | }
|
1147 | 1196 |
|
1148 | 1197 | private MySearchParameters getSearchParameters() {
|
|
0 commit comments