Skip to content

Commit

Permalink
HSEARCH-3290 Allow to omit the .toPredicate() call for predicates ins…
Browse files Browse the repository at this point in the history
…ide other predicates
  • Loading branch information
yrodiere authored and gsmet committed Nov 2, 2018
1 parent 95ec563 commit 8f4e04f
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 215 deletions.
Expand Up @@ -97,6 +97,51 @@ public interface BooleanJunctionPredicateContext extends
*/
BooleanJunctionPredicateContext filter(SearchPredicate searchPredicate);

/*
* Syntactic sugar allowing to skip the toPredicate() call by passing a SearchPredicateTerminalContext
* directly.
*/

/**
* Add a <a href="#must">"must" clause</a> based on an almost-built {@link SearchPredicate}.
*
* @param terminalContext The terminal context allowing to retrieve a {@link SearchPredicate}.
* @return {@code this}, for method chaining.
*/
default BooleanJunctionPredicateContext must(SearchPredicateTerminalContext terminalContext) {
return must( terminalContext.toPredicate() );
}

/**
* Add a <a href="#mustnot">"must not" clause</a> based on an almost-built {@link SearchPredicate}.
*
* @param terminalContext The terminal context allowing to retrieve a {@link SearchPredicate}.
* @return {@code this}, for method chaining.
*/
default BooleanJunctionPredicateContext mustNot(SearchPredicateTerminalContext terminalContext) {
return mustNot( terminalContext.toPredicate() );
}

/**
* Add a <a href="#should">"should" clause</a> based on an almost-built {@link SearchPredicate}.
*
* @param terminalContext The terminal context allowing to retrieve a {@link SearchPredicate}.
* @return {@code this}, for method chaining.
*/
default BooleanJunctionPredicateContext should(SearchPredicateTerminalContext terminalContext) {
return should( terminalContext.toPredicate() );
}

/**
* Add a <a href="#filter">"filter" clause</a> based on an almost-built {@link SearchPredicate}.
*
* @param terminalContext The terminal context allowing to retrieve a {@link SearchPredicate}.
* @return {@code this}, for method chaining.
*/
default BooleanJunctionPredicateContext filter(SearchPredicateTerminalContext terminalContext) {
return filter( terminalContext.toPredicate() );
}

/*
* Alternative syntax taking advantage of lambdas,
* allowing the structure of the predicate building code to mirror the structure of predicates,
Expand Down
Expand Up @@ -26,6 +26,22 @@ public interface MatchAllPredicateContext extends SearchPredicateNoFieldContext<
*/
MatchAllPredicateContext except(SearchPredicate searchPredicate);

/*
* Syntactic sugar allowing to skip the toPredicate() call by passing a SearchPredicateTerminalContext
* directly.
*/

/**
* Add a "must not" clause based on an almost-built {@link SearchPredicate}.
* <p>
* Documents matching the "must not" clause won't match the "match all" predicate.
*
* @param terminalContext The terminal context allowing to retrieve a {@link SearchPredicate}.
* @return A context allowing to get the resulting predicate.
*/
default MatchAllPredicateContext except(SearchPredicateTerminalContext terminalContext) {
return except( terminalContext.toPredicate() );
}
/*
* Alternative syntax taking advantage of lambdas,
* allowing the structure of the predicate building code to mirror the structure of predicates,
Expand Down
Expand Up @@ -28,6 +28,24 @@ public interface NestedPredicateFieldContext {
*/
SearchPredicateTerminalContext nest(SearchPredicate searchPredicate);

/*
* Syntactic sugar allowing to skip the toPredicate() call by passing a SearchPredicateTerminalContext
* directly.
*/

/**
* Set the inner predicate to an almost-built {@link SearchPredicate}.
* <p>
* Matching documents are those for which at least one element of the nested object field
* matches the inner predicate.
*
* @param terminalContext The terminal context allowing to retrieve a {@link SearchPredicate}.
* @return A context allowing to get the resulting predicate.
*/
default SearchPredicateTerminalContext nest(SearchPredicateTerminalContext terminalContext) {
return nest( terminalContext.toPredicate() );
}

/*
* Alternative syntax taking advantage of lambdas,
* allowing the structure of the predicate building code to mirror the structure of predicates,
Expand Down
Expand Up @@ -85,11 +85,9 @@ public void predicate_fromJsonString() {
.predicate( f -> f.bool()
.should( f.extension( ElasticsearchExtension.get() )
.fromJsonString( "{'match': {'string': 'text 1'}}" )
.toPredicate()
)
.should( f.extension( ElasticsearchExtension.get() )
.fromJsonString( "{'match': {'integer': 2}}" )
.toPredicate()
)
.should( f.extension( ElasticsearchExtension.get() )
.fromJsonString(
Expand All @@ -103,10 +101,9 @@ public void predicate_fromJsonString() {
+ "}"
+ "}"
)
.toPredicate()
)
// Also test using the standard DSL on a field defined with the extension
.should( f.match().onField( "yearDays" ).matching( "'2018:12'" ).toPredicate() )
.should( f.match().onField( "yearDays" ).matching( "'2018:12'" ) )
.toPredicate()
)
.build();
Expand Down
Expand Up @@ -100,15 +100,12 @@ public void predicate_fromLuceneQuery() {
.predicate( f -> f.bool()
.should( f.extension( LuceneExtension.get() )
.fromLuceneQuery( new TermQuery( new Term( "string", "text 1" ) ) )
.toPredicate()
)
.should( f.extension( LuceneExtension.get() )
.fromLuceneQuery( IntPoint.newExactQuery( "integer", 2 ) )
.toPredicate()
)
.should( f.extension( LuceneExtension.get() )
.fromLuceneQuery( LatLonPoint.newDistanceQuery( "geoPoint", 40, -70, 200_000 ) )
.toPredicate()
)
.toPredicate()
)
Expand Down
Expand Up @@ -119,7 +119,6 @@ public void search_on_nested_object_only_returns_elements_of_the_tenant() {
.predicate( f -> f.nested().onObjectField( "nestedObject" )
.nest( f.match()
.onField( "nestedObject.string" ).matching( STRING_VALUE_1 )
.toPredicate()
)
.toPredicate()
)
Expand All @@ -134,7 +133,6 @@ public void search_on_nested_object_only_returns_elements_of_the_tenant() {
.predicate( f -> f.nested().onObjectField( "nestedObject" )
.nest( f.match()
.onField( "nestedObject.string" ).matching( STRING_VALUE_1 )
.toPredicate()
)
.toPredicate()
)
Expand Down Expand Up @@ -233,7 +231,6 @@ public void update_only_updates_elements_of_the_tenant() {
.predicate( f -> f.nested().onObjectField( "nestedObject" )
.nest( f.match()
.onField( "nestedObject.string" ).matching( UPDATED_STRING )
.toPredicate()
)
.toPredicate()
)
Expand All @@ -259,7 +256,6 @@ public void update_only_updates_elements_of_the_tenant() {
.predicate( f -> f.nested().onObjectField( "nestedObject" )
.nest( f.match()
.onField( "nestedObject.string" ).matching( UPDATED_STRING )
.toPredicate()
)
.toPredicate()
)
Expand All @@ -283,7 +279,6 @@ public void update_only_updates_elements_of_the_tenant() {
.predicate( f -> f.nested().onObjectField( "nestedObject" )
.nest( f.match()
.onField( "nestedObject.string" ).matching( STRING_VALUE_1 )
.toPredicate()
)
.toPredicate()
)
Expand Down
Expand Up @@ -125,10 +125,10 @@ public void search_match() {
SearchQuery<DocumentReference> query = searchTarget.query( sessionContext )
.asReferences()
.predicate( f -> f.bool()
.must( f.match().onField( "flattenedObject.string" ).matching( MATCHING_STRING ).toPredicate() )
.must( f.match().onField( "flattenedObject.string_analyzed" ).matching( MATCHING_STRING_ANALYZED ).toPredicate() )
.must( f.match().onField( "flattenedObject.integer" ).matching( MATCHING_INTEGER ).toPredicate() )
.must( f.match().onField( "flattenedObject.localDate" ).matching( MATCHING_LOCAL_DATE ).toPredicate() )
.must( f.match().onField( "flattenedObject.string" ).matching( MATCHING_STRING ) )
.must( f.match().onField( "flattenedObject.string_analyzed" ).matching( MATCHING_STRING_ANALYZED ) )
.must( f.match().onField( "flattenedObject.integer" ).matching( MATCHING_INTEGER ) )
.must( f.match().onField( "flattenedObject.localDate" ).matching( MATCHING_LOCAL_DATE ) )
.toPredicate()
)
.build();
Expand All @@ -140,11 +140,10 @@ public void search_match() {
.asReferences()
.predicate( f -> f.nested().onObjectField( "nestedObject" )
.nest( f.bool()
.must( f.match().onField( "nestedObject.string" ).matching( MATCHING_STRING ).toPredicate() )
.must( f.match().onField( "nestedObject.string_analyzed" ).matching( MATCHING_STRING_ANALYZED ).toPredicate() )
.must( f.match().onField( "nestedObject.integer" ).matching( MATCHING_INTEGER ).toPredicate() )
.must( f.match().onField( "nestedObject.localDate" ).matching( MATCHING_LOCAL_DATE ).toPredicate() )
.toPredicate()
.must( f.match().onField( "nestedObject.string" ).matching( MATCHING_STRING ) )
.must( f.match().onField( "nestedObject.string_analyzed" ).matching( MATCHING_STRING_ANALYZED ) )
.must( f.match().onField( "nestedObject.integer" ).matching( MATCHING_INTEGER ) )
.must( f.match().onField( "nestedObject.localDate" ).matching( MATCHING_LOCAL_DATE ) )
)
.toPredicate()
)
Expand All @@ -163,15 +162,12 @@ public void search_range() {
.predicate( f -> f.bool()
.must( f.range().onField( "flattenedObject.string" )
.from( MATCHING_STRING ).to( MATCHING_STRING )
.toPredicate()
)
.must( f.range().onField( "flattenedObject.integer" )
.from( MATCHING_INTEGER - 1 ).to( MATCHING_INTEGER + 1 )
.toPredicate()
)
.must( f.range().onField( "flattenedObject.localDate" )
.from( MATCHING_LOCAL_DATE.minusDays( 1 ) ).to( MATCHING_LOCAL_DATE.plusDays( 1 ) )
.toPredicate()
)
.toPredicate()
)
Expand All @@ -186,18 +182,14 @@ public void search_range() {
.nest( f.bool()
.must( f.range().onField( "nestedObject.string" )
.from( MATCHING_STRING ).to( MATCHING_STRING )
.toPredicate()
)
.must( f.range().onField( "nestedObject.integer" )
.from( MATCHING_INTEGER - 1 ).to( MATCHING_INTEGER + 1 )
.toPredicate()
)
.must( f.range().onField( "nestedObject.localDate" )
.from( MATCHING_LOCAL_DATE.minusDays( 1 ) )
.to( MATCHING_LOCAL_DATE.plusDays( 1 ) )
.toPredicate()
)
.toPredicate()
)
.toPredicate()
)
Expand Down
Expand Up @@ -160,8 +160,8 @@ public void predicate_boolean() {
SearchQuery<DocumentReference> query = searchTarget.query( sessionContext )
.asReferences()
.predicate( f -> f.bool()
.should( f.match().onField( "integer" ).matching( 1 ).toPredicate() )
.should( f.match().onField( "integer" ).matching( 2 ).toPredicate() )
.should( f.match().onField( "integer" ).matching( 1 ) )
.should( f.match().onField( "integer" ).matching( 2 ) )
.toPredicate()
)
.build();
Expand All @@ -172,8 +172,8 @@ public void predicate_boolean() {
query = searchTarget.query( sessionContext )
.asReferences()
.predicate( f -> f.bool()
.must( f.match().onField( "string_analyzed" ).matching( "text" ).toPredicate() )
.filter( f.match().onField( "integer" ).matching( 1 ).toPredicate() )
.must( f.match().onField( "string_analyzed" ).matching( "text" ) )
.filter( f.match().onField( "integer" ).matching( 1 ) )
.toPredicate()
)
.build();
Expand All @@ -184,8 +184,8 @@ public void predicate_boolean() {
query = searchTarget.query( sessionContext )
.asReferences()
.predicate( f -> f.bool()
.must( f.match().onField( "string_analyzed" ).matching( "text" ).toPredicate() )
.mustNot( f.match().onField( "integer" ).matching( 2 ).toPredicate() )
.must( f.match().onField( "string_analyzed" ).matching( "text" ) )
.mustNot( f.match().onField( "integer" ).matching( 2 ) )
.toPredicate()
)
.build();
Expand All @@ -202,8 +202,8 @@ public void predicate_nested() {
SearchQuery<DocumentReference> query = searchTarget.query( sessionContext )
.asReferences()
.predicate( f -> f.bool()
.must( f.match().onField( "flattenedObject.string" ).matching( "text 1_2" ).toPredicate() )
.must( f.match().onField( "flattenedObject.integer" ).matching( 101 ).toPredicate() )
.must( f.match().onField( "flattenedObject.string" ).matching( "text 1_2" ) )
.must( f.match().onField( "flattenedObject.integer" ).matching( 101 ) )
.toPredicate()
)
.build();
Expand All @@ -225,9 +225,8 @@ public void predicate_nested() {
.asReferences()
.predicate( f -> f.nested().onObjectField( "nestedObject" )
.nest( f.bool()
.must( f.match().onField( "nestedObject.string" ).matching( "text 1_2" ).toPredicate() )
.must( f.match().onField( "nestedObject.integer" ).matching( 101 ).toPredicate() )
.toPredicate()
.must( f.match().onField( "nestedObject.string" ).matching( "text 1_2" ) )
.must( f.match().onField( "nestedObject.integer" ).matching( 101 ) )
)
.toPredicate()
)
Expand All @@ -241,9 +240,8 @@ public void predicate_nested() {
.asReferences()
.predicate( f -> f.nested().onObjectField( "nestedObject" )
.nest( f.bool()
.must( f.match().onField( "nestedObject.string" ).matching( "text 1_1" ).toPredicate() )
.must( f.match().onField( "nestedObject.integer" ).matching( 101 ).toPredicate() )
.toPredicate()
.must( f.match().onField( "nestedObject.string" ).matching( "text 1_1" ) )
.must( f.match().onField( "nestedObject.integer" ).matching( 101 ) )
)
.toPredicate()
)
Expand Down

0 comments on commit 8f4e04f

Please sign in to comment.