Skip to content

Commit

Permalink
HSEARCH-4245 Implement withRoot(...) in query element factories
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Jun 17, 2021
1 parent 0669050 commit 91ed489
Show file tree
Hide file tree
Showing 37 changed files with 236 additions and 67 deletions.
Expand Up @@ -116,11 +116,34 @@ public ElasticsearchSearchIndexScopeImpl(BackendMappingContext mappingContext,
this.aggregationFactory = new ElasticsearchSearchAggregationBuilderFactory( this );
}

private ElasticsearchSearchIndexScopeImpl(ElasticsearchSearchIndexScopeImpl parentScope,
ElasticsearchSearchIndexCompositeNodeContext overriddenRoot) {
super( parentScope, overriddenRoot );
this.backendContext = parentScope.backendContext;
this.userFacingGson = parentScope.userFacingGson;
this.searchSyntax = parentScope.searchSyntax;
this.multiTenancyStrategy = parentScope.multiTenancyStrategy;
this.timingSource = parentScope.timingSource;
this.mappedTypeNameToIndex = parentScope.mappedTypeNameToIndex;
this.maxResultWindow = parentScope.maxResultWindow;

this.predicateBuilderFactory = new ElasticsearchSearchPredicateBuilderFactory( this );
this.sortBuilderFactory = new ElasticsearchSearchSortBuilderFactory( this );
this.projectionBuilderFactory = new ElasticsearchSearchProjectionBuilderFactory(
backendContext.getSearchProjectionBackendContext(), this );
this.aggregationFactory = new ElasticsearchSearchAggregationBuilderFactory( this );
}

@Override
protected ElasticsearchSearchIndexScopeImpl self() {
return this;
}

@Override
public ElasticsearchSearchIndexScopeImpl withRoot(String objectFieldPath) {
return new ElasticsearchSearchIndexScopeImpl( this, field( objectFieldPath ).toComposite() );
}

@Override
public ElasticsearchSearchPredicateBuilderFactory predicateBuilders() {
return predicateBuilderFactory;
Expand Down
Expand Up @@ -28,6 +28,13 @@ public ElasticsearchSearchAggregationFactoryImpl(
super( dslContext );
}

@Override
public ElasticsearchSearchAggregationFactory withRoot(String objectFieldPath) {
return new ElasticsearchSearchAggregationFactoryImpl( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ),
dslContext.predicateFactory().withRoot( objectFieldPath ) ) );
}

@Override
public AggregationFinalStep<JsonObject> fromJson(JsonObject jsonObject) {
return new ElasticsearchJsonAggregationFinalStep(
Expand Down
Expand Up @@ -44,12 +44,12 @@ protected ElasticsearchSearchIndexCompositeNodeTypeContext typeOf(
}

@Override
protected ElasticsearchSearchIndexNodeContext childInScope(String childRelativeName) {
return scope.child( this, childRelativeName );
public ElasticsearchSearchIndexValueFieldContext<?> toValueField() {
return SearchIndexSchemaElementContextHelper.throwingToValueField( this );
}

@Override
public ElasticsearchSearchIndexValueFieldContext<?> toValueField() {
return SearchIndexSchemaElementContextHelper.throwingToValueField( this );
protected ElasticsearchSearchIndexNodeContext childInScope(String childRelativeName) {
return scope.child( this, childRelativeName );
}
}
Expand Up @@ -27,6 +27,12 @@ public ElasticsearchSearchPredicateFactoryImpl(SearchPredicateDslContext<Elastic
super( dslContext );
}

@Override
public ElasticsearchSearchPredicateFactory withRoot(String objectFieldPath) {
return new ElasticsearchSearchPredicateFactoryImpl( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ) ) );
}

@Override
public PredicateFinalStep fromJson(String jsonString) {
return new StaticPredicateFinalStep( dslContext.scope().predicateBuilders().fromJson( jsonString ) );
Expand Down
Expand Up @@ -54,7 +54,12 @@ public class ElasticsearchSimpleQueryStringPredicate extends AbstractElasticsear
ElasticsearchSimpleQueryStringPredicate(Builder builder) {
super( builder );
nestedPathHierarchy = builder.firstFieldState.field().nestedPathHierarchy();
fieldPaths = new ArrayList<>( builder.fieldStates.keySet() );
// Warning: we must use field().absolutePath(), not the keys in the map,
// because that key may be a relative path when using SearchPredicateFactory.withRoot(...)
fieldPaths = new ArrayList<>( builder.fieldStates.size() );
for ( ElasticsearchSimpleQueryStringPredicateBuilderFieldState state : builder.fieldStates.values() ) {
fieldPaths.add( state.field().absolutePath() );
}
fieldNameAndBoosts = new ArrayList<>();
for ( ElasticsearchSimpleQueryStringPredicateBuilderFieldState fieldContext : builder.fieldStates.values() ) {
fieldNameAndBoosts.add( fieldContext.build() );
Expand All @@ -65,7 +70,6 @@ public class ElasticsearchSimpleQueryStringPredicate extends AbstractElasticsear
flags = builder.flags;
}


@Override
protected JsonObject doToJsonQuery(PredicateRequestContext context, JsonObject outerObject,
JsonObject innerObject) {
Expand Down
Expand Up @@ -28,6 +28,12 @@ public ElasticsearchSearchProjectionFactoryImpl(SearchProjectionDslContext<Elast
super( dslContext );
}

@Override
public ElasticsearchSearchProjectionFactory<R, E> withRoot(String objectFieldPath) {
return new ElasticsearchSearchProjectionFactoryImpl<>( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ) ) );
}

@Override
public ProjectionFinalStep<JsonObject> source() {
return new StaticProjectionFinalStep<>( dslContext.scope().projectionBuilders().source() );
Expand Down
Expand Up @@ -30,6 +30,13 @@ public ElasticsearchSearchSortFactoryImpl(SearchSortDslContext<ElasticsearchSear
super( dslContext );
}

@Override
public ElasticsearchSearchSortFactory withRoot(String objectFieldPath) {
return new ElasticsearchSearchSortFactoryImpl( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ),
dslContext.predicateFactory().withRoot( objectFieldPath ) ) );
}

@Override
public SortThenStep fromJson(JsonObject jsonObject) {
return staticThenStep( dslContext.scope().sortBuilders().fromJson( jsonObject ) );
Expand Down
Expand Up @@ -104,6 +104,21 @@ public LuceneSearchIndexScopeImpl(BackendMappingContext mappingContext,
this.aggregationBuilderFactory = new LuceneSearchAggregationBuilderFactory( this );
}

private LuceneSearchIndexScopeImpl(LuceneSearchIndexScopeImpl parentScope,
LuceneSearchIndexCompositeNodeContext overriddenRoot) {
super( parentScope, overriddenRoot );
this.backendContext = parentScope.backendContext;
this.analysisDefinitionRegistry = parentScope.analysisDefinitionRegistry;
this.multiTenancyStrategy = parentScope.multiTenancyStrategy;
this.timingSource = parentScope.timingSource;
this.mappedTypeNameToIndex = parentScope.mappedTypeNameToIndex;

this.predicateBuilderFactory = new LuceneSearchPredicateBuilderFactory( this );
this.sortBuilderFactory = new LuceneSearchSortBuilderFactory( this );
this.projectionBuilderFactory = new LuceneSearchProjectionBuilderFactory( this );
this.aggregationBuilderFactory = new LuceneSearchAggregationBuilderFactory( this );
}

private static Set<? extends LuceneIndexModel> toModels(
Set<? extends LuceneScopeIndexManagerContext> indexManagerContexts) {
return indexManagerContexts.stream().map( LuceneScopeIndexManagerContext::model )
Expand All @@ -115,6 +130,11 @@ protected LuceneSearchIndexScopeImpl self() {
return this;
}

@Override
public LuceneSearchIndexScopeImpl withRoot(String objectFieldPath) {
return new LuceneSearchIndexScopeImpl( this, field( objectFieldPath ).toComposite() );
}

@Override
public LuceneSearchPredicateBuilderFactory predicateBuilders() {
return predicateBuilderFactory;
Expand Down
Expand Up @@ -25,6 +25,13 @@ public LuceneSearchAggregationFactoryImpl(
super( dslContext );
}

@Override
public LuceneSearchAggregationFactory withRoot(String objectFieldPath) {
return new LuceneSearchAggregationFactoryImpl( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ),
dslContext.predicateFactory().withRoot( objectFieldPath ) ) );
}

// Empty: no extension at the moment.

}
Expand Up @@ -9,7 +9,6 @@
import java.util.List;

import org.hibernate.search.engine.search.common.spi.AbstractMultiIndexSearchIndexCompositeNodeContext;
import org.hibernate.search.engine.search.common.spi.SearchIndexSchemaElementContextHelper;

public final class LuceneMultiIndexSearchIndexCompositeNodeContext
extends AbstractMultiIndexSearchIndexCompositeNodeContext<
Expand Down Expand Up @@ -44,9 +43,4 @@ protected LuceneSearchIndexCompositeNodeTypeContext typeOf(LuceneSearchIndexComp
protected LuceneSearchIndexNodeContext childInScope(String childRelativeName) {
return scope.child( this, childRelativeName );
}

@Override
public LuceneSearchIndexValueFieldContext<?> toValueField() {
return SearchIndexSchemaElementContextHelper.throwingToValueField( this );
}
}
Expand Up @@ -14,7 +14,4 @@ public interface LuceneSearchIndexNodeContext
@Override
LuceneSearchIndexCompositeNodeContext toComposite();

@Override
LuceneSearchIndexValueFieldContext<?> toValueField();

}
Expand Up @@ -27,6 +27,12 @@ public LuceneSearchPredicateFactoryImpl(SearchPredicateDslContext<LuceneSearchPr
super( dslContext );
}

@Override
public LuceneSearchPredicateFactory withRoot(String objectFieldPath) {
return new LuceneSearchPredicateFactoryImpl( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ) ) );
}

@Override
public PredicateFinalStep fromLuceneQuery(Query luceneQuery) {
return new StaticPredicateFinalStep( dslContext.scope().predicateBuilders().fromLuceneQuery( luceneQuery ) );
Expand Down
Expand Up @@ -168,22 +168,25 @@ private Analyzer buildAnalyzer() {
* would pick the first analyzer returned by any of the scoped analyzers in its list.
*/
ScopedAnalyzer.Builder builder = new ScopedAnalyzer.Builder();
for ( Map.Entry<String, LuceneSimpleQueryStringPredicateBuilderFieldState> entry : fieldStates.entrySet() ) {
builder.setAnalyzer( entry.getKey(), entry.getValue().field().type().searchAnalyzerOrNormalizer() );
for ( LuceneSimpleQueryStringPredicateBuilderFieldState state : fieldStates.values() ) {
// Warning: we must use field().absolutePath(), not the key in the map,
// because that key may be a relative path when using SearchPredicateFactory.withRoot(...)
builder.setAnalyzer( state.field().absolutePath(), state.field().type().searchAnalyzerOrNormalizer() );
}
return builder.build();
}

private Map<String, Float> buildWeights() {
Map<String, Float> weights = new LinkedHashMap<>();
for ( Map.Entry<String, LuceneSimpleQueryStringPredicateBuilderFieldState> entry : fieldStates.entrySet() ) {
LuceneSimpleQueryStringPredicateBuilderFieldState state = entry.getValue();
for ( LuceneSimpleQueryStringPredicateBuilderFieldState state : fieldStates.values() ) {
Float boost = state.boost();
if ( boost == null ) {
boost = 1f;
}

weights.put( entry.getKey(), boost );
// Warning: we must use field().absolutePath(), not the key in the map,
// because that key may be a relative path when using SearchPredicateFactory.withRoot(...)
weights.put( state.field().absolutePath(), boost );
}
return weights;
}
Expand Down
Expand Up @@ -29,6 +29,12 @@ public LuceneSearchProjectionFactoryImpl(SearchProjectionDslContext<LuceneSearch
super( dslContext );
}

@Override
public LuceneSearchProjectionFactory<R, E> withRoot(String objectFieldPath) {
return new LuceneSearchProjectionFactoryImpl<>( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ) ) );
}

@Override
public ProjectionFinalStep<Document> document() {
return new StaticProjectionFinalStep<>( dslContext.scope().projectionBuilders().document() );
Expand Down
Expand Up @@ -29,6 +29,13 @@ public LuceneSearchSortFactoryImpl(SearchSortDslContext<LuceneSearchSortIndexSco
super( dslContext );
}

@Override
public LuceneSearchSortFactory withRoot(String objectFieldPath) {
return new LuceneSearchSortFactoryImpl( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ),
dslContext.predicateFactory().withRoot( objectFieldPath ) ) );
}

@Override
public SortThenStep fromLuceneSortField(SortField luceneSortField) {
return staticThenStep( dslContext.scope().sortBuilders().fromLuceneSortField( luceneSortField ) );
Expand Down
Expand Up @@ -50,6 +50,9 @@ public abstract class AbstractSearchIndexScope<
private final Set<String> hibernateSearchIndexNames;
private final Set<? extends M> indexModels;

// withRoot(...)
private final C overriddenRoot;

public AbstractSearchIndexScope(BackendMappingContext mappingContext, Set<? extends M> indexModels) {
this.mappingContext = mappingContext;

Expand All @@ -59,10 +62,25 @@ public AbstractSearchIndexScope(BackendMappingContext mappingContext, Set<? exte
hibernateSearchIndexNames.add( model.hibernateSearchName() );
}
this.indexModels = indexModels;

this.overriddenRoot = null;
}

protected final EventContext indexesEventContext() {
return EventContexts.fromIndexNames( hibernateSearchIndexNames );
protected AbstractSearchIndexScope(AbstractSearchIndexScope<S, M, N, C> parentScope, C overriddenRoot) {
this.mappingContext = parentScope.mappingContext;
this.hibernateSearchIndexNames = parentScope.hibernateSearchIndexNames;
this.indexModels = parentScope.indexModels;
this.overriddenRoot = overriddenRoot;
}

protected final EventContext eventContext() {
EventContext indexes = EventContexts.fromIndexNames( hibernateSearchIndexNames );
if ( overriddenRoot == null ) {
return indexes;
}
else {
return indexes.append( overriddenRoot.relativeEventContext() );
}
}

protected abstract S self();
Expand Down Expand Up @@ -95,13 +113,16 @@ public final DocumentIdentifierValueConverter<?> idDslConverter(ValueConvert val
}
else if ( !converter.isCompatibleWith( converterForIndex ) ) {
throw log.inconsistentConfigurationForIdentifierForSearch( converter, converterForIndex,
indexesEventContext() );
eventContext() );
}
}
return converter;
}

private C root() {
protected C root() {
if ( overriddenRoot != null ) {
return overriddenRoot;
}
if ( indexModels.size() == 1 ) {
return indexModels.iterator().next().root();
}
Expand All @@ -114,7 +135,14 @@ private C root() {
}
}

private N field(String absoluteFieldPath) {
protected N field(String fieldPath) {
if ( overriddenRoot != null ) {
return fieldInternal( overriddenRoot.absolutePath( fieldPath ) );
}
return fieldInternal( fieldPath );
}

private N fieldInternal(String absoluteFieldPath) {
N resultOrNull;
if ( indexModels.size() == 1 ) {
resultOrNull = indexModels.iterator().next().fieldOrNull( absoluteFieldPath );
Expand All @@ -123,14 +151,14 @@ private N field(String absoluteFieldPath) {
resultOrNull = createMultiIndexFieldContext( absoluteFieldPath );
}
if ( resultOrNull == null ) {
throw log.unknownFieldForSearch( absoluteFieldPath, indexesEventContext() );
throw log.unknownFieldForSearch( absoluteFieldPath, eventContext() );
}
return resultOrNull;
}

@Override
public final N child(SearchIndexCompositeNodeContext<?> parent, String name) {
return field( parent.absolutePath( name ) );
return fieldInternal( parent.absolutePath( name ) );
}

@SuppressWarnings({"rawtypes", "unchecked"}) // We check types using reflection
Expand Down Expand Up @@ -177,8 +205,8 @@ public final <T> T rootQueryElement(SearchQueryElementTypeKey<T> key) {
}

@Override
public final <T> T fieldQueryElement(String absoluteFieldPath, SearchQueryElementTypeKey<T> key) {
return field( absoluteFieldPath ).queryElement( key, self() );
public final <T> T fieldQueryElement(String fieldPath, SearchQueryElementTypeKey<T> key) {
return field( fieldPath ).queryElement( key, self() );
}

protected abstract C createMultiIndexSearchRootContext(List<C> rootForEachIndex);
Expand Down
Expand Up @@ -15,7 +15,7 @@
*/
public interface IndexScope {

SearchQueryIndexScope<?> searchScope();
SearchQueryIndexScope<? extends SearchQueryIndexScope<?>> searchScope();

/**
* Extend the current index scope with the given extension,
Expand Down
Expand Up @@ -27,10 +27,7 @@ public interface ExtendedSearchAggregationFactory<
extends SearchAggregationFactory {

@Override
// TODO implement and remove default
default S withRoot(String objectFieldPath) {
return (S) this;
}
S withRoot(String objectFieldPath);

@Override
RangeAggregationFieldStep<PDF> range();
Expand Down

0 comments on commit 91ed489

Please sign in to comment.