From 372c9e48ce039afa630a583822b02b07bf7adc89 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 22 Oct 2019 14:38:58 +0000 Subject: [PATCH] Bug 23875: Explicitly srt searches by score and provide local-number as tie breaker To test: 1 - Set search engine to ES 2 - Search the catalog for "*" 3 - Note results 4 - Edit first result 5 - Repeat search, results are reordered 6 - Apply patch 7 - Search for "*" 8 - Highest biblionumbers are returned first 9 - Edit first result, repeat search 10 - Order does not change 11 - Confirm sorting by author, title, etc passes the same trials Signed-off-by: Ere Maijala --- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 6 +++++ .../Koha/SearchEngine/Elasticsearch/QueryBuilder.t | 28 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 8f3aea9445..4bc33dd145 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -114,6 +114,12 @@ sub build_query { push @{ $res->{sort} }, { $f => { order => $d } }; } } + # If not sorting by anything explicitly search by score + if (!defined $res->{sort}){ + push @{ $res->{sort} }, { _score => { order => 'desc' } }; + } + # Add a tie breaker in case of equally relevant records + push @{ $res->{sort} }, { 'local-number.raw' => { order => 'desc' } }; # See _convert_facets in Search.pm for how these get turned into # things that Koha can use. diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index 1266da5ffe..cb9d98e0f7 100644 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -216,7 +216,7 @@ subtest 'build_authorities_query_compat() tests' => sub { }; subtest 'build_query tests' => sub { - plan tests => 53; + plan tests => 54; my $qb; @@ -238,11 +238,35 @@ subtest 'build_query tests' => sub { 'title__sort' => { 'order' => 'asc' } - } + }, + { + 'local-number.raw' => { + 'order' => 'desc' + } + } ], "sort parameter properly formed" ); + $query = $qb->build_query('test', () ); + + is_deeply( + $query->{sort}, + [ + { + '_score' => { + 'order' => 'desc' + } + }, + { + 'local-number.raw' => { + 'order' => 'desc' + } + } + ], + "sort parameter properly formed if no sort passed" + ); + t::lib::Mocks::mock_preference('FacetMaxCount','37'); t::lib::Mocks::mock_preference('DisplayLibraryFacets','both'); $query = $qb->build_query('test', %options); -- 2.11.0