Skip to content

Commit

Permalink
HSEARCH-3290 Replace end() calls with toPredicate()/toSort() in the p…
Browse files Browse the repository at this point in the history
…redicate and sort DSLs
  • Loading branch information
yrodiere authored and gsmet committed Nov 2, 2018
1 parent 35cc0d8 commit b67c74b
Show file tree
Hide file tree
Showing 25 changed files with 111 additions and 129 deletions.
Expand Up @@ -46,7 +46,7 @@ default MatchPredicateFieldSetContext orField(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="SearchPredicateContainerContext.html#commonconcepts-parametertype">there</a> for more information.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
SearchPredicateTerminalContext matching(Object value);

Expand Down
Expand Up @@ -24,7 +24,7 @@ public interface NestedPredicateFieldContext {
* matches the inner predicate.
*
* @param searchPredicate The predicate that must be matched by at least one element of the nested object field.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
SearchPredicateTerminalContext nest(SearchPredicate searchPredicate);

Expand All @@ -45,7 +45,7 @@ public interface NestedPredicateFieldContext {
*
* @param predicateContributor A consumer that will add a predicate to the context passed in parameter.
* Should generally be a lambda expression.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
SearchPredicateTerminalContext nest(Consumer<? super SearchPredicateContainerContext> predicateContributor);

Expand Down
Expand Up @@ -81,7 +81,7 @@ default RangePredicateFromContext from(Object value) {
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateContainerContext.html#commonconcepts-parametertype">there</a> for more information.
* @param inclusion Whether the lower bound should be included in or excluded from the range.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
SearchPredicateTerminalContext above(Object value, RangeBoundInclusion inclusion);

Expand All @@ -94,7 +94,7 @@ default RangePredicateFromContext from(Object value) {
*
* @param value The lower bound of the range (included)
* (see {@link #above(Object, RangeBoundInclusion)} for details about null-ness and type).
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*
* @see #above(Object, RangeBoundInclusion)
*/
Expand All @@ -111,7 +111,7 @@ default SearchPredicateTerminalContext above(Object value) {
* but a specific type is expected depending on the targeted field.
* See <a href="SearchPredicateContainerContext.html#commonconcepts-parametertype">there</a> for more information.
* @param inclusion Whether the upper bound should be included in or excluded from the range.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
SearchPredicateTerminalContext below(Object value, RangeBoundInclusion inclusion);

Expand All @@ -124,7 +124,7 @@ default SearchPredicateTerminalContext above(Object value) {
*
* @param value The upper bound of the range (included)
* (see {@link #below(Object, RangeBoundInclusion)} for details about null-ness and type).
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*
* @see #below(Object, RangeBoundInclusion)
*/
Expand Down
Expand Up @@ -58,7 +58,7 @@ public interface SearchPredicateContainerContext {
* Match all documents.
*
* @return A context allowing to define the predicate more precisely
* and ultimately {@link SearchPredicateTerminalContext#end() end the predicate definition}.
* and ultimately {@link SearchPredicateTerminalContext#toPredicate() get the resulting predicate}.
* @see MatchAllPredicateContext
*/
MatchAllPredicateContext matchAll();
Expand All @@ -67,7 +67,7 @@ public interface SearchPredicateContainerContext {
* Match documents if they match a combination of boolean clauses.
*
* @return A context allowing to define the predicate more precisely
* and ultimately {@link SearchPredicateTerminalContext#end() end the predicate definition}.
* and ultimately {@link SearchPredicateTerminalContext#toPredicate() get the resulting predicate}.
* @see BooleanJunctionPredicateContext
*/
BooleanJunctionPredicateContext bool();
Expand All @@ -80,7 +80,7 @@ public interface SearchPredicateContainerContext {
*
* @param clauseContributor A consumer that will add clauses to the context passed in parameter.
* Should generally be a lambda expression.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
* @see BooleanJunctionPredicateContext
*/
SearchPredicateTerminalContext bool(Consumer<? super BooleanJunctionPredicateContext> clauseContributor);
Expand All @@ -93,7 +93,7 @@ public interface SearchPredicateContainerContext {
* while analyzed, full-text fields imply approximate matches depending on how they are analyzed.
*
* @return A context allowing to define the predicate more precisely
* and ultimately {@link SearchPredicateTerminalContext#end() end the predicate definition}.
* and ultimately {@link SearchPredicateTerminalContext#toPredicate() get the resulting predicate}.
* @see MatchPredicateContext
*/
MatchPredicateContext match();
Expand All @@ -102,7 +102,7 @@ public interface SearchPredicateContainerContext {
* Match documents where targeted fields have a value within lower and upper bounds.
*
* @return A context allowing to define the predicate more precisely
* and ultimately {@link SearchPredicateTerminalContext#end() end the predicate definition}.
* and ultimately {@link SearchPredicateTerminalContext#toPredicate() get the resulting predicate}.
* @see RangePredicateContext
*/
RangePredicateContext range();
Expand All @@ -113,7 +113,7 @@ public interface SearchPredicateContainerContext {
* matches a given predicate.
*
* @return A context allowing to define the predicate more precisely
* and ultimately {@link SearchPredicateTerminalContext#end() end the predicate definition}.
* and ultimately {@link SearchPredicateTerminalContext#toPredicate() get the resulting predicate}.
* @see NestedPredicateContext
*/
NestedPredicateContext nested();
Expand Down
Expand Up @@ -14,15 +14,6 @@
*/
public interface SearchPredicateTerminalContext {

/**
* End the current context and get the resulting {@link SearchPredicate} object.
*
* @return The {@link SearchPredicate} resulting from the previous DSL steps.
*/
default SearchPredicate end() {
return toPredicate();
}

/**
* Create a {@link SearchPredicate} instance
* matching the definition given in the previous DSL steps.
Expand Down
Expand Up @@ -17,7 +17,7 @@ public interface SpatialPredicateContext {
* a circle (maximum distance matching), a polygon, a bounding box, ...
*
* @return A context allowing to define the predicate more precisely
* and ultimately {@link SearchPredicateTerminalContext#end() end the predicate definition}.
* and ultimately {@link SearchPredicateTerminalContext#toPredicate() get the resulting predicate}.
*/
SpatialWithinPredicateContext within();

Expand Down
Expand Up @@ -52,7 +52,7 @@ default SpatialWithinPredicateFieldSetContext orField(String absoluteFieldPath)
* @param center The center of the bounding circle.
* @param radius The radius of the bounding circle, in the unit defined by parameter {@code unit}.
* @param unit The unit used for the radius.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
SearchPredicateTerminalContext circle(GeoPoint center, double radius, DistanceUnit unit);

Expand All @@ -62,7 +62,7 @@ default SpatialWithinPredicateFieldSetContext orField(String absoluteFieldPath)
*
* @param center The center of the bounding circle.
* @param radiusInMeters The radius of the bounding circle, in meters.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
default SearchPredicateTerminalContext circle(GeoPoint center, double radiusInMeters) {
return circle( center, radiusInMeters, DistanceUnit.METERS );
Expand All @@ -76,7 +76,7 @@ default SearchPredicateTerminalContext circle(GeoPoint center, double radiusInMe
* @param longitude The longitude of the center of the bounding circle.
* @param radius The radius of the bounding circle, in the unit defined by parameter {@code unit}.
* @param unit The unit used for the radius.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
default SearchPredicateTerminalContext circle(double latitude, double longitude, double radius, DistanceUnit unit) {
return circle( GeoPoint.of( latitude, longitude ), radius, unit );
Expand All @@ -89,7 +89,7 @@ default SearchPredicateTerminalContext circle(double latitude, double longitude,
* @param latitude The latitude of the center of the bounding circle.
* @param longitude The longitude of the center of the bounding circle.
* @param radiusInMeters The radius of the bounding circle, in meters.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
default SearchPredicateTerminalContext circle(double latitude, double longitude, double radiusInMeters) {
return circle( GeoPoint.of( latitude, longitude ), radiusInMeters, DistanceUnit.METERS );
Expand All @@ -99,15 +99,15 @@ default SearchPredicateTerminalContext circle(double latitude, double longitude,
* Require at least one of the targeted fields to point to a location within the given polygon.
*
* @param polygon The bounding polygon.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
SearchPredicateTerminalContext polygon(GeoPolygon polygon);

/**
* Require at least one of the targeted fields to point to a location within the given box (~rectangle).
*
* @param boundingBox The bounding box.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
SearchPredicateTerminalContext boundingBox(GeoBoundingBox boundingBox);

Expand All @@ -118,7 +118,7 @@ default SearchPredicateTerminalContext circle(double latitude, double longitude,
* @param topLeftLongitude The longitude of the top-left corner of the box.
* @param bottomRightLatitude The latitude of the bottom-right corner of the box.
* @param bottomRightLongitude The longitude of the bottom-right corner of the box.
* @return A context allowing to end the predicate definition.
* @return A context allowing to get the resulting predicate.
*/
default SearchPredicateTerminalContext boundingBox(double topLeftLatitude, double topLeftLongitude, double bottomRightLatitude,
double bottomRightLongitude) {
Expand Down
Expand Up @@ -24,15 +24,15 @@ public interface SearchSortContainerContext {
* The default order is <strong>descending</strong>, i.e. higher scores come first.
*
* @return A context allowing to define the sort more precisely, {@link NonEmptySortContext#then() chain other sorts}
* or {@link SearchSortTerminalContext#end() end the sort definition}.
* or {@link SearchSortTerminalContext#toSort() get the resulting sort}.
*/
ScoreSortContext byScore();

/**
* Order elements by their internal index order.
*
* @return A context allowing to {@link NonEmptySortContext#then() chain other sorts}
* or {@link SearchSortTerminalContext#end() end the sort definition}.
* or {@link SearchSortTerminalContext#toSort() get the resulting sort}.
*/
NonEmptySortContext byIndexOrder();

Expand All @@ -43,7 +43,7 @@ public interface SearchSortContainerContext {
*
* @param absoluteFieldPath The absolute path of the index field to sort by
* @return A context allowing to define the sort more precisely, {@link NonEmptySortContext#then() chain other sorts}
* or {@link SearchSortTerminalContext#end() end the sort definition}.
* or {@link SearchSortTerminalContext#toSort() get the resulting sort}.
* @throws SearchException If the sort field type could not be automatically determined.
*/
FieldSortContext byField(String absoluteFieldPath);
Expand All @@ -56,7 +56,7 @@ public interface SearchSortContainerContext {
* @param absoluteFieldPath The absolute path of the indexed location field to sort by.
* @param location The location to which we want to compute the distance.
* @return A context allowing to define the sort more precisely, {@link NonEmptySortContext#then() chain other sorts}
* or {@link SearchSortTerminalContext#end() end the sort definition}.
* or {@link SearchSortTerminalContext#toSort() get the resulting sort}.
* @throws SearchException If the field type does not constitute a valid location.
*/
DistanceSortContext byDistance(String absoluteFieldPath, GeoPoint location);
Expand All @@ -70,7 +70,7 @@ public interface SearchSortContainerContext {
* @param latitude The latitude of the location to which we want to compute the distance.
* @param longitude The longitude of the location to which we want to compute the distance.
* @return A context allowing to define the sort more precisely, {@link NonEmptySortContext#then() chain other sorts}
* or {@link SearchSortTerminalContext#end() end the sort definition}.
* or {@link SearchSortTerminalContext#toSort() get the resulting sort}.
* @throws SearchException If the field type does not constitute a valid location.
*/
default DistanceSortContext byDistance(String absoluteFieldPath, double latitude, double longitude) {
Expand All @@ -85,7 +85,7 @@ default DistanceSortContext byDistance(String absoluteFieldPath, double latitude
*
* @param sort A previously-built {@link SearchSort} object.
* @return A context allowing to {@link NonEmptySortContext#then() chain other sorts}
* or {@link SearchSortTerminalContext#end() end the sort definition}.
* or {@link SearchSortTerminalContext#toSort() get the resulting sort}.
*/
NonEmptySortContext by(SearchSort sort);

Expand Down
Expand Up @@ -14,15 +14,6 @@
*/
public interface SearchSortTerminalContext {

/**
* End the current context and get the resulting {@link SearchSort} object.
*
* @return The {@link SearchSort} resulting from the previous DSL steps.
*/
default SearchSort end() {
return toSort();
}

/**
* Create a {@link SearchSort} instance
* matching the definition given in the previous DSL steps.
Expand Down
Expand Up @@ -116,9 +116,9 @@ public void predicate_fromJsonString_separatePredicate() {
IndexSearchTarget searchTarget = indexManager.createSearchTarget().build();

SearchPredicate predicate1 = searchTarget.predicate().extension( ElasticsearchExtension.get() )
.fromJsonString( "{'match': {'string': 'text 1'}}" ).end();
.fromJsonString( "{'match': {'string': 'text 1'}}" ).toPredicate();
SearchPredicate predicate2 = searchTarget.predicate().extension( ElasticsearchExtension.get() )
.fromJsonString( "{'match': {'integer': 2}}" ).end();
.fromJsonString( "{'match': {'integer': 2}}" ).toPredicate();
SearchPredicate predicate3 = searchTarget.predicate().extension( ElasticsearchExtension.get() )
.fromJsonString(
"{"
Expand All @@ -131,16 +131,16 @@ public void predicate_fromJsonString_separatePredicate() {
+ "}"
+ "}"
)
.end();
.toPredicate();
// Also test using the standard DSL on a field defined with the extension
SearchPredicate predicate4 = searchTarget.predicate().match().onField( "yearDays" )
.matching( "'2018:12'" ).end();
.matching( "'2018:12'" ).toPredicate();
SearchPredicate booleanPredicate = searchTarget.predicate().bool( b -> {
b.should( predicate1 );
b.should( predicate2 );
b.should( predicate3 );
b.should( predicate4 );
} ).end();
} ).toPredicate();

SearchQuery<DocumentReference> query = searchTarget.query( sessionContext )
.asReferences()
Expand Down Expand Up @@ -201,18 +201,18 @@ public void sort_fromJsonString_separateSort() {

SearchSort sort1Asc = searchTarget.sort().extension( ElasticsearchExtension.get() )
.fromJsonString( "{'sort1': 'asc'}" )
.end();
.toSort();
SearchSort sort2Asc = searchTarget.sort().extension( ElasticsearchExtension.get() )
.fromJsonString( "{'sort2': 'asc'}" )
.end();
.toSort();
SearchSort sort3Asc = searchTarget.sort().extension( ElasticsearchExtension.get() )
.fromJsonString( "{'sort3': 'asc'}" )
.end();
.toSort();
// Also test using the standard DSL on a field defined with the extension
SearchSort sort4Asc = searchTarget.sort()
.byField( "sort4" ).asc().onMissingValue().sortLast()
.then().byField( "sort5" ).asc().onMissingValue().sortFirst()
.end();
.toSort();

SearchQuery<DocumentReference> query = searchTarget.query( sessionContext )
.asReferences()
Expand All @@ -224,17 +224,17 @@ public void sort_fromJsonString_separateSort() {

SearchSort sort1Desc = searchTarget.sort().extension( ElasticsearchExtension.get() )
.fromJsonString( "{'sort1': 'desc'}" )
.end();
.toSort();
SearchSort sort2Desc = searchTarget.sort().extension( ElasticsearchExtension.get() )
.fromJsonString( "{'sort2': 'desc'}" )
.end();
.toSort();
SearchSort sort3Desc = searchTarget.sort().extension( ElasticsearchExtension.get() )
.fromJsonString( "{'sort3': 'desc'}" )
.end();
.toSort();
SearchSort sort4Desc = searchTarget.sort()
.byField( "sort4" ).desc().onMissingValue().sortLast()
.then().byField( "sort5" ).asc().onMissingValue().sortFirst()
.end();
.toSort();

query = searchTarget.query( sessionContext )
.asReferences()
Expand Down

0 comments on commit b67c74b

Please sign in to comment.