/** * 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="#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() ); }
@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 ); }
@Test public void should_separatePredicateObject() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchPredicate predicate1 = searchTarget.predicate().match().onField( "field1" ).matching( FIELD1_VALUE1 ).toPredicate(); SearchPredicate predicate2 = searchTarget.predicate().match().onField( "field1" ).matching( FIELD1_VALUE3 ).toPredicate(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( predicate1 ) .should( predicate2 ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1, DOCUMENT_3 ); }
@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 should() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1 ); query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field1" ).matching( FIELD1_VALUE2 ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_1, DOCUMENT_2 ); }
@Test public void boost() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.spatial().within().onField( "geoPoint" ).boundingBox( CHEZ_MARGOTTE_BOUNDING_BOX ) ) .should( f.match().onField( "string" ).boostedTo( 42 ).matching( OURSON_QUI_BOIT_STRING ) ) .toPredicate() ) .sort( c -> c.byScore() ) .build(); assertThat( query ) .hasDocRefHitsExactOrder( INDEX_NAME, OURSON_QUI_BOIT_ID, CHEZ_MARGOTTE_ID ); query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.spatial().within().onField( "geoPoint" ).boostedTo( 42 ).boundingBox( CHEZ_MARGOTTE_BOUNDING_BOX ) ) .should( f.match().onField( "string" ).matching( OURSON_QUI_BOIT_STRING ) ) .toPredicate() ) .sort( c -> c.byScore() ) .build(); assertThat( query ) .hasDocRefHitsExactOrder( INDEX_NAME, CHEZ_MARGOTTE_ID, OURSON_QUI_BOIT_ID ); }
@Test public void boost() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.spatial().within().onField( "geoPoint" ).polygon( CHEZ_MARGOTTE_POLYGON ) ) .should( f.match().onField( "string" ).boostedTo( 42 ).matching( OURSON_QUI_BOIT_STRING ) ) .toPredicate() ) .sort( c -> c.byScore() ) .build(); assertThat( query ) .hasDocRefHitsExactOrder( INDEX_NAME, OURSON_QUI_BOIT_ID, CHEZ_MARGOTTE_ID ); query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.spatial().within().onField( "geoPoint" ).boostedTo( 42 ).polygon( CHEZ_MARGOTTE_POLYGON ) ) .should( f.match().onField( "string" ).matching( OURSON_QUI_BOIT_STRING ) ) .toPredicate() ) .sort( c -> c.byScore() ) .build(); assertThat( query ) .hasDocRefHitsExactOrder( INDEX_NAME, CHEZ_MARGOTTE_ID, OURSON_QUI_BOIT_ID ); }
.predicate( f -> f.bool() .minimumShouldMatchNumber( -1 ) .should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE3 ) ) .toPredicate() .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() .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( b -> { b.minimumShouldMatch( minimumShouldMatchConstraints ); b.should( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ); } ) ) .build(); .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(); .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();
@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 must_should() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); // A boolean predicate with must + 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() .must( 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() .must( f.match().onField( "field1" ).matching( FIELD1_VALUE2 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE1 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, DOCUMENT_2 ); }
@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( root -> root.bool() .should( c -> c.range().onField( indexMapping.string1Field.relativeFieldName ) .above( indexMapping.string1Field.document3Value.indexedValue ).toPredicate() .should( c -> c.range().onField( indexMapping.string1Field.relativeFieldName ).boostedTo( 42 ) .below( indexMapping.string1Field.document1Value.indexedValue ).toPredicate() .asReference() .predicate( root -> root.bool() .should( c -> c.range().onField( indexMapping.string1Field.relativeFieldName ).boostedTo( 42 ) .above( indexMapping.string1Field.document3Value.indexedValue ).toPredicate() .should( c -> c.range().onField( indexMapping.string1Field.relativeFieldName ) .below( indexMapping.string1Field.document1Value.indexedValue ).toPredicate()
.asReference() .predicate( root -> root.bool() .should( c -> c.match().onField( indexMapping.string1Field.relativeFieldName ) .matching( indexMapping.string1Field.document1Value.indexedValue ).toPredicate() .should( c -> c.match().onField( indexMapping.string1Field.relativeFieldName ).boostedTo( 42 ) .matching( indexMapping.string1Field.document3Value.indexedValue ).toPredicate() .asReference() .predicate( root -> root.bool() .should( c -> c.match().onField( indexMapping.string1Field.relativeFieldName ).boostedTo( 42 ) .matching( indexMapping.string1Field.document1Value.indexedValue ).toPredicate() .should( c -> c.match().onField( indexMapping.string1Field.relativeFieldName ) .matching( indexMapping.string1Field.document3Value.indexedValue ).toPredicate()
.asReference() .predicate( f -> f.bool() .should( f.spatial().within() .onField( "geoPoint" ) .circle( METRO_GARIBALDI, 400 ) .should( f.match() .onField( "string" ).boostedTo( 42 ) .matching( OURSON_QUI_BOIT_STRING ) .asReference() .predicate( f -> f.bool() .should( f.spatial().within() .onField( "geoPoint" ).boostedTo( 42 ) .circle( METRO_GARIBALDI, 400 ) .should( f.match() .onField( "string" ) .matching( OURSON_QUI_BOIT_STRING )
.mustNot( f.match().onField( "field1" ).matching( FIELD1_VALUE2 ) ) .mustNot( f.match().onField( "field1" ).matching( FIELD1_VALUE3 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE2 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate() .mustNot( f.match().onField( "field1" ).matching( FIELD1_VALUE1 ) ) .mustNot( f.match().onField( "field1" ).matching( FIELD1_VALUE3 ) ) .should( f.match().onField( "field2" ).matching( FIELD2_VALUE2 ) ) .should( f.match().onField( "field3" ).matching( FIELD3_VALUE3 ) ) .toPredicate()