Skip to content

Commit

Permalink
Fix issue #209 and #210 (#211)
Browse files Browse the repository at this point in the history
* Fix issue #209 and #210

Extract some logic

* Add more thorough test
  • Loading branch information
Firesphere authored Mar 31, 2020
1 parent 52b1092 commit 2d8fb7f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
7 changes: 5 additions & 2 deletions docs/03-Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,11 @@ class SearchController extends PageController
$sort = isset($data['Order']) ? strtolower($data['Order']) : 'asc';
// Set the sorting. This can be an array of multiple sorts
$params['sort'] = MySortableClass::class . '_Created ' . $sort;
$query->setSort($params);
$params['sort'] = [MySortableClass::class . '_Created ' => $sort];
$query->setSort($params['sort']);
// Alternative:
$query->addSort(MySortableClass::class . '_Created', $sort);
// Execute the search
$result = $index->doSearch($query);
Expand Down
21 changes: 18 additions & 3 deletions src/Factories/QueryComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,10 @@ public function setIndex(BaseIndex $index): self
protected function buildTerms(): void
{
$terms = $this->query->getTerms();

$boostTerms = $this->getBoostTerms();

foreach ($terms as $search) {
$term = $search['text'];
$term = $this->escapeSearch($term);
$term = $this->getBuildTerm($search);
$postfix = $this->isFuzzy($search);
// We can add the same term multiple times with different boosts
// Not ideal, but it might happen, so let's add the term itself only once
Expand Down Expand Up @@ -308,4 +306,21 @@ protected function buildSpellcheck(): void
$spellcheck->setExtendedResults(true);
$spellcheck->setCollateExtendedResults(true);
}

/**
* Get the escaped search string, or, if empty, a global search
*
* @param array $search
* @return string
*/
protected function getBuildTerm($search)
{
$term = $search['text'];
$term = $this->escapeSearch($term);
if ($term === '') {
$term = '*:*';
}

return $term;
}
}
20 changes: 17 additions & 3 deletions src/Indexes/BaseIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ abstract class BaseIndex
* @var array The query terms as an array
*/
protected $queryTerms = [];
/**
* @var Query Query that will hit the client
*/
protected $clientQuery;
/**
* @var bool Signify if a retry should occur if nothing was found and there are suggestions to follow
*/
Expand Down Expand Up @@ -199,12 +203,14 @@ public function doSearch(BaseQuery $query)
{
SiteState::alterQuery($query);
// Build the actual query parameters
$clientQuery = $this->buildSolrQuery($query);
$this->clientQuery = $this->buildSolrQuery($query);
// Set the sorting
$this->clientQuery->addSorts($query->getSort());

$this->extend('onBeforeSearch', $query, $clientQuery);
$this->extend('onBeforeSearch', $query, $this->clientQuery);

try {
$result = $this->client->select($clientQuery);
$result = $this->client->select($this->clientQuery);
} catch (Exception $error) {
// @codeCoverageIgnoreStart
$logger = new SolrLogger();
Expand Down Expand Up @@ -415,4 +421,12 @@ public function getQueryFactory(): QueryComponentFactory
{
return $this->queryFactory;
}

/**
* @return Query
*/
public function getClientQuery(): Query
{
return $this->clientQuery;
}
}
19 changes: 19 additions & 0 deletions src/Traits/QueryTraits/BaseQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ trait BaseQueryTrait
*/
protected $exclude = [];

/**
* @var array Sorting order
*/
protected $sort = [];

/**
* Each boosted query needs a separate addition!
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3)
Expand Down Expand Up @@ -138,4 +143,18 @@ public function addFacetFilter($field, $value): self

return $this;
}

/**
* Add a field to sort on
*
* @param string $field
* @param string $direction
* @return $this
*/
public function addSort($field, $direction): self
{
$this->sort[$field] = $direction;

return $this;
}
}
16 changes: 16 additions & 0 deletions tests/unit/BaseIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@ public function testDoSearch()
$this->assertInstanceOf(ArrayList::class, $result->getSpellcheck());
$this->assertGreaterThan(0, $result->getSpellcheck()->count());
$this->assertNotEmpty($result->getCollatedSpellcheck());

$index = new CircleCITestIndex();
$query = new BaseQuery();
$query->addTerm('');
$index->doSearch($query);

$this->assertEquals(['*:*'], $index->getQueryTerms());

$index = new CircleCITestIndex();
$query = new BaseQuery();
$query->addTerm('test');
$query->addSort('SiteTree_Title', 'asc');
$index->doSearch($query);

$this->assertEquals(['test'], $index->getQueryTerms());
$this->assertArrayHasKey('SiteTree_Title', $index->getClientQuery()->getSorts());
}

public function testDoRetry()
Expand Down

0 comments on commit 2d8fb7f

Please sign in to comment.