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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HSEARCH-3319 WIP: Type-safe field references v2 #4156

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,23 @@
*
* @see #get()
*/
public final class ElasticsearchExtension<H, R, E, LOS>
implements SearchQueryDslExtension<ElasticsearchSearchQuerySelectStep<R, E, LOS>, R, E, LOS>,
public final class ElasticsearchExtension<SR, H, R, E, LOS>
implements SearchQueryDslExtension<SR, ElasticsearchSearchQuerySelectStep<SR, R, E, LOS>, R, E, LOS>,
SearchQueryExtension<ElasticsearchSearchQuery<H>, H>,
SearchPredicateFactoryExtension<ElasticsearchSearchPredicateFactory>,
SearchSortFactoryExtension<ElasticsearchSearchSortFactory>,
SearchProjectionFactoryExtension<ElasticsearchSearchProjectionFactory<R, E>, R, E>,
SearchAggregationFactoryExtension<ElasticsearchSearchAggregationFactory>,
SearchPredicateFactoryExtension<SR, ElasticsearchSearchPredicateFactory<SR>>,
SearchSortFactoryExtension<SR, ElasticsearchSearchSortFactory<SR>>,
SearchProjectionFactoryExtension<SR, ElasticsearchSearchProjectionFactory<SR, R, E>, R, E>,
SearchAggregationFactoryExtension<SR, ElasticsearchSearchAggregationFactory<SR>>,
IndexFieldTypeFactoryExtension<ElasticsearchIndexFieldTypeFactory>,
SchemaExportExtension<ElasticsearchIndexSchemaExport> {

private static final ElasticsearchExtension<Object, Object, Object, Object> INSTANCE = new ElasticsearchExtension<>();
private static final ElasticsearchExtension<Object, Object, Object, Object, Object> INSTANCE =
new ElasticsearchExtension<>();

/**
* Get the extension with generic parameters automatically set as appropriate for the context in which it's used.
*
* @param <SR> Scope root type.
* @param <H> The type of query hits.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() )}.
Expand All @@ -90,8 +92,8 @@ public final class ElasticsearchExtension<H, R, E, LOS>
* @return The extension.
*/
@SuppressWarnings("unchecked") // The instance works for any H, R, E and LOS
public static <H, R, E, LOS> ElasticsearchExtension<H, R, E, LOS> get() {
return (ElasticsearchExtension<H, R, E, LOS>) INSTANCE;
public static <SR, H, R, E, LOS> ElasticsearchExtension<SR, H, R, E, LOS> get() {
return (ElasticsearchExtension<SR, H, R, E, LOS>) INSTANCE;
}

private ElasticsearchExtension() {
Expand All @@ -102,8 +104,8 @@ private ElasticsearchExtension() {
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchQuerySelectStep<R, E, LOS>> extendOptional(
SearchQuerySelectStep<?, R, E, LOS, ?, ?> original,
public Optional<ElasticsearchSearchQuerySelectStep<SR, R, E, LOS>> extendOptional(
SearchQuerySelectStep<SR, ?, R, E, LOS, ?, ?> original,
SearchQueryIndexScope<?> scope,
BackendSessionContext sessionContext,
SearchLoadingContextBuilder<E, LOS> loadingContextBuilder) {
Expand Down Expand Up @@ -135,9 +137,9 @@ public Optional<ElasticsearchSearchQuery<H>> extendOptional(SearchQuery<H> origi
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchPredicateFactory> extendOptional(SearchPredicateFactory original) {
public Optional<ElasticsearchSearchPredicateFactory<SR>> extendOptional(SearchPredicateFactory<SR> original) {
if ( original instanceof ElasticsearchSearchPredicateFactory ) {
return Optional.of( (ElasticsearchSearchPredicateFactory) original );
return Optional.of( (ElasticsearchSearchPredicateFactory<SR>) original );
}
else {
return Optional.empty();
Expand All @@ -148,10 +150,10 @@ public Optional<ElasticsearchSearchPredicateFactory> extendOptional(SearchPredic
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchSortFactory> extendOptional(
SearchSortFactory original) {
public Optional<ElasticsearchSearchSortFactory<SR>> extendOptional(
SearchSortFactory<SR> original) {
if ( original instanceof ElasticsearchSearchSortFactory ) {
return Optional.of( (ElasticsearchSearchSortFactory) original );
return Optional.of( (ElasticsearchSearchSortFactory<SR>) original );
}
else {
return Optional.empty();
Expand All @@ -162,9 +164,9 @@ public Optional<ElasticsearchSearchSortFactory> extendOptional(
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchProjectionFactory<R, E>> extendOptional(SearchProjectionFactory<R, E> original) {
public Optional<ElasticsearchSearchProjectionFactory<SR, R, E>> extendOptional(SearchProjectionFactory<SR, R, E> original) {
if ( original instanceof ElasticsearchSearchProjectionFactory ) {
return Optional.of( (ElasticsearchSearchProjectionFactory<R, E>) original );
return Optional.of( (ElasticsearchSearchProjectionFactory<SR, R, E>) original );
}
else {
return Optional.empty();
Expand All @@ -175,10 +177,10 @@ public Optional<ElasticsearchSearchProjectionFactory<R, E>> extendOptional(Searc
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchAggregationFactory> extendOptional(
SearchAggregationFactory original) {
public Optional<ElasticsearchSearchAggregationFactory<SR>> extendOptional(
SearchAggregationFactory<SR> original) {
if ( original instanceof ElasticsearchSearchAggregationFactory ) {
return Optional.of( (ElasticsearchSearchAggregationFactory) original );
return Optional.of( (ElasticsearchSearchAggregationFactory<SR>) original );
}
else {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,25 @@ public <P> ElasticsearchSearchQueryBuilder<P> select(BackendSessionContext sessi
}

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

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

@Override
public <R, E> ElasticsearchSearchProjectionFactory<R, E> projectionFactory() {
public <SR, R, E> ElasticsearchSearchProjectionFactory<SR, R, E> projectionFactory() {
return new ElasticsearchSearchProjectionFactoryImpl<>( SearchProjectionDslContext.root( this ) );
}

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

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

import com.google.gson.JsonObject;

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

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

import com.google.gson.JsonObject;

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

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

@Override
public ElasticsearchSearchAggregationFactory withRoot(String objectFieldPath) {
return new ElasticsearchSearchAggregationFactoryImpl( dslContext.rescope(
public ElasticsearchSearchAggregationFactory<SR> withRoot(String objectFieldPath) {
return new ElasticsearchSearchAggregationFactoryImpl<SR>( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ),
dslContext.predicateFactory().withRoot( objectFieldPath ) ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

/**
* A factory for search predicates with some Elasticsearch-specific methods.
*
* @param <SR> Scope root type.
*/
public interface ElasticsearchSearchPredicateFactory
extends ExtendedSearchPredicateFactory<ElasticsearchSearchPredicateFactory> {
public interface ElasticsearchSearchPredicateFactory<SR>
extends ExtendedSearchPredicateFactory<SR, ElasticsearchSearchPredicateFactory<SR>> {

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

import com.google.gson.JsonObject;

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static class Builder extends AbstractBuilder implements NamedPredicateBu
private final PredicateDefinition definition;
private final String predicateName;
private final ElasticsearchSearchIndexCompositeNodeContext field;
private SearchPredicateFactory factory;
private SearchPredicateFactory<?> factory;
private final Map<String, Object> params = new LinkedHashMap<>();

Builder(PredicateDefinition definition, String predicateName,
Expand All @@ -84,7 +84,7 @@ private static class Builder extends AbstractBuilder implements NamedPredicateBu
}

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

Expand All @@ -95,8 +95,9 @@ public void param(String name, Object value) {

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

ElasticsearchSearchPredicate providedPredicate = ElasticsearchSearchPredicate.from(
scope, definition.create( ctx ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
/**
* A factory for search projections with some Elasticsearch-specific methods.
*
* @param <SR> Scope root type.
* @param <R> The type of entity references.
* @param <E> The type of entities.
* @see SearchProjectionFactory
*/
public interface ElasticsearchSearchProjectionFactory<R, E>
extends ExtendedSearchProjectionFactory<ElasticsearchSearchProjectionFactory<R, E>, R, E> {
public interface ElasticsearchSearchProjectionFactory<SR, R, E>
extends ExtendedSearchProjectionFactory<SR, ElasticsearchSearchProjectionFactory<SR, R, E>, R, E> {

/**
* Project to a {@link JsonObject} representing the document as stored in Elasticsearch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@

import com.google.gson.JsonObject;

public class ElasticsearchSearchProjectionFactoryImpl<R, E>
public class ElasticsearchSearchProjectionFactoryImpl<SR, R, E>
extends AbstractSearchProjectionFactory<
ElasticsearchSearchProjectionFactory<R, E>,
SR,
ElasticsearchSearchProjectionFactory<SR, R, E>,
ElasticsearchSearchProjectionIndexScope<?>,
R,
E>
implements ElasticsearchSearchProjectionFactory<R, E> {
implements ElasticsearchSearchProjectionFactory<SR, R, E> {

public ElasticsearchSearchProjectionFactoryImpl(
SearchProjectionDslContext<ElasticsearchSearchProjectionIndexScope<?>> dslContext) {
super( dslContext );
}

@Override
public ElasticsearchSearchProjectionFactory<R, E> withRoot(String objectFieldPath) {
public ElasticsearchSearchProjectionFactory<SR, R, E> withRoot(String objectFieldPath) {
return new ElasticsearchSearchProjectionFactoryImpl<>( dslContext.rescope(
dslContext.scope().withRoot( objectFieldPath ) ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,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<SR, H, LOS>
extends SearchQueryOptionsStep<
ElasticsearchSearchQueryOptionsStep<H, LOS>,
SR,
ElasticsearchSearchQueryOptionsStep<SR, H, LOS>,
H,
LOS,
ElasticsearchSearchSortFactory,
ElasticsearchSearchAggregationFactory>,
ElasticsearchSearchSortFactory<SR>,
ElasticsearchSearchAggregationFactory<SR>>,
ElasticsearchSearchFetchable<H> {

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

@Override
ElasticsearchSearchQuery<H> toQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,35 @@
import org.hibernate.search.engine.search.projection.dsl.ProjectionFinalStep;
import org.hibernate.search.engine.search.query.dsl.SearchQuerySelectStep;

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

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

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

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

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
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<SR, H, LOS>
extends
SearchQueryWhereStep<SR,
ElasticsearchSearchQueryOptionsStep<SR, H, LOS>,
H,
LOS,
ElasticsearchSearchPredicateFactory<SR>> {

}
Loading
Loading