From 8acab3754da7b965720333572907af04f61d89c1 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 27 Apr 2020 15:41:44 -0400
Subject: [PATCH] EDS: Override empty search with a default query. (#1551)

---
 .../VuFindSearch/Backend/EDS/QueryBuilder.php   | 12 ++++++++++++
 .../VuFindTest/Backend/EDS/QueryBuilderTest.php | 17 +++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/QueryBuilder.php b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/QueryBuilder.php
index 79fefed753e..d761481e70f 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/QueryBuilder.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/QueryBuilder.php
@@ -44,6 +44,14 @@ use VuFindSearch\Query\QueryGroup;
  */
 class QueryBuilder
 {
+    /**
+     * Default query (used when query string is empty). This should retrieve all
+     * records in the index, facilitating high-level facet-based browsing.
+     *
+     * @var string
+     */
+    protected $defaultQuery = '(FT yes) OR (FT no)';
+
     /**
      * Constructor
      */
@@ -82,6 +90,10 @@ class QueryBuilder
         $expression = SearchRequestModel::escapeSpecialCharacters($expression);
         $fieldCode = ($query->getHandler() == 'AllFields')
             ? '' : $query->getHandler();  //fieldcode
+        // Special case: default search
+        if (empty($fieldCode) && empty($expression)) {
+            return $this->defaultQuery;
+        }
         if (!empty($fieldCode)) {
             $expression = $fieldCode . ':' . $expression;
         }
diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/EDS/QueryBuilderTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/EDS/QueryBuilderTest.php
index cb9377b2614..13f3d3e9f69 100644
--- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/EDS/QueryBuilderTest.php
+++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/EDS/QueryBuilderTest.php
@@ -42,6 +42,23 @@ use VuFindSearch\Backend\EDS\QueryBuilder;
  */
 class QueryBuilderTest extends TestCase
 {
+    /**
+     * Test special case for blank queries.
+     *
+     * @return void
+     */
+    public function testBlankSearch()
+    {
+        $qb = new QueryBuilder();
+        $params = $qb->build(new \VuFindSearch\Query\Query());
+        $this->assertEquals(
+            [
+                'query' => ['(FT yes) OR (FT no)']
+            ],
+            $params->getArrayCopy()
+        );
+    }
+
     /**
      * Test query parsing.
      *
-- 
GitLab