Skip to content

Commit

Permalink
HSEARCH-3510 Add DslConverter option on range from to above below
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever authored and yrodiere committed Mar 26, 2019
1 parent c94483f commit 312ceb0
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 30 deletions.
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.search.backend.elasticsearch.types.codec.impl.ElasticsearchFieldCodec;
import org.hibernate.search.engine.backend.types.converter.ToDocumentFieldValueConverter;
import org.hibernate.search.engine.reporting.spi.EventContexts;
import org.hibernate.search.engine.search.predicate.DslConverter;
import org.hibernate.search.engine.search.predicate.spi.RangePredicateBuilder;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

Expand Down Expand Up @@ -58,7 +59,7 @@ public ElasticsearchRangePredicateBuilder(ElasticsearchSearchContext searchConte
}

@Override
public void lowerLimit(Object value) {
public void lowerLimit(Object value, DslConverter dslConverter) {
try {
F converted = dslToIndexConverter.convertUnknown( value, searchContext.getToDocumentFieldValueConvertContext() );
this.lowerLimit = codec.encode( converted );
Expand All @@ -76,7 +77,7 @@ public void excludeLowerLimit() {
}

@Override
public void upperLimit(Object value) {
public void upperLimit(Object value, DslConverter dslConverter) {
try {
F converted = dslToIndexConverter.convertUnknown( value, searchContext.getToDocumentFieldValueConvertContext() );
this.upperLimit = codec.encode( converted );
Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.hibernate.search.backend.lucene.types.codec.impl.LuceneStandardFieldCodec;
import org.hibernate.search.engine.backend.types.converter.ToDocumentFieldValueConverter;
import org.hibernate.search.engine.reporting.spi.EventContexts;
import org.hibernate.search.engine.search.predicate.DslConverter;
import org.hibernate.search.engine.search.predicate.spi.RangePredicateBuilder;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

Expand Down Expand Up @@ -53,7 +54,8 @@ protected AbstractLuceneStandardRangePredicateBuilder(
}

@Override
public void lowerLimit(Object value) {
public void lowerLimit(Object value, DslConverter dslConverter) {
// TODO handle dslConverter
try {
F converted = converter.convertUnknown( value, searchContext.getToDocumentFieldValueConvertContext() );
lowerLimit = codec.encode( converted );
Expand All @@ -71,7 +73,8 @@ public void excludeLowerLimit() {
}

@Override
public void upperLimit(Object value) {
public void upperLimit(Object value, DslConverter dslConverter) {
// TODO handle dslConverter
try {
F converted = converter.convertUnknown( value, searchContext.getToDocumentFieldValueConvertContext() );
upperLimit = codec.encode( converted );
Expand Down
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.search.engine.search.dsl.predicate;

import org.hibernate.search.engine.search.predicate.DslConverter;

/**
* The context used when defining a range predicate, after at least one field was mentioned.
*/
Expand Down Expand Up @@ -67,6 +69,29 @@ default RangePredicateFieldSetContext orRawField(String absoluteFieldPath) {
*/
RangePredicateFieldSetContext orRawFields(String... absoluteFieldPaths);

/**
* Require at least one of the targeted fields to be "higher than" the given value,
* and "lower than" another value (to be provided in following calls).
* <p>
* This syntax is essentially used like this: {@code .from( lowerBound ).to( upperBound )}.
* <p>
* This method will apply DSL converters to {@code value} before Hibernate Search attempts to interpret it as a field value.
* See {@link DslConverter#ENABLED}.
*
* @param value The lower bound of the range. May be null, in which case the range has no lower bound
* and the upper bound (passed to {@link RangePredicateFromContext#to(Object)}) must not be null.
* The signature of this method defines this parameter as an {@link Object},
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateFactoryContext.html#commonconcepts-parametertype">there</a> for more information.
*
* Lower bound is included by default in the range.
*
* @return A context allowing to exclude the lower bound from the range or to set the upper bound of the range.
*/
default RangePredicateFromContext from(Object value) {
return from( value, DslConverter.ENABLED );
}

/**
* Require at least one of the targeted fields to be "higher than" the given value,
* and "lower than" another value (to be provided in following calls).
Expand All @@ -78,12 +103,34 @@ default RangePredicateFieldSetContext orRawField(String absoluteFieldPath) {
* The signature of this method defines this parameter as an {@link Object},
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateFactoryContext.html#commonconcepts-parametertype">there</a> for more information.
* @param dslConverter Controls how the {@code value} should be converted before Hibernate Search attempts to interpret it as a field value.
* See {@link DslConverter}.
*
* Lower bound is included by default in the range.
*
* @return A context allowing to exclude the lower bound from the range or to set the upper bound of the range.
*/
RangePredicateFromContext from(Object value);
RangePredicateFromContext from(Object value, DslConverter dslConverter);

/**
* Require at least one of the targeted fields to be "higher than" the given value,
* with no limit as to how high it can be.
* <p>
* This method will apply DSL converters to {@code value} before Hibernate Search attempts to interpret it as a field value.
* See {@link DslConverter#ENABLED}.
*
* @param value The lower bound of the range. Must not be null.
* The signature of this method defines this parameter as an {@link Object},
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateFactoryContext.html#commonconcepts-parametertype">there</a> for more information.
*
* Lower bound is included by default in the range.
*
* @return A context allowing to exclude the lower bound from the range, to set options or to get the resulting predicate.
*/
default RangePredicateLimitTerminalContext above(Object value) {
return above( value, DslConverter.ENABLED );
}

/**
* Require at least one of the targeted fields to be "higher than" the given value,
Expand All @@ -93,12 +140,34 @@ default RangePredicateFieldSetContext orRawField(String absoluteFieldPath) {
* The signature of this method defines this parameter as an {@link Object},
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateFactoryContext.html#commonconcepts-parametertype">there</a> for more information.
* @param dslConverter Controls how the {@code value} should be converted before Hibernate Search attempts to interpret it as a field value.
* See {@link DslConverter}.
*
* Lower bound is included by default in the range.
*
* @return A context allowing to exclude the lower bound from the range, to set options or to get the resulting predicate.
*/
RangePredicateLimitTerminalContext above(Object value);
RangePredicateLimitTerminalContext above(Object value, DslConverter dslConverter);

/**
* Require at least one of the targeted fields to be "lower than" the given value,
* with no limit as to how low it can be.
* <p>
* This method will apply DSL converters to {@code value} before Hibernate Search attempts to interpret it as a field value.
* See {@link DslConverter#ENABLED}.
*
* @param value The upper bound of the range. Must not be null.
* The signature of this method defines this parameter as an {@link Object},
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateFactoryContext.html#commonconcepts-parametertype">there</a> for more information.
*
* Upper bound is included by default in the range.
*
* @return A context allowing to exclude the upper bound from the range, to set options or to get the resulting predicate.
*/
default RangePredicateLimitTerminalContext below(Object value) {
return below( value, DslConverter.ENABLED );
}

/**
* Require at least one of the targeted fields to be "lower than" the given value,
Expand All @@ -108,11 +177,13 @@ default RangePredicateFieldSetContext orRawField(String absoluteFieldPath) {
* The signature of this method defines this parameter as an {@link Object},
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateFactoryContext.html#commonconcepts-parametertype">there</a> for more information.
* @param dslConverter Controls how the {@code value} should be converted before Hibernate Search attempts to interpret it as a field value.
* See {@link DslConverter}.
*
* Upper bound is included by default in the range.
*
* @return A context allowing to exclude the upper bound from the range, to set options or to get the resulting predicate.
*/
RangePredicateLimitTerminalContext below(Object value);
RangePredicateLimitTerminalContext below(Object value, DslConverter dslConverter);

}
Expand Up @@ -6,12 +6,37 @@
*/
package org.hibernate.search.engine.search.dsl.predicate;

import org.hibernate.search.engine.search.predicate.DslConverter;

/**
* The context used when defining a range predicate,
* after the lower bound was provided but before the upper bound was provided.
*/
public interface RangePredicateFromContext {

/**
* Require at least one of the targeted fields to be "lower than" the given value,
* in addition to being "higher than" the value provided to the
* former <code>{@link RangePredicateFieldSetContext#from(Object) from}</code> call.
* <p>
* This method will apply DSL converters to {@code value} before Hibernate Search attempts to interpret it as a field value.
* See {@link DslConverter#ENABLED}.
*
* @param value The upper bound of the range. May be null, in which case the range has no upper bound,
* but this is only possible if the lower bound ({@link RangePredicateFieldSetContext#from(Object)})
* was not null.
* The signature of this method defines this parameter as an {@link Object},
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateFactoryContext.html#commonconcepts-parametertype">there</a> for more information.
*
* Upper bound is included by default in the range.
*
* @return A context allowing to exclude the upper bound from the range, to set options or to get the resulting predicate.
*/
default RangePredicateLimitTerminalContext to(Object value) {
return to( value, DslConverter.ENABLED );
}

/**
* Require at least one of the targeted fields to be "lower than" the given value,
* in addition to being "higher than" the value provided to the
Expand All @@ -23,12 +48,14 @@ public interface RangePredicateFromContext {
* The signature of this method defines this parameter as an {@link Object},
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateFactoryContext.html#commonconcepts-parametertype">there</a> for more information.
* @param dslConverter Controls how the {@code value} should be converted before Hibernate Search attempts to interpret it as a field value.
* See {@link DslConverter}.
*
* Upper bound is included by default in the range.
*
* @return A context allowing to exclude the upper bound from the range, to set options or to get the resulting predicate.
*/
RangePredicateLimitTerminalContext to(Object value);
RangePredicateLimitTerminalContext to(Object value, DslConverter dslConverter);

/**
* Exclude the lower bound from the range.
Expand Down
Expand Up @@ -63,18 +63,18 @@ public RangePredicateFieldSetContext boostedTo(float boost) {
}

@Override
public RangePredicateFromContext from(Object value) {
return commonState.from( value );
public RangePredicateFromContext from(Object value, DslConverter dslConverter) {
return commonState.from( value, dslConverter );
}

@Override
public RangePredicateLimitTerminalContext above(Object value) {
return commonState.above( value );
public RangePredicateLimitTerminalContext above(Object value, DslConverter dslConverter) {
return commonState.above( value, dslConverter );
}

@Override
public RangePredicateLimitTerminalContext below(Object value) {
return commonState.below( value );
public RangePredicateLimitTerminalContext below(Object value, DslConverter dslConverter) {
return commonState.below( value, dslConverter );
}

@Override
Expand Down Expand Up @@ -118,36 +118,36 @@ protected B toImplementation() {
return super.toImplementation();
}

RangePredicateFromContext from(Object value) {
doAbove( value );
RangePredicateFromContext from(Object value, DslConverter dslConverter) {
doAbove( value, dslConverter );
return new RangePredicateFromContextImpl<>( this );
}

RangePredicateLimitTerminalContext above(Object value) {
doAbove( value );
RangePredicateLimitTerminalContext above(Object value, DslConverter dslConverter) {
doAbove( value, dslConverter );
checkHasNonNullBound();
return this;
}

RangePredicateLimitTerminalContext below(Object value) {
doBelow( value );
RangePredicateLimitTerminalContext below(Object value, DslConverter dslConverter) {
doBelow( value, dslConverter );
checkHasNonNullBound();
return this;
}

private void doAbove(Object value) {
private void doAbove(Object value, DslConverter dslConverter) {
excludeUpperLimit = false;
if ( value != null ) {
hasNonNullBound = true;
getQueryBuilders().forEach( q -> q.lowerLimit( value ) );
getQueryBuilders().forEach( q -> q.lowerLimit( value, dslConverter ) );
}
}

private void doBelow(Object value) {
private void doBelow(Object value, DslConverter dslConverter) {
excludeUpperLimit = true;
if ( value != null ) {
hasNonNullBound = true;
getQueryBuilders().forEach( q -> q.upperLimit( value ) );
getQueryBuilders().forEach( q -> q.upperLimit( value, dslConverter ) );
}
}

Expand Down Expand Up @@ -176,8 +176,8 @@ static class RangePredicateFromContextImpl<B> implements RangePredicateFromConte
}

@Override
public RangePredicateLimitTerminalContext to(Object value) {
delegate.doBelow( value );
public RangePredicateLimitTerminalContext to(Object value, DslConverter dslConverter) {
delegate.doBelow( value, dslConverter );
delegate.checkHasNonNullBound();
return delegate;
}
Expand Down
Expand Up @@ -6,13 +6,15 @@
*/
package org.hibernate.search.engine.search.predicate.spi;

import org.hibernate.search.engine.search.predicate.DslConverter;

public interface RangePredicateBuilder<B> extends SearchPredicateBuilder<B> {

void lowerLimit(Object value);
void lowerLimit(Object value, DslConverter dslConverter);

void excludeLowerLimit();

void upperLimit(Object value);
void upperLimit(Object value, DslConverter dslConverter);

void excludeUpperLimit();

Expand Down
Expand Up @@ -89,7 +89,7 @@ public void value(Object value, DslConverter dslConverter) {
}

@Override
public void lowerLimit(Object value) {
public void lowerLimit(Object value, DslConverter dslConverter) {
// No-op
}

Expand All @@ -99,7 +99,7 @@ public void excludeLowerLimit() {
}

@Override
public void upperLimit(Object value) {
public void upperLimit(Object value, DslConverter dslConverter) {
// No-op
}

Expand Down

0 comments on commit 312ceb0

Please sign in to comment.