Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

HSEARCH-3319 WIP: DRAFT: IDEA: TEST: Type-safe field references #4113

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
public final class ElasticsearchExtension<H, R, E, LOS>
implements SearchQueryDslExtension<ElasticsearchSearchQuerySelectStep<R, E, LOS>, R, E, LOS>,
SearchQueryExtension<ElasticsearchSearchQuery<H>, H>,
SearchPredicateFactoryExtension<ElasticsearchSearchPredicateFactory>,
SearchSortFactoryExtension<ElasticsearchSearchSortFactory>,
SearchPredicateFactoryExtension<ElasticsearchSearchPredicateFactory<E>>,
SearchSortFactoryExtension<ElasticsearchSearchSortFactory<E>>,
SearchProjectionFactoryExtension<ElasticsearchSearchProjectionFactory<R, E>, R, E>,
SearchAggregationFactoryExtension<ElasticsearchSearchAggregationFactory>,
SearchAggregationFactoryExtension<ElasticsearchSearchAggregationFactory<E>>,
IndexFieldTypeFactoryExtension<ElasticsearchIndexFieldTypeFactory>,
SchemaExportExtension<ElasticsearchIndexSchemaExport> {

Expand Down Expand Up @@ -181,8 +181,8 @@ public Optional<ElasticsearchSearchProjectionFactory<R, E>> extendOptional(Searc
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchAggregationFactory> extendOptional(
SearchAggregationFactory original) {
public Optional<ElasticsearchSearchAggregationFactory<E>> extendOptional(
SearchAggregationFactory<?> original) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SearchAggregationFactory<?> original) {
SearchAggregationFactory<E> original) {

See SearchProjectionFactoryExtension.

if ( original instanceof ElasticsearchSearchAggregationFactory ) {
return Optional.of( (ElasticsearchSearchAggregationFactory) original );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ public <P> ElasticsearchSearchQueryBuilder<P> select(BackendSessionContext sessi
}

@Override
public ElasticsearchSearchPredicateFactory predicateFactory() {
return new ElasticsearchSearchPredicateFactoryImpl( SearchPredicateDslContext.root( this ) );
public <E> ElasticsearchSearchPredicateFactory<E> predicateFactory() {
return new ElasticsearchSearchPredicateFactoryImpl<>( SearchPredicateDslContext.root( this ) );
}

@Override
public ElasticsearchSearchSortFactory sortFactory() {
return new ElasticsearchSearchSortFactoryImpl( SearchSortDslContext
public <E> ElasticsearchSearchSortFactory<E> sortFactory() {
return new ElasticsearchSearchSortFactoryImpl<>( SearchSortDslContext
.root( this, ElasticsearchSearchSortFactoryImpl::new, predicateFactory() ) );
}

Expand All @@ -189,8 +189,8 @@ public <R, E> ElasticsearchSearchProjectionFactory<R, E> projectionFactory() {
}

@Override
public ElasticsearchSearchAggregationFactory aggregationFactory() {
return new ElasticsearchSearchAggregationFactoryImpl( SearchAggregationDslContext.root( this, predicateFactory() ) );
public <E> ElasticsearchSearchAggregationFactory<E> aggregationFactory() {
return new ElasticsearchSearchAggregationFactoryImpl<>( SearchAggregationDslContext.root( this, predicateFactory() ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

import com.google.gson.JsonObject;

public interface ElasticsearchSearchAggregationFactory
extends ExtendedSearchAggregationFactory<ElasticsearchSearchAggregationFactory, ElasticsearchSearchPredicateFactory> {
public interface ElasticsearchSearchAggregationFactory<E>
extends ExtendedSearchAggregationFactory<E, ElasticsearchSearchAggregationFactory<E>, ElasticsearchSearchPredicateFactory<E>> {

/**
* Create an aggregation from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@

import com.google.gson.JsonObject;

public class ElasticsearchSearchAggregationFactoryImpl
public class ElasticsearchSearchAggregationFactoryImpl<E>
extends AbstractSearchAggregationFactory<
ElasticsearchSearchAggregationFactory,
E,
ElasticsearchSearchAggregationFactory<E>,
ElasticsearchSearchAggregationIndexScope<?>,
ElasticsearchSearchPredicateFactory>
implements ElasticsearchSearchAggregationFactory {
ElasticsearchSearchPredicateFactory<E>>
implements ElasticsearchSearchAggregationFactory<E> {

public ElasticsearchSearchAggregationFactoryImpl(
SearchAggregationDslContext<ElasticsearchSearchAggregationIndexScope<?>,
ElasticsearchSearchPredicateFactory> dslContext) {
SearchAggregationDslContext<E,
ElasticsearchSearchAggregationIndexScope<?>,
ElasticsearchSearchPredicateFactory<E>> dslContext) {
super( dslContext );
}

@Override
public ElasticsearchSearchAggregationFactory withRoot(String objectFieldPath) {
return new ElasticsearchSearchAggregationFactoryImpl( dslContext.rescope(
public ElasticsearchSearchAggregationFactory<E> withRoot(String objectFieldPath) {
return new ElasticsearchSearchAggregationFactoryImpl<>( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ),
dslContext.predicateFactory().withRoot( objectFieldPath ) ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
/**
* A factory for search predicates with some Elasticsearch-specific methods.
*/
public interface ElasticsearchSearchPredicateFactory
extends ExtendedSearchPredicateFactory<ElasticsearchSearchPredicateFactory> {
public interface ElasticsearchSearchPredicateFactory<E>
extends ExtendedSearchPredicateFactory<E, ElasticsearchSearchPredicateFactory<E>> {

/**
* Create a predicate from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@

import com.google.gson.JsonObject;

public class ElasticsearchSearchPredicateFactoryImpl
public class ElasticsearchSearchPredicateFactoryImpl<E>
extends AbstractSearchPredicateFactory<
ElasticsearchSearchPredicateFactory,
E,
ElasticsearchSearchPredicateFactory<E>,
ElasticsearchSearchPredicateIndexScope<?>>
implements ElasticsearchSearchPredicateFactory {
implements ElasticsearchSearchPredicateFactory<E> {

public ElasticsearchSearchPredicateFactoryImpl(
SearchPredicateDslContext<ElasticsearchSearchPredicateIndexScope<?>> dslContext) {
super( dslContext );
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

import com.google.gson.JsonObject;

public class ElasticsearchNamedPredicate extends AbstractElasticsearchSingleFieldPredicate {
public class ElasticsearchNamedPredicate<E> extends AbstractElasticsearchSingleFieldPredicate {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

private final ElasticsearchSearchPredicate providedPredicate;

private ElasticsearchNamedPredicate(Builder builder, ElasticsearchSearchPredicate providedPredicate) {
private ElasticsearchNamedPredicate(Builder<E> builder, ElasticsearchSearchPredicate providedPredicate) {
super( builder );
this.providedPredicate = providedPredicate;
}
Expand All @@ -47,40 +47,40 @@ protected JsonObject doToJsonQuery(PredicateRequestContext context,
return providedPredicate.toJsonQuery( context );
}

public static class Factory
extends AbstractElasticsearchCompositeNodeSearchQueryElementFactory<NamedPredicateBuilder> {
private final PredicateDefinition definition;
public static class Factory<E>
extends AbstractElasticsearchCompositeNodeSearchQueryElementFactory<NamedPredicateBuilder<E>> {
private final PredicateDefinition<E> definition;
private final String predicateName;

public Factory(PredicateDefinition definition, String predicateName) {
public Factory(PredicateDefinition<E> definition, String predicateName) {
this.definition = definition;
this.predicateName = predicateName;
}

@Override
public void checkCompatibleWith(SearchQueryElementFactory<?, ?, ?> other) {
super.checkCompatibleWith( other );
Factory castedOther = (Factory) other;
Factory<E> castedOther = (Factory<E>) other;
if ( !definition.equals( castedOther.definition ) ) {
throw log.differentPredicateDefinitionForQueryElement( definition, castedOther.definition );
}
}

@Override
public NamedPredicateBuilder create(ElasticsearchSearchIndexScope<?> scope,
public NamedPredicateBuilder<E> create(ElasticsearchSearchIndexScope<?> scope,
ElasticsearchSearchIndexCompositeNodeContext node) {
return new Builder( definition, predicateName, scope, node );
return new Builder<>( definition, predicateName, scope, node );
}
}

private static class Builder extends AbstractBuilder implements NamedPredicateBuilder {
private final PredicateDefinition definition;
private static class Builder<E> extends AbstractBuilder implements NamedPredicateBuilder<E> {
private final PredicateDefinition<E> definition;
private final String predicateName;
private final ElasticsearchSearchIndexCompositeNodeContext field;
private SearchPredicateFactory factory;
private SearchPredicateFactory<E> factory;
private final Map<String, Object> params = new LinkedHashMap<>();

Builder(PredicateDefinition definition, String predicateName,
Builder(PredicateDefinition<E> definition, String predicateName,
ElasticsearchSearchIndexScope<?> scope,
ElasticsearchSearchIndexCompositeNodeContext node) {
super( scope, node );
Expand All @@ -90,7 +90,7 @@ private static class Builder extends AbstractBuilder implements NamedPredicateBu
}

@Override
public void factory(SearchPredicateFactory factory) {
public void factory(SearchPredicateFactory<E> factory) {
this.factory = factory;
}

Expand All @@ -101,13 +101,14 @@ public void param(String name, Object value) {

@Override
public SearchPredicate build() {
NamedValuesBasedPredicateDefinitionContext ctx = new NamedValuesBasedPredicateDefinitionContext( factory, params,
name -> log.paramNotDefined( name, predicateName, field.eventContext() ) );
NamedValuesBasedPredicateDefinitionContext<E> ctx =
new NamedValuesBasedPredicateDefinitionContext<>( factory, params,
name -> log.paramNotDefined( name, predicateName, field.eventContext() ) );

ElasticsearchSearchPredicate providedPredicate = ElasticsearchSearchPredicate.from(
scope, definition.create( ctx ) );

return new ElasticsearchNamedPredicate( this, providedPredicate );
return new ElasticsearchNamedPredicate<>( this, providedPredicate );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import org.hibernate.search.engine.search.query.dsl.SearchQueryOptionsStep;
import org.hibernate.search.util.common.annotation.Incubating;

public interface ElasticsearchSearchQueryOptionsStep<H, LOS>
public interface ElasticsearchSearchQueryOptionsStep<E, H, LOS>
extends SearchQueryOptionsStep<
ElasticsearchSearchQueryOptionsStep<H, LOS>,
E,
ElasticsearchSearchQueryOptionsStep<E, H, LOS>,
H,
LOS,
ElasticsearchSearchSortFactory,
ElasticsearchSearchAggregationFactory>,
ElasticsearchSearchSortFactory<E>,
ElasticsearchSearchAggregationFactory<E>>,
ElasticsearchSearchFetchable<H> {

/**
Expand All @@ -36,7 +37,7 @@ public interface ElasticsearchSearchQueryOptionsStep<H, LOS>
* @return {@code this}, for method chaining.
*/
@Incubating
ElasticsearchSearchQueryOptionsStep<H, LOS> requestTransformer(ElasticsearchSearchRequestTransformer transformer);
ElasticsearchSearchQueryOptionsStep<E, H, LOS> requestTransformer(ElasticsearchSearchRequestTransformer transformer);

@Override
ElasticsearchSearchQuery<H> toQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@

public interface ElasticsearchSearchQuerySelectStep<R, E, LOS>
extends SearchQuerySelectStep<
ElasticsearchSearchQueryOptionsStep<E, LOS>,
ElasticsearchSearchQueryOptionsStep<E, E, LOS>,
R,
E,
LOS,
ElasticsearchSearchProjectionFactory<R, E>,
ElasticsearchSearchPredicateFactory>,
ElasticsearchSearchQueryWhereStep<E, LOS> {
ElasticsearchSearchPredicateFactory<E>>,
ElasticsearchSearchQueryWhereStep<E, E, LOS> {

@Override
ElasticsearchSearchQueryWhereStep<E, LOS> selectEntity();
ElasticsearchSearchQueryWhereStep<E, E, LOS> selectEntity();

@Override
ElasticsearchSearchQueryWhereStep<R, LOS> selectEntityReference();
ElasticsearchSearchQueryWhereStep<E, R, LOS> selectEntityReference();

@Override
<P> ElasticsearchSearchQueryWhereStep<P, LOS> select(Class<P> objectClass);
<P> ElasticsearchSearchQueryWhereStep<E, P, LOS> select(Class<P> objectClass);

@Override
<P> ElasticsearchSearchQueryWhereStep<P, LOS> select(
<P> ElasticsearchSearchQueryWhereStep<E, P, LOS> select(
Function<? super ElasticsearchSearchProjectionFactory<R, E>,
? extends ProjectionFinalStep<P>> projectionContributor);

@Override
<P> ElasticsearchSearchQueryWhereStep<P, LOS> select(SearchProjection<P> projection);
<P> ElasticsearchSearchQueryWhereStep<E, P, LOS> select(SearchProjection<P> projection);

@Override
ElasticsearchSearchQueryWhereStep<List<?>, LOS> select(SearchProjection<?>... projections);
ElasticsearchSearchQueryWhereStep<E, List<?>, LOS> select(SearchProjection<?>... projections);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.hibernate.search.backend.elasticsearch.search.predicate.dsl.ElasticsearchSearchPredicateFactory;
import org.hibernate.search.engine.search.query.dsl.SearchQueryWhereStep;

public interface ElasticsearchSearchQueryWhereStep<H, LOS>
extends SearchQueryWhereStep<ElasticsearchSearchQueryOptionsStep<H, LOS>, H, LOS, ElasticsearchSearchPredicateFactory> {
public interface ElasticsearchSearchQueryWhereStep<E, H, LOS>
extends SearchQueryWhereStep<E, ElasticsearchSearchQueryOptionsStep<E, H, LOS>, H, LOS, ElasticsearchSearchPredicateFactory<E>> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
import org.hibernate.search.engine.search.loading.spi.SearchLoadingContextBuilder;
import org.hibernate.search.engine.search.query.dsl.spi.AbstractExtendedSearchQueryOptionsStep;

class ElasticsearchSearchQueryOptionsStepImpl<H, LOS>
class ElasticsearchSearchQueryOptionsStepImpl<E, H, LOS>
extends AbstractExtendedSearchQueryOptionsStep<
ElasticsearchSearchQueryOptionsStep<H, LOS>,
E,
ElasticsearchSearchQueryOptionsStep<E, H, LOS>,
H,
ElasticsearchSearchResult<H>,
ElasticsearchSearchScroll<H>,
LOS,
ElasticsearchSearchPredicateFactory,
ElasticsearchSearchSortFactory,
ElasticsearchSearchAggregationFactory,
ElasticsearchSearchPredicateFactory<E>,
ElasticsearchSearchSortFactory<E>,
ElasticsearchSearchAggregationFactory<E>,
ElasticsearchSearchQueryIndexScope<?>>
implements ElasticsearchSearchQueryWhereStep<H, LOS>, ElasticsearchSearchQueryOptionsStep<H, LOS> {
implements ElasticsearchSearchQueryWhereStep<E, H, LOS>, ElasticsearchSearchQueryOptionsStep<E, H, LOS> {

private final ElasticsearchSearchQueryBuilder<H> searchQueryBuilder;

Expand All @@ -44,7 +45,7 @@ class ElasticsearchSearchQueryOptionsStepImpl<H, LOS>
}

@Override
public ElasticsearchSearchQueryOptionsStep<H, LOS> requestTransformer(ElasticsearchSearchRequestTransformer transformer) {
public ElasticsearchSearchQueryOptionsStep<E, H, LOS> requestTransformer(ElasticsearchSearchRequestTransformer transformer) {
searchQueryBuilder.requestTransformer( transformer );
return thisAsS();
}
Expand All @@ -55,22 +56,22 @@ public ElasticsearchSearchQuery<H> toQuery() {
}

@Override
protected ElasticsearchSearchQueryOptionsStepImpl<H, LOS> thisAsS() {
protected ElasticsearchSearchQueryOptionsStepImpl<E, H, LOS> thisAsS() {
return this;
}

@Override
protected ElasticsearchSearchPredicateFactory predicateFactory() {
protected ElasticsearchSearchPredicateFactory<E> predicateFactory() {
return scope.predicateFactory();
}

@Override
protected ElasticsearchSearchSortFactory sortFactory() {
protected ElasticsearchSearchSortFactory<E> sortFactory() {
return scope.sortFactory();
}

@Override
protected ElasticsearchSearchAggregationFactory aggregationFactory() {
protected ElasticsearchSearchAggregationFactory<E> aggregationFactory() {
return scope.aggregationFactory();
}

Expand Down