@Test public void testRangeQueryFromTo() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); calendar.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); calendar.set( 1900, 2, 12, 0, 0, 0 ); calendar.set( Calendar.MILLISECOND, 0 ); Date from = calendar.getTime(); calendar.set( 1910, 2, 12, 0, 0, 0 ); Date to = calendar.getTime(); Query query = monthQb .range() .onField( "estimatedCreation" ) .andField( "justfortest" ) .ignoreFieldBridge().ignoreAnalyzer() .from( from ) .to( to ).excludeLimit() .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
@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 public void testIndexingWithNullEncodingFieldBridge() { Sample entity = new Sample(); entity.id = 1; entity.description = "null date"; entity.deletionDate = null; // should trigger the marker token helper.add( entity ); Query termQuery = NumericFieldUtils.createExactMatchQuery( "deletionDate", Long.parseLong( "-1" ) ); helper.assertThat( termQuery ) .from( Sample.class ) .hasResultSize( 1 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2199") public void testCharFilters() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //regular query Query query = monthQb.keyword().onField( "htmlDescription" ).matching( "strong" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 2 ); query = monthQb.keyword().onField( "htmlDescription" ).matching( "em" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 2 ); //using the HTMLStripCharFilter query = monthQb.keyword().onField( "htmlDescription_htmlStrip" ).matching( "strong" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 0 ); query = monthQb.keyword().onField( "htmlDescription_htmlStrip" ).matching( "em" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 0 ); query = monthQb.keyword().onField( "htmlDescription_htmlStrip" ).matching( "month" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 3 ); query = monthQb.keyword().onField( "htmlDescription_htmlStrip" ).matching( "spring" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); query = monthQb.keyword().onField( "htmlDescription_htmlStrip" ).matching( "fake" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); query = monthQb.keyword().onField( "htmlDescription_htmlStrip" ).matching( "escaped" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
@Test public void testAddingClassSimpleAPI() throws Exception { searchIntegrator = integratorResource.create( new SearchConfigurationForTest() ); searchIntegrator.addClasses( A.class ); helper.index( new A( 1, "Emmanuel" ), 1 ); helper.assertThat( "name", "emmanuel" ) .from( A.class ) .hasResultSize( 1 ); searchIntegrator.addClasses( B.class, C.class ); helper.index() .push( new B( 1, "Noel" ), 1 ) .push( new C( 1, "Vincent" ), 1 ) .execute(); helper.assertThat( "name", "noel" ) .from( B.class ) .hasResultSize( 1 ); helper.assertThat( "name", "vincent" ) .from( C.class ) .hasResultSize( 1 ); }
@Test public void testTermQueryOnAnalyzer() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //regular term query Query query = monthQb.keyword().onField( "mythology" ).matching( "cold" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 0 ); //term query based on several words query = monthQb.keyword().onField( "mythology" ).matching( "colder darker" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); //term query applying the analyzer and generating one term per word query = monthQb.keyword().onField( "mythology_stem" ).matching( "snowboard" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); //term query applying the analyzer and generating several terms per word query = monthQb.keyword().onField( "mythology_ngram" ).matching( "snobored" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); //term query not using analyzers query = monthQb.keyword().onField( "mythology" ).ignoreAnalyzer().matching( "Month" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 0 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2785") public void testTermQueryOnNormalizer() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb.keyword().onField( "name" ).matching( "February" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); query = monthQb.keyword().onField( "name" ).matching( "february" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); query = monthQb.keyword().onField( "mythology_normalized" ).matching( "Month of fake spring" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); query = monthQb.keyword().onField( "mythology_normalized" ).matching( "month of fake spring" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); query = monthQb.keyword().onField( "mythology_normalized" ).matching( "Month" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 0 ); query = monthQb.keyword().onField( "mythology_normalized" ).matching( "month" ).createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 0 ); }
@Test public void testIndexAndSearch() { A a = new A(); a.id = 0L; a.foo = "someValue"; B b = new B(); b.a = a; b.id = 1L; C c = new C(); c.b = b; c.id = 2L; helper.add( c ); helper.assertThat( "b.a.foo", a.foo ) .from( C.class ) .hasResultSize( 1 ); }
@Test public void testUseOfMultipleCustomFieldBridgeInstances() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //Rather complex code here as we're not exposing the #withFieldBridge methods on the public interface final ConnectedTermMatchingContext field1Context = (ConnectedTermMatchingContext) monthQb .keyword() .onField( MonthClassBridge.FIELD_NAME_1 ); final ConnectedTermMatchingContext field2Context = (ConnectedTermMatchingContext) field1Context .withFieldBridge( new String2FieldBridgeAdaptor( new RomanNumberFieldBridge() ) ) .andField( MonthClassBridge.FIELD_NAME_2 ); Query query = field2Context .withFieldBridge( new String2FieldBridgeAdaptor( new RomanNumberFieldBridge() ) ) .matching( 2 ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
private void assertSearchMatches(String field) throws ParseException { helper.assertThat( field, "foo" ) .from( Book.class ) .hasResultSize( 0 ); helper.assertThat( field, "lucene" ) .from( Book.class ) .hasResultSize( 1 ); helper.assertThat( field, "null" ) .from( Book.class ) .hasResultSize( 1 ); helper.assertThat() .from( Book.class ) .hasResultSize( 2 ); }
private void assertDocumentsIndexed(int number) { helper.assertThat().from( Quote.class ).hasResultSize( number ); }
@Test public void testFuzzyQuery() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //fuzzy search with custom threshold and prefix Query query = monthQb .keyword() .fuzzy() .withEditDistanceUpTo( 1 ) .withPrefixLength( 1 ) .onField( "mythology" ) .matching( "calder" ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
@Test public void testAddingClassFullModel() throws Exception { searchIntegrator = integratorResource.create( new SearchConfigurationForTest() ); searchIntegrator = integratorResource.create( searchIntegrator, A.class ); helper.index( new A( 1, "Emmanuel" ), 1 ); helper.assertThat( "name", "emmanuel" ) .from( A.class ) .hasResultSize( 1 ); searchIntegrator = integratorResource.create( searchIntegrator, B.class ); helper.index( new B( 1, "Noel" ), 1 ); helper.assertThat( "name", "noel" ) .from( B.class ) .hasResultSize( 1 ); }
private void assertCount(Class<?> entityType, int count) { helper.assertThat().from( entityType ).hasResultSize( count ); }
@Test public void testSortOnNullableNumericFieldArray() throws Exception { helper.index( new Person( 1, 25, "name1", 1, 2, 3 ), new Person( 2, 22, "name2", 1, null, 3 ), new Person( 3, 23, "name3", null, null, null ) ); Query rangeQuery = queryForRangeOnFieldSorted( 0, 2, "array" ); Sort sortAsInt = new Sort( new SortField( "array", SortField.Type.INT ) ); helper.assertThat( rangeQuery ) .from( Person.class ) .sort( sortAsInt ) .hasResultSize( 2 ); }
@Test public void testFuzzyQueryOnMultipleFields() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .keyword() .fuzzy() .withEditDistanceUpTo( 2 ) .withPrefixLength( 1 ) .onFields( "mythology", "history" ) .matching( "showboarding" ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 2 ); }
@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 ); }
@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 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 ); }
@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 ); }