Skip to content

Commit

Permalink
HSEARCH-3886 Expect documents with a missing value to be sorted last …
Browse files Browse the repository at this point in the history
…by default in field sorts
  • Loading branch information
yrodiere authored and fax4ever committed Apr 8, 2020
1 parent ad452ec commit 985caf8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
3 changes: 1 addition & 2 deletions documentation/src/main/asciidoc/search-dsl-sort.asciidoc
Expand Up @@ -239,8 +239,7 @@ include::{sourcedir}/org/hibernate/search/documentation/search/sort/SortDslIT.ja
// Search 5 anchors backward compatibility
[[_handling_missing_values]]

Documents that do not have any value for a sort field will be assigned a default value
depending on the sort and field type (`0`, an empty string, ...).
Documents that do not have any value for a sort field will appear in the last position by default.

The behavior for missing values can be controlled explicitly through the `.missing()` option:

Expand Down
Expand Up @@ -219,19 +219,39 @@ public void sumWithTemporalField() {
}

@Test
public void missingValue() {
@TestForIssue(jiraKey = "HSEARCH-3886")
public void missingValue_default() {
assumeTestParametersWork();

SearchQuery<DocumentReference> query;

String fieldPathForAscendingOrderTests = getFieldPath( SortOrder.ASC );
String fieldPathForDescendingOrderTests = getFieldPath( SortOrder.DESC );

// Default order
query = matchNonEmptyAndEmpty1Query( f -> f.field( fieldPathForAscendingOrderTests ).missing().last() );
// Default for missing values is last, regardless of the order

query = matchNonEmptyAndEmpty1Query( f -> f.field( fieldPathForAscendingOrderTests ) );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, DOCUMENT_1, DOCUMENT_2, DOCUMENT_3, EMPTY_1 );

query = matchNonEmptyAndEmpty1Query( f -> f.field( fieldPathForAscendingOrderTests ).asc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, DOCUMENT_1, DOCUMENT_2, DOCUMENT_3, EMPTY_1 );

query = matchNonEmptyAndEmpty1Query( f -> f.field( fieldPathForDescendingOrderTests ).desc() );
assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, DOCUMENT_3, DOCUMENT_2, DOCUMENT_1, EMPTY_1 );
}

@Test
public void missingValue_explicit() {
assumeTestParametersWork();

SearchQuery<DocumentReference> query;

String fieldPathForAscendingOrderTests = getFieldPath( SortOrder.ASC );
String fieldPathForDescendingOrderTests = getFieldPath( SortOrder.DESC );

// Explicit order with missing().last()
query = matchNonEmptyAndEmpty1Query( f -> f.field( fieldPathForAscendingOrderTests ).asc().missing().last() );
assertThat( query )
Expand Down Expand Up @@ -269,7 +289,7 @@ public void missingValue() {

@Test
@TestForIssue(jiraKey = "HSEARCH-3254")
public void missingValue_multipleEmpty() {
public void missingValue_explicit_multipleEmpty() {
assumeTestParametersWork();

List<DocumentReference> docRefHits;
Expand Down

0 comments on commit 985caf8

Please sign in to comment.