/** * 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() ); }
@Override public SearchPredicate bool(Consumer<? super BooleanJunctionPredicateContext> clauseContributor) { BooleanJunctionPredicateContext context = bool(); clauseContributor.accept( context ); return context.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() ); }
.predicate( f -> f.bool() .must( f.nested().onObjectField( "nestedObject.nestedObject" ) .nest( f.bool() .must( f.match() .onField( "nestedObject.nestedObject.field1" ) .matching( MATCHING_SECOND_LEVEL_CONDITION1_FIELD1 ) .must( f.match() .onField( "nestedObject.nestedObject.field2" ) .matching( MATCHING_SECOND_LEVEL_CONDITION1_FIELD2 ) .must( f.nested().onObjectField( "nestedObject.nestedObject" ) .nest( f.bool() .must( f.match() .onField( "nestedObject.nestedObject.field1" ) .matching( MATCHING_SECOND_LEVEL_CONDITION2_FIELD1 ) .must( f.match() .onField( "nestedObject.nestedObject.field2" ) .matching( MATCHING_SECOND_LEVEL_CONDITION2_FIELD2 ) .toPredicate()
@Test public void nested() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .must( f.bool() .should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field1" ).matching( FIELD1_VALUE3 ) ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1, DOCUMENT_3 ); query = searchTarget.query() .asReference() .predicate( f -> f.bool() .must( f.bool() .should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field1" ).matching( FIELD1_VALUE3 ) ) ) .mustNot( f.match().onField( "field1" ).matching( FIELD1_VALUE3 ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1 ); }
@Test public void should_function() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f2 -> f2.match().onField( "field1" ).matching( FIELD1_VALUE1 ).toPredicate() ) .should( f2 -> f2.match().onField( "field1" ).matching( FIELD1_VALUE2 ).toPredicate() ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1, DOCUMENT_2 ); }
.asReference() .predicate( f -> f.bool() .minimumShouldMatchPercent( -50 ) .should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE3 ) ) .toPredicate() .asReference() .predicate( f -> f.bool() .must( f.match().onField( "field4" ).matching( FIELD4_VALUE1AND2 ) ) .minimumShouldMatchPercent( -50 ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE2 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate() .asReference() .predicate( f -> f.bool() .minimumShouldMatchPercent( -40 ) // The minimum should be rounded up to 2 .should( f.match().onField( "field4" ).matching( FIELD4_VALUE1AND2 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE1 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate()
.asReference() .predicate( f -> f.bool() .minimumShouldMatchNumber( -1 ) .should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE3 ) ) .toPredicate() .asReference() .predicate( f -> f.bool() .must( f.match().onField( "field4" ).matching( FIELD4_VALUE1AND2 ) ) .minimumShouldMatchNumber( -1 ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE2 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate() .asReference() .predicate( f -> f.bool() .minimumShouldMatchNumber( -1 ) .should( f.match().onField( "field4" ).matching( FIELD4_VALUE1AND2 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE1 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate()
.predicate( f -> f.bool().must( cachingContributor ).toPredicate() ) .build(); .asReference() .predicate( f -> f.bool() .should( cachingContributor ) .should( f.match().onField( "string" ).matching( STRING_2 ) ) .toPredicate()
@Test public void must_mustNot() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .must( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .mustNot( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .toPredicate() ) .build(); assertThat( query ).hasNoHits(); query = searchTarget.query() .asReference() .predicate( f -> f.bool() .must( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .mustNot( f.match().onField( "field1" ).matching( FIELD1_VALUE2 ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1 ); }
@Test public void should_mustNot() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field1" ).matching( FIELD1_VALUE3 ) ) .mustNot( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_3 ); }
@Test public void filter_should() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); // A boolean predicate with filter + should clauses: // documents should match regardless of whether should clauses match. // Non-matching "should" clauses SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .filter( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE2 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1 ); // One matching and one non-matching "should" clause query = searchTarget.query() .asReference() .predicate( f -> f.bool() .filter( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE1 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1 ); }
.asReference() .predicate( f -> f.bool( b -> { b.minimumShouldMatch( minimumShouldMatchConstraints ); b.must( f.match().onField( "field4" ).matching( FIELD4_VALUE1AND2 ) ); } ) ) .build(); .asReference() .predicate( f -> f.bool( b -> { b.minimumShouldMatch( minimumShouldMatchConstraints ); b.should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ); } ) ) .build(); .asReference() .predicate( f -> f.bool( b -> { b.minimumShouldMatch( minimumShouldMatchConstraints ); b.should( f.match().onField( "field4" ).matching( FIELD4_VALUE1AND2 ) ); b.should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ); } ) ) .build(); .asReference() .predicate( f -> f.bool( b -> { b.minimumShouldMatch( minimumShouldMatchConstraints ); b.should( f.match().onField( "field4" ).matching( FIELD4_VALUE1AND2 ) ); b.should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ); b.should( f.match().onField( "field2" ).matching( FIELD2_VALUE3 ) ); } ) )
@Test public void mustNot_function() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .mustNot( f2 -> f2.match().onField( "field1" ).matching( FIELD1_VALUE1 ).toPredicate() ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_2, DOCUMENT_3 ); }
.asReference() .predicate( f -> f.bool( b -> { b.minimumShouldMatch( minimumShouldMatchConstraints ); b.should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ); } ) ) .build(); .asReference() .predicate( f -> f.bool( b -> { b.minimumShouldMatch( minimumShouldMatchConstraints ); b.should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ); b.should( f.match().onField( "field2" ).matching( FIELD2_VALUE2 ) ); } ) ) .build(); .asReference() .predicate( f -> f.bool( b -> { b.minimumShouldMatch( minimumShouldMatchConstraints ); b.should( f.match().onField( "field4" ).matching( FIELD4_VALUE1AND2 ) ); b.should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ); b.should( f.match().onField( "field2" ).matching( FIELD2_VALUE3 ) ); } ) ) .build();
/** * Add a default <a href="MinimumShouldMatchContext.html#minimumshouldmatch">"minimumShouldMatch" constraint</a>. * * @param matchingClausesNumber A definition of the number of "should" clauses that have to match. * If positive, it is the number of clauses that have to match. * See <a href="MinimumShouldMatchContext.html#minimumshouldmatch-minimum">Definition of the minimum</a> * for details and possible values, in particular negative values. * @return {@code this}, for method chaining. */ default BooleanJunctionPredicateContext minimumShouldMatchNumber(int matchingClausesNumber) { return minimumShouldMatch() .ifMoreThan( 0 ).thenRequireNumber( matchingClausesNumber ) .end(); }
/** * 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="#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() ); }
@Test public void must_function() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .must( f2 -> f2.match().onField( "field1" ).matching( FIELD1_VALUE1 ).toPredicate() ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1 ); }
.asReference() .predicate( f -> f.bool() .should( f.match().onField( "integer" ).matching( 1 ) ) .should( f.match().onField( "integer" ).matching( 2 ) ) .toPredicate() .asReference() .predicate( f -> f.bool() .must( f.match().onField( "string_analyzed" ).matching( "text" ) ) .filter( f.match().onField( "integer" ).matching( 1 ) ) .toPredicate() .asReference() .predicate( f -> f.bool() .must( f.match().onField( "string_analyzed" ).matching( "text" ) ) .mustNot( f.match().onField( "integer" ).matching( 2 ) ) .toPredicate()