@Test public void testMoreLikeThisExcludingEntityBeingCompared() { Query mltQuery; List<Object[]> results; QueryBuilder qb = getCoffeeQueryBuilder(); // exclude comparing entity mltQuery = qb .moreLikeThis() .comparingField( "summary" ).boostedTo( 10f ) .andField( "description" ) .toEntityWithId( decaffInstance.getId() ) .createQuery(); results = doQuery( mltQuery ); mltQuery = qb .moreLikeThis() .excludeEntityUsedForComparison() .comparingField( "summary" ).boostedTo( 10f ) .andField( "description" ) .toEntityWithId( decaffInstance.getId() ) .createQuery(); List<Object[]> resultsWoComparingEntity = doQuery( mltQuery ); assertThat( resultsWoComparingEntity ).hasSize( results.size() - 1 ); for ( int index = 0; index < resultsWoComparingEntity.size(); index++ ) { Object[] real = resultsWoComparingEntity.get( index ); Object[] expected = results.get( index + 1 ); assertThat( real[1] ).isEqualTo( expected[1] ); assertThat( real[0] ).isEqualTo( expected[0] ); } outputQueryAndResults( decaffInstance, mltQuery, resultsWoComparingEntity ); }
@Test public void testMoreLikeThisOnCompressedFields() { Query mltQuery; List<Object[]> entityResults; QueryBuilder qb = getCoffeeQueryBuilder(); // using compressed field mltQuery = qb .moreLikeThis() .comparingField( "brand.description" ) .toEntityWithId( decaffInstance.getId() ) .createQuery(); entityResults = doQuery( mltQuery ); assertThat( entityResults ).hasSize( 3 ); float score = -1; for ( Object[] element : entityResults ) { if ( score == -1 ) { score = (Float) element[1]; } assertThat( element[1] ).as( "All scores should be equal as the same brand is used" ) .isEqualTo( score ); } outputQueryAndResults( decaffInstance, mltQuery, entityResults ); }
.moreLikeThis() .comparingField( "brand.name" ) .toEntityWithId( decaffInstance.getId() ) .moreLikeThis() .comparingField( "brand.id" ) .toEntityWithId( decaffInstance.getId() )
.moreLikeThis() .comparingField( "summary" ).boostedTo( 10f ) .andField( "description" ) .moreLikeThis() .comparingField( "summary" ).boostedTo( 10f ) .andField( "description" ) copyOfDecaffInstance.setSummary( nonMatchingOne.getSummary() ); mltQuery = qb .moreLikeThis() .comparingField( "summary" ).boostedTo( 10f ) .andField( "description" ) copyOfDecaffInstance.setSummary( decaffInstance.getSummary() ); mltQuery = qb .moreLikeThis() .comparingField( "summary" ).boostedTo( 10f ) .andField( "description" )
@Test @TestForIssue(jiraKey = "HSEARCH-1614") public void testMoreLikeThisOnUnknownFieldThrowsException() { QueryBuilder queryBuilder = getCoffeeQueryBuilder(); try { queryBuilder.moreLikeThis() .comparingField( "foo" ) .toEntityWithId( decaffInstance.getId() ) .createQuery(); fail( "Creating the query should have failed" ); } catch (SearchException e) { assertTrue( "Unexpected error message: " + e.getMessage(), e.getMessage().startsWith( "HSEARCH000218" ) ); } }
QueryBuilder qb = getCoffeeQueryBuilder(); Query mltQuery = qb .moreLikeThis() .favorSignificantTermsWithFactor( 1 ) .comparingAllFields() .moreLikeThis() .comparingField( "summary" ).boostedTo( 10f ) .andField( "description" ) .moreLikeThis() .comparingField( "summary" ) .andField( "internalDescription" )