private HSQuery queryAll() { QueryBuilder qb = helper.queryBuilder( IndexedEntity.class ); return helper.hsQuery( IndexedEntity.class ) .sort( qb.sort().byField( "idSort" ).createSort() ); }
@Test @SuppressWarnings("unchecked") public void testResultOrderedByDateDescending() throws Exception { EntityTransaction tx = em.getTransaction(); tx.begin(); QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity( ProductArticle.class ).get(); org.apache.lucene.search.Query query = builder.keyword().wildcard().onField( "header" ).matching( "hib*" ).createQuery(); Sort dateDescending = builder.sort().byField( "creationDate" ).desc().createSort(); List<ProductArticle> result = em.createFullTextQuery( query, ProductArticle.class ) .setSort( dateDescending ).setFirstResult( 3 ).getResultList(); assertThat( result ).as( "query result" ).hasSize( 3 ); assertThat( result.get( 0 ).getArticleId() ).as( "article id" ).isEqualTo( 3 ); assertThat( result.get( 1 ).getArticleId() ).as( "article id" ).isEqualTo( 2 ); assertThat( result.get( 2 ).getArticleId() ).as( "article id" ).isEqualTo( 1 ); tx.commit(); em.clear(); }
@Test(expected = SearchException.class) public void singleField_stringFieldBridge_missingValue_use() throws Exception { builder().sort() .byField( "fieldBridgedStringField" ) .onMissingValue().use( "1.5" ) .createSort(); }
@Test public void singleField_stringFieldBridge() throws Exception { Sort sort = builder().sort() .byField( "fieldBridgedStringField" ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byField( "fieldBridgedStringField" ) .asc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byField( "fieldBridgedStringField" ) .desc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 1, 2 ); }
@Test public void singleField_numericFieldBridge_nonMetadataProviding() throws Exception { // Missing value is not provided; the missing values should be considered as 0 Sort sort = builder().sort() .byField( "nonMetadataProvidingFieldBridgedNumericField", SortField.Type.DOUBLE ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byField( "nonMetadataProvidingFieldBridgedNumericField", SortField.Type.DOUBLE ) .asc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byField( "nonMetadataProvidingFieldBridgedNumericField", SortField.Type.DOUBLE ) .desc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 1, 2 ); }
@Test public void docID() throws Exception { Sort sort = builder().sort() .byIndexOrder() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 0, 1, 2, 3 ); sort = builder().sort() .byIndexOrder() .asc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 0, 1, 2, 3 ); sort = builder().sort() .byIndexOrder() .desc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 2, 1, 0 ); }
@Test public void singleField() throws Exception { // Missing value is not provided; the missing values should be considered as 0 Sort sort = builder().sort() .byField( "uniqueDoubleField" ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byField( "uniqueDoubleField" ) .asc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byField( "uniqueDoubleField" ) .desc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 1, 2 ); }
@Test public void nativeLucene() throws Exception { // Missing value is not provided; the missing values should be considered as 0 Sort sort = builder().sort() .byNative( new SortField( "uniqueDoubleField", SortField.Type.DOUBLE ) ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byNative( new SortField( "uniqueDoubleField", SortField.Type.DOUBLE, false ) ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byNative( new SortField( "uniqueDoubleField", SortField.Type.DOUBLE, true ) ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 1, 2 ); }
@Test public void singleField_numericFieldBridge() throws Exception { // Missing value is not provided; the missing values should be considered as 0 Sort sort = builder().sort() .byField( "fieldBridgedNumericField" ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byField( "fieldBridgedNumericField" ) .asc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 1, 0, 3 ); sort = builder().sort() .byField( "fieldBridgedNumericField" ) .desc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 1, 2 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2587") public void embeddedField() throws Exception { // Missing value is not provided; the missing values should be considered as 0 Sort sort = builder().sort() .byField( "previous.uniqueDoubleField" ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 2, 1, 0 ); sort = builder().sort() .byField( "previous.uniqueDoubleField" ) .asc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 2, 1, 0 ); sort = builder().sort() .byField( "previous.uniqueDoubleField" ) .desc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 0, 1, 2, 3 ); }
@Test public void multipleFields() throws Exception { Sort sort = builder().sort() .byField( "nonUniqueIntegerField" ) .andByField( "uniqueDoubleField" ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 0, 3, 1 ); sort = builder().sort() .byField( "nonUniqueIntegerField" ) .andByField( "uniqueDoubleField" ) .asc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 2, 0, 3, 1 ); sort = builder().sort() .byField( "nonUniqueIntegerField" ) .andByField( "uniqueDoubleField" ) .desc() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 2, 1 ); }
@Test public void singleField_numericFieldBridge_nonMetadataProviding_missingValue_use() throws Exception { Sort sort = builder().sort() .byField( "nonMetadataProvidingFieldBridgedNumericField", SortField.Type.DOUBLE ) .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 2, 0, 3 ); sort = builder().sort() .byField( "nonMetadataProvidingFieldBridgedNumericField", SortField.Type.DOUBLE ) .asc() .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 2, 0, 3 ); sort = builder().sort() .byField( "nonMetadataProvidingFieldBridgedNumericField", SortField.Type.DOUBLE ) .desc() .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 2, 1 ); }
@Test public void singleField_double_missingValue_use() throws Exception { Sort sort = builder().sort() .byField( "uniqueDoubleField" ) .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 2, 0, 3 ); sort = builder().sort() .byField( "uniqueDoubleField" ) .asc() .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 2, 0, 3 ); sort = builder().sort() .byField( "uniqueDoubleField" ) .desc() .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 2, 1 ); }
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 public void singleField_integer_missingValue_use() throws Exception { Sort sort = builder().sort() .byField( "uniqueIntegerField" ) .onMissingValue().use( 2 ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 2, 0, 3 ); sort = builder().sort() .byField( "uniqueIntegerField" ) .asc() .onMissingValue().use( 2 ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 2, 0, 3 ); sort = builder().sort() .byField( "uniqueIntegerField" ) .desc() .onMissingValue().use( 2 ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 2, 1 ); }
@Test public void singleField_numericFieldBridge_missingValue_use() throws Exception { Sort sort = builder().sort() .byField( "fieldBridgedNumericField" ) .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 2, 0, 3 ); sort = builder().sort() .byField( "fieldBridgedNumericField" ) .asc() .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 2, 0, 3 ); sort = builder().sort() .byField( "fieldBridgedNumericField" ) .desc() .onMissingValue().use( 1.5d ) .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 2, 1 ); }
@Test public void singleField_missingValue_sortLast() throws Exception { Sort sort = builder().sort() .byField( "uniqueDoubleField" ) .onMissingValue().sortLast() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 0, 3, 2 ); sort = builder().sort() .byField( "uniqueDoubleField" ) .asc() .onMissingValue().sortLast() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 1, 0, 3, 2 ); sort = builder().sort() .byField( "uniqueDoubleField" ) .desc() .onMissingValue().sortLast() .createSort(); assertQueryAll( sort ).matchesExactlyIds( 3, 0, 1, 2 ); }
@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(expected = ClassCastException.class) public void singleField_numericFieldBridge_missingValue_use_nonRaw() throws Exception { Sort sort = builder().sort() .byField( "fieldBridgedNumericField" ) .onMissingValue().use( new WrappedDoubleValue( 1.5d ) ) .createSort(); sfHolder.getSearchFactory().createHSQuery( new MatchAllDocsQuery(), IndexedEntry.class ) .sort( sort ) .queryEntityInfos(); }
@Test(expected = ClassCastException.class) public void singleField_numericFieldBridge_nonMetadataProviding_missingValue_use_nonRaw() throws Exception { Query query = builder().all().createQuery(); Sort sort = builder().sort() .byField( "nonMetadataProvidingFieldBridgedNumericField", SortField.Type.DOUBLE ) .onMissingValue().use( new WrappedDoubleValue( 1.5d ) ) .createSort(); sfHolder.getSearchFactory().createHSQuery( query, IndexedEntry.class ) .sort( sort ) .queryEntityInfos(); }