@Test @TestForIssue( jiraKey = "HSEARCH-703" ) public void testObtainingBuilderForUnindexedTypeWithoutIndexedSubTypesCausesException() { try { helper.queryBuilder( Animal.class ); fail( "Obtaining a builder not allowed for unindexed type without any indexed sub-types." ); } catch (SearchException e) { // success } }
@Test public void testNumericFieldsTermQuery() { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb.keyword() .onField( "raindropInMm" ) .matching( 0.231d ) .createQuery(); assertTrue( query.getClass().isAssignableFrom( NumericRangeQuery.class ) ); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-1791") public void testUsingMatchQueryOnNumericDocumentIdGeneratesTermQuery() throws Exception { // making sure a string based TermQuery is used final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb.keyword() .onField( "id" ) .matching( 1 ) .createQuery(); assertTrue( "A string based TermQuery is expected, but got a " + query.getClass(), query instanceof TermQuery ); }
private HSQuery queryAll() { QueryBuilder qb = helper.queryBuilder( IndexedEntity.class ); return helper.hsQuery( IndexedEntity.class ) .sort( qb.sort().byField( "idSort" ).createSort() ); }
@Test public void testNumericRangeQueries() { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .range() .onField( "raindropInMm" ) .from( 0.23d ) .to( 0.24d ) .createQuery(); assertTrue( query.getClass().isAssignableFrom( NumericRangeQuery.class ) ); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1 ); }
@Test(expected = SearchException.class) public void testIllegalBooleanJunction() { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //forgetting to set any condition on the boolean, an exception shall be thrown: BooleanJunction<?> booleanJunction = monthQb.bool(); assertTrue( booleanJunction.isEmpty() ); booleanJunction.createQuery(); Assert.fail( "should not reach this point" ); }
@Test public void minimumShouldMatch_error_multipleConflictingConstraints() { QueryBuilder queryBuilder = helper.queryBuilder( IndexedEntity.class ); thrown.expect( SearchException.class ); thrown.expectMessage( "Multiple conflicting minimumShouldMatch constraints" ); queryBuilder.bool().minimumShouldMatchNumber( -1 ).minimumShouldMatchPercent( 100 ); }
@Test public void testSearchString() throws Exception { QueryBuilder qb = helper.queryBuilder( ContainerEntity.class ); Query q = qb.keyword().onField( "emb.embeddedField" ).matching( "embedded" ).createQuery(); assertQuery( q ).matchesExactlyIds( 1L ); }
@Test public void testPhraseQueryWithStopWords() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .phrase() .onField( "mythology" ) .sentence( "colder and whitening" ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
@Test @TestForIssue( jiraKey = "HSEARCH-703" ) public void testPolymorphicQueryForUnindexedSuperTypeReturnsIndexedSubType() { final QueryBuilder builder = helper.queryBuilder( Object.class ); Query query = builder.all().createQuery(); helper.assertThat( query ).from( Object.class ) .as( "expected all instances of all indexed types" ) .hasResultSize( 8 ); }
@Test @TestForIssue( jiraKey = "HSEARCH-703" ) public void testPolymorphicQueryWithKeywordTermForUnindexedSuperTypeReturnsIndexedSubType() { final QueryBuilder builder = helper.queryBuilder( Car.class ); Query query = builder.keyword().onField( "name" ).matching( "Morris" ).createQuery(); helper.assertThat( query ).matchesExactlyIds( 2 ); }
@Test public void testUseOfFieldBridge() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb.keyword().onField( "monthValue" ).matching( 2 ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
@Test public void testWildcardQuery() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .keyword() .wildcard() .onField( "mythology" ) .matching( "mon*" ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 3 ); }
private void expectedProjections(NumericRangeQuery<Long> numericRangeQuery, String... expectedProjections) { SearchIntegrator searchFactory = factoryHolder.getSearchFactory(); QueryBuilder queryBuilder = helper.queryBuilder( Staff.class ); HSQuery hsQuery = searchFactory.createHSQuery( numericRangeQuery, Staff.class ) .projection( "name" ) .sort( queryBuilder.sort().byField( "idSort" ).createSort() ); helper.assertThat( hsQuery ) .matchesExactlySingleProjections( expectedProjections ) .hasResultSize( expectedProjections.length ); }
@Test @TestForIssue(jiraKey = "HSEARCH-1811") public void testWildcardQueryOnMultipleFields() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .keyword() .wildcard() .onFields( "mythology", "history" ) .matching( "snowbo*" ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 2, 3 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2479") public void testPhraseQueryTermCreation() throws Exception { String testCaseText = "Test the Test test of your test test to test test test of test and Test budgeting."; final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .phrase() .onField( "mythology" ) .sentence( testCaseText ) .createQuery(); helper.assertThat( query ).from( Month.class ).as( "test term ordering" ).hasResultSize( 0 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2656") public void testNumericQueryWithFieldTypeOverriddenByFieldBridge() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .keyword() .onField( "monthBase0" ) .ignoreFieldBridge().ignoreAnalyzer() .matching( 0 ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesUnorderedIds( 1 ); }
@Test public void clear_sort() throws Exception { QueryBuilder qb = helper.queryBuilder( IndexedEntity.class ); HSQuery hsQuery = queryAll(); helper.assertThat( hsQuery ).matchesExactlyIds( 0, 1, 2 ); hsQuery.sort( qb.sort().byField( "idSort" ).desc().createSort() ); helper.assertThat( hsQuery ).matchesExactlyIds( 2, 1, 0 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2656") public void testNumericRangeQueryWithFieldTypeOverriddenByFieldBridge() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .range() .onField( "monthBase0" ) .ignoreFieldBridge().ignoreAnalyzer() .below( 1 ).excludeLimit() .createQuery(); helper.assertThat( query ).from( Month.class ).matchesUnorderedIds( 1 ); }
@Test public void testUseOfCustomFieldBridgeInstance() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); ConnectedTermMatchingContext termMatchingContext = (ConnectedTermMatchingContext) monthQb .keyword() .onField( MonthClassBridge.FIELD_NAME_1 ); Query query = termMatchingContext .withFieldBridge( new String2FieldBridgeAdaptor( new RomanNumberFieldBridge() ) ) .matching( 2 ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }