Skip to content

Commit

Permalink
HSEARCH-4162 Test that named predicates use paths relative to their o…
Browse files Browse the repository at this point in the history
…bject field for all predicates
  • Loading branch information
yrodiere committed Jun 17, 2021
1 parent 64c15e7 commit 2c1c483
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Expand Up @@ -10,6 +10,7 @@

import org.hibernate.search.engine.search.predicate.dsl.PredicateFinalStep;
import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory;
import org.hibernate.search.engine.search.predicate.factories.NamedPredicateProvider;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.BulkIndexer;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex;
import org.hibernate.search.util.impl.test.annotation.TestForIssue;
Expand Down Expand Up @@ -45,6 +46,30 @@ public void factoryWithRoot_flattened() {
.hasDocRefHitsAnyOrder( mainIndex.typeName(), dataSet.docId( 0 ) );
}

@Test
@TestForIssue(jiraKey = "HSEARCH-4162")
public void inNamedPredicate_nested() {
assertThatQuery( mainIndex.query()
.where( f -> f.named( binding.nested.absolutePath + "." + StubPredicateProvider.NAME )
.param( StubPredicateProvider.IMPL_PARAM_NAME, (NamedPredicateProvider) context ->
predicateWithRelativePath( context.predicate(), binding.nested, 0 )
.toPredicate() ) )
.routing( dataSet.routingKey ) )
.hasDocRefHitsAnyOrder( mainIndex.typeName(), dataSet.docId( 0 ) );
}

@Test
@TestForIssue(jiraKey = "HSEARCH-4162")
public void inNamedPredicate_flattened() {
assertThatQuery( mainIndex.query()
.where( f -> f.named( binding.flattened.absolutePath + "." + StubPredicateProvider.NAME )
.param( StubPredicateProvider.IMPL_PARAM_NAME, (NamedPredicateProvider) context ->
predicateWithRelativePath( context.predicate(), binding.flattened, 0 )
.toPredicate() ) )
.routing( dataSet.routingKey ) )
.hasDocRefHitsAnyOrder( mainIndex.typeName(), dataSet.docId( 0 ) );
}

@Override
protected final PredicateFinalStep predicate(SearchPredicateFactory f, ObjectFieldBinding objectFieldBinding,
int matchingDocOrdinal) {
Expand Down
Expand Up @@ -15,8 +15,11 @@
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField;
import org.hibernate.search.engine.backend.types.ObjectStructure;
import org.hibernate.search.engine.search.predicate.SearchPredicate;
import org.hibernate.search.engine.search.predicate.dsl.PredicateFinalStep;
import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory;
import org.hibernate.search.engine.search.predicate.factories.NamedPredicateProvider;
import org.hibernate.search.engine.search.predicate.factories.NamedPredicateProviderContext;
import org.hibernate.search.integrationtest.backend.tck.testsupport.types.FieldTypeDescriptor;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.SimpleFieldModelsByType;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex;
Expand Down Expand Up @@ -283,6 +286,7 @@ static ObjectFieldBinding create(IndexSchemaElement parent, String parentAbsolut
super( objectField, parentAbsolutePath == null ? relativeFieldName : parentAbsolutePath + "." + relativeFieldName, fieldTypes );
relativeName = relativeFieldName;
reference = objectField.toReference();
objectField.namedPredicate( StubPredicateProvider.NAME, new StubPredicateProvider() );
if ( depth < MAX_DEPTH ) {
nested = create( objectField, absolutePath, "nested", ObjectStructure.NESTED,
fieldTypes, depth + 1 );
Expand All @@ -295,4 +299,15 @@ static ObjectFieldBinding create(IndexSchemaElement parent, String parentAbsolut
}
}
}

public static class StubPredicateProvider implements NamedPredicateProvider {
public static final String NAME = "stub-predicate";
public static final String IMPL_PARAM_NAME = "impl";

@Override
public SearchPredicate create(NamedPredicateProviderContext context) {
NamedPredicateProvider impl = (NamedPredicateProvider) context.param( IMPL_PARAM_NAME );
return impl.create( context );
}
}
}

0 comments on commit 2c1c483

Please sign in to comment.