|
| 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.integrationtest.backend.tck.search.predicate; |
| 8 | + |
| 9 | +import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThat; |
| 10 | +import static org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMapperUtils.referenceProvider; |
| 11 | + |
| 12 | +import java.time.LocalDate; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.function.Consumer; |
| 16 | +import java.util.function.Function; |
| 17 | + |
| 18 | +import org.hibernate.search.engine.backend.document.DocumentElement; |
| 19 | +import org.hibernate.search.engine.backend.document.IndexFieldAccessor; |
| 20 | +import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement; |
| 21 | +import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaFieldContext; |
| 22 | +import org.hibernate.search.engine.backend.document.model.dsl.StandardIndexSchemaFieldTypedContext; |
| 23 | +import org.hibernate.search.engine.backend.index.spi.IndexWorkPlan; |
| 24 | +import org.hibernate.search.engine.search.DocumentReference; |
| 25 | +import org.hibernate.search.engine.search.SearchQuery; |
| 26 | +import org.hibernate.search.engine.spatial.GeoPoint; |
| 27 | +import org.hibernate.search.integrationtest.backend.tck.configuration.DefaultAnalysisDefinitions; |
| 28 | +import org.hibernate.search.integrationtest.backend.tck.util.StandardFieldMapper; |
| 29 | +import org.hibernate.search.integrationtest.backend.tck.util.ValueWrapper; |
| 30 | +import org.hibernate.search.integrationtest.backend.tck.util.rule.SearchSetupHelper; |
| 31 | +import org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMappingIndexManager; |
| 32 | +import org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMappingSearchTarget; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +public class MatchIdSearchPredicateIT { |
| 38 | + |
| 39 | + private static final String INDEX_NAME = "IndexName"; |
| 40 | + |
| 41 | + private static final String DOCUMENT_1 = "document1"; |
| 42 | + private static final String DOCUMENT_2 = "document2"; |
| 43 | + private static final String DOCUMENT_3 = "document3"; |
| 44 | + private static final String EMPTY = "empty"; |
| 45 | + |
| 46 | + @Rule |
| 47 | + public SearchSetupHelper setupHelper = new SearchSetupHelper(); |
| 48 | + |
| 49 | + private IndexMapping indexMapping; |
| 50 | + private StubMappingIndexManager indexManager; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setup() { |
| 54 | + setupHelper.withDefaultConfiguration() |
| 55 | + .withIndex( |
| 56 | + "MappedType", INDEX_NAME, |
| 57 | + ctx -> this.indexMapping = new IndexMapping( ctx.getSchemaElement() ), |
| 58 | + indexManager -> this.indexManager = indexManager |
| 59 | + ) |
| 60 | + .setup(); |
| 61 | + |
| 62 | + initData(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void match_id() { |
| 67 | + StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); |
| 68 | + |
| 69 | + SearchQuery<DocumentReference> query = searchTarget.query() |
| 70 | + .asReference() |
| 71 | + .predicate( f -> f.id().matching( DOCUMENT_1 ).toPredicate() ) |
| 72 | + .build(); |
| 73 | + |
| 74 | + assertThat( query ) |
| 75 | + .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1 ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void match_multiple_ids() { |
| 80 | + StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); |
| 81 | + |
| 82 | + SearchQuery<DocumentReference> query = searchTarget.query() |
| 83 | + .asReference() |
| 84 | + .predicate( f -> f.id() |
| 85 | + .matching( DOCUMENT_1 ) |
| 86 | + .matching( DOCUMENT_3 ) |
| 87 | + .toPredicate() ) |
| 88 | + .build(); |
| 89 | + |
| 90 | + assertThat( query ) |
| 91 | + .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1, DOCUMENT_3 ); |
| 92 | + } |
| 93 | + |
| 94 | + private void initData() { |
| 95 | + IndexWorkPlan<? extends DocumentElement> workPlan = indexManager.createWorkPlan(); |
| 96 | + workPlan.add( referenceProvider( DOCUMENT_1 ), document -> { |
| 97 | + indexMapping.supportedFieldModels.forEach( f -> f.document1Value.write( document ) ); |
| 98 | + indexMapping.supportedFieldWithDslConverterModels.forEach( f -> f.document1Value.write( document ) ); |
| 99 | + indexMapping.unsupportedFieldModels.forEach( f -> f.document1Value.write( document ) ); |
| 100 | + } ); |
| 101 | + workPlan.add( referenceProvider( DOCUMENT_2 ), document -> { |
| 102 | + indexMapping.supportedFieldModels.forEach( f -> f.document2Value.write( document ) ); |
| 103 | + indexMapping.supportedFieldWithDslConverterModels.forEach( f -> f.document2Value.write( document ) ); |
| 104 | + indexMapping.unsupportedFieldModels.forEach( f -> f.document2Value.write( document ) ); |
| 105 | + } ); |
| 106 | + workPlan.add( referenceProvider( DOCUMENT_3 ), document -> { } ); |
| 107 | + workPlan.add( referenceProvider( EMPTY ), document -> { } ); |
| 108 | + workPlan.execute().join(); |
| 109 | + |
| 110 | + // Check that all documents are searchable |
| 111 | + StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); |
| 112 | + SearchQuery<DocumentReference> query = searchTarget.query() |
| 113 | + .asReference() |
| 114 | + .predicate( f -> f.matchAll().toPredicate() ) |
| 115 | + .build(); |
| 116 | + assertThat( query ).hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1, DOCUMENT_2, EMPTY, |
| 117 | + DOCUMENT_3 |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + private static class IndexMapping { |
| 122 | + final List<ByTypeFieldModel<?>> supportedFieldModels; |
| 123 | + final List<ByTypeFieldModel<?>> supportedFieldWithDslConverterModels; |
| 124 | + final List<ByTypeFieldModel<?>> unsupportedFieldModels; |
| 125 | + |
| 126 | + IndexMapping(IndexSchemaElement root) { |
| 127 | + supportedFieldModels = mapSupportedFields( root, "supported_", ignored -> { } ); |
| 128 | + supportedFieldWithDslConverterModels = mapSupportedFields( |
| 129 | + root, "supported_converted_", c -> c.dslConverter( ValueWrapper.toIndexFieldConverter() ) |
| 130 | + ); |
| 131 | + unsupportedFieldModels = Arrays.asList( |
| 132 | + ByTypeFieldModel.mapper( |
| 133 | + GeoPoint.class, |
| 134 | + GeoPoint.of( 40, 70 ), |
| 135 | + GeoPoint.of( 45, 98 ) |
| 136 | + ) |
| 137 | + .map( root, "geoPoint" ) |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + private List<ByTypeFieldModel<?>> mapSupportedFields(IndexSchemaElement root, String prefix, |
| 142 | + Consumer<StandardIndexSchemaFieldTypedContext<?, ?>> additionalConfiguration) { |
| 143 | + return Arrays.asList( |
| 144 | + ByTypeFieldModel.mapper( |
| 145 | + c -> c.asString().analyzer( DefaultAnalysisDefinitions.ANALYZER_STANDARD.name ), |
| 146 | + "irving and company", "Auster", "Irving" |
| 147 | + ) |
| 148 | + .map( |
| 149 | + root, prefix + "analyzedString", additionalConfiguration |
| 150 | + ), |
| 151 | + ByTypeFieldModel.mapper( String.class, "Irving", "Auster" ) |
| 152 | + .map( root, prefix + "nonAnalyzedString", additionalConfiguration ), |
| 153 | + ByTypeFieldModel.mapper( Integer.class, 42, 67 ) |
| 154 | + .map( root, prefix + "integer", additionalConfiguration ), |
| 155 | + ByTypeFieldModel.mapper( |
| 156 | + LocalDate.class, |
| 157 | + LocalDate.of( 1980, 10, 11 ), |
| 158 | + LocalDate.of( 1984, 10, 7 ) |
| 159 | + ) |
| 160 | + .map( root, prefix + "localDate", additionalConfiguration ) |
| 161 | + ); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static class ValueModel<F> { |
| 166 | + private final IndexFieldAccessor<F> accessor; |
| 167 | + final F indexedValue; |
| 168 | + |
| 169 | + private ValueModel(IndexFieldAccessor<F> accessor, F indexedValue) { |
| 170 | + this.accessor = accessor; |
| 171 | + this.indexedValue = indexedValue; |
| 172 | + } |
| 173 | + |
| 174 | + public void write(DocumentElement target) { |
| 175 | + accessor.write( target, indexedValue ); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private static class ByTypeFieldModel<F> { |
| 180 | + static <F> StandardFieldMapper<F, ByTypeFieldModel<F>> mapper(Class<F> type, |
| 181 | + F document1Value, F document2Value) { |
| 182 | + return mapper( |
| 183 | + c -> (StandardIndexSchemaFieldTypedContext<?, F>) c.as( type ), |
| 184 | + document1Value, document2Value, document1Value |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + static <F> StandardFieldMapper<F, ByTypeFieldModel<F>> mapper( |
| 189 | + Function<IndexSchemaFieldContext, StandardIndexSchemaFieldTypedContext<?, F>> configuration, |
| 190 | + F document1Value, F document2Value, F predicateParameterValue) { |
| 191 | + return (parent, name, additionalConfiguration) -> { |
| 192 | + IndexSchemaFieldContext untypedContext = parent.field( name ); |
| 193 | + StandardIndexSchemaFieldTypedContext<?, F> context = configuration.apply( untypedContext ); |
| 194 | + additionalConfiguration.accept( context ); |
| 195 | + IndexFieldAccessor<F> accessor = context.createAccessor(); |
| 196 | + return new ByTypeFieldModel<>( |
| 197 | + accessor, document1Value, document2Value |
| 198 | + ); |
| 199 | + }; |
| 200 | + } |
| 201 | + |
| 202 | + final ValueModel<F> document1Value; |
| 203 | + final ValueModel<F> document2Value; |
| 204 | + |
| 205 | + private ByTypeFieldModel(IndexFieldAccessor<F> accessor, |
| 206 | + F document1Value, F document2Value) { |
| 207 | + this.document1Value = new ValueModel<>( accessor, document1Value ); |
| 208 | + this.document2Value = new ValueModel<>( accessor, document2Value ); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments