Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit 2d8fb7f

Browse files
authored
Fix issue #209 and #210 (#211)
* Fix issue #209 and #210 Extract some logic * Add more thorough test
1 parent 52b1092 commit 2d8fb7f

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

docs/03-Usage.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,11 @@ class SearchController extends PageController
307307
$sort = isset($data['Order']) ? strtolower($data['Order']) : 'asc';
308308
309309
// Set the sorting. This can be an array of multiple sorts
310-
$params['sort'] = MySortableClass::class . '_Created ' . $sort;
311-
$query->setSort($params);
310+
$params['sort'] = [MySortableClass::class . '_Created ' => $sort];
311+
$query->setSort($params['sort']);
312+
// Alternative:
313+
$query->addSort(MySortableClass::class . '_Created', $sort);
314+
312315
313316
// Execute the search
314317
$result = $index->doSearch($query);

src/Factories/QueryComponentFactory.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,10 @@ public function setIndex(BaseIndex $index): self
226226
protected function buildTerms(): void
227227
{
228228
$terms = $this->query->getTerms();
229-
230229
$boostTerms = $this->getBoostTerms();
231230

232231
foreach ($terms as $search) {
233-
$term = $search['text'];
234-
$term = $this->escapeSearch($term);
232+
$term = $this->getBuildTerm($search);
235233
$postfix = $this->isFuzzy($search);
236234
// We can add the same term multiple times with different boosts
237235
// Not ideal, but it might happen, so let's add the term itself only once
@@ -308,4 +306,21 @@ protected function buildSpellcheck(): void
308306
$spellcheck->setExtendedResults(true);
309307
$spellcheck->setCollateExtendedResults(true);
310308
}
309+
310+
/**
311+
* Get the escaped search string, or, if empty, a global search
312+
*
313+
* @param array $search
314+
* @return string
315+
*/
316+
protected function getBuildTerm($search)
317+
{
318+
$term = $search['text'];
319+
$term = $this->escapeSearch($term);
320+
if ($term === '') {
321+
$term = '*:*';
322+
}
323+
324+
return $term;
325+
}
311326
}

src/Indexes/BaseIndex.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ abstract class BaseIndex
8888
* @var array The query terms as an array
8989
*/
9090
protected $queryTerms = [];
91+
/**
92+
* @var Query Query that will hit the client
93+
*/
94+
protected $clientQuery;
9195
/**
9296
* @var bool Signify if a retry should occur if nothing was found and there are suggestions to follow
9397
*/
@@ -199,12 +203,14 @@ public function doSearch(BaseQuery $query)
199203
{
200204
SiteState::alterQuery($query);
201205
// Build the actual query parameters
202-
$clientQuery = $this->buildSolrQuery($query);
206+
$this->clientQuery = $this->buildSolrQuery($query);
207+
// Set the sorting
208+
$this->clientQuery->addSorts($query->getSort());
203209

204-
$this->extend('onBeforeSearch', $query, $clientQuery);
210+
$this->extend('onBeforeSearch', $query, $this->clientQuery);
205211

206212
try {
207-
$result = $this->client->select($clientQuery);
213+
$result = $this->client->select($this->clientQuery);
208214
} catch (Exception $error) {
209215
// @codeCoverageIgnoreStart
210216
$logger = new SolrLogger();
@@ -415,4 +421,12 @@ public function getQueryFactory(): QueryComponentFactory
415421
{
416422
return $this->queryFactory;
417423
}
424+
425+
/**
426+
* @return Query
427+
*/
428+
public function getClientQuery(): Query
429+
{
430+
return $this->clientQuery;
431+
}
418432
}

src/Traits/QueryTraits/BaseQueryTrait.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ trait BaseQueryTrait
5151
*/
5252
protected $exclude = [];
5353

54+
/**
55+
* @var array Sorting order
56+
*/
57+
protected $sort = [];
58+
5459
/**
5560
* Each boosted query needs a separate addition!
5661
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3)
@@ -138,4 +143,18 @@ public function addFacetFilter($field, $value): self
138143

139144
return $this;
140145
}
146+
147+
/**
148+
* Add a field to sort on
149+
*
150+
* @param string $field
151+
* @param string $direction
152+
* @return $this
153+
*/
154+
public function addSort($field, $direction): self
155+
{
156+
$this->sort[$field] = $direction;
157+
158+
return $this;
159+
}
141160
}

tests/unit/BaseIndexTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,22 @@ public function testDoSearch()
308308
$this->assertInstanceOf(ArrayList::class, $result->getSpellcheck());
309309
$this->assertGreaterThan(0, $result->getSpellcheck()->count());
310310
$this->assertNotEmpty($result->getCollatedSpellcheck());
311+
312+
$index = new CircleCITestIndex();
313+
$query = new BaseQuery();
314+
$query->addTerm('');
315+
$index->doSearch($query);
316+
317+
$this->assertEquals(['*:*'], $index->getQueryTerms());
318+
319+
$index = new CircleCITestIndex();
320+
$query = new BaseQuery();
321+
$query->addTerm('test');
322+
$query->addSort('SiteTree_Title', 'asc');
323+
$index->doSearch($query);
324+
325+
$this->assertEquals(['test'], $index->getQueryTerms());
326+
$this->assertArrayHasKey('SiteTree_Title', $index->getClientQuery()->getSorts());
311327
}
312328

313329
public function testDoRetry()

0 commit comments

Comments
 (0)