From 51e11cd813f62e606fe669b1defd2ce2edf508bf Mon Sep 17 00:00:00 2001
From: Nick <nick@bywatersolutions.com>
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
---
 Koha/SearchEngine/Elasticsearch/QueryBuilder.pm    |  6 +++++
 .../Koha/SearchEngine/Elasticsearch/QueryBuilder.t | 28 +++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
index 3e63086730..1639f1311f 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 fd983498aa..9810e4d2b8 100644
--- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
+++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
@@ -192,7 +192,7 @@ subtest 'build_authorities_query_compat() tests' => sub {
 };
 
 subtest 'build_query tests' => sub {
-    plan tests => 40;
+    plan tests => 41;
 
     my $qb;
 
@@ -214,11 +214,37 @@ subtest 'build_query tests' => sub {
             'title__sort.phrase' => {
                     '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');
     $query = $qb->build_query('test', %options);
     ok( defined $query->{aggregations}{ccode}{terms}{size},'we need to ask for a size or we get only 5 facet' );
-- 
2.11.0