Skip to content

Commit

Permalink
Fix issue with ranges and quotes in filterquery key parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas de Nooijer authored and Bas de Nooijer committed Nov 9, 2016
1 parent 13ee582 commit e93978f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion library/Solarium/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ protected function parseFilterQueries(Query $query, Request $request)
}

if (!array_key_exists('key', $filterQueryParams)) {
preg_match_all('/\b([\w]+):/', $filterQueryParams['query'], $matches);
// Remove content inside quotes and brackets, as these can contain semicolons
$queryString = preg_replace('/[\"\[].*[\"\]]/u', '', $filterQueryParams['query']);
preg_match_all('/\b([\w]+):/', $queryString, $matches);
$key = implode('-', $matches[1]);
$filterQueryParams['key'] = $key;

Expand Down
30 changes: 30 additions & 0 deletions tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,36 @@ public function testQueryWithMultipleFilters()
$this->assertEquals('cat:C', $filterQuery->getQuery());
}

public function testQueryWithQuotedDateFilter()
{
$this->request->addParam('fq', 'startDate:"2016-01-01T00:00:00Z"');
$this->queryBuilder->build($this->query, $this->request);
$filterQueries = $this->query->getFilterQueries();

$this->assertCount(1, $filterQueries);
$filterQuery = current($filterQueries);
$this->assertEquals('startDate', $filterQuery->getKey());
$this->assertEquals('startDate:"2016-01-01T00:00:00Z"', $filterQuery->getQuery());
}

public function testQueryWithDateRangeFilters()
{
$this->request->addParam('fq', 'dateA:[2016-01-01T00:00:00Z TO 2016-01-02T00:00:00Z]');
$this->request->addParam('fq', 'dateB:["2016-01-01T00:00:00Z" TO "2016-01-02T00:00:00Z"]');
$this->queryBuilder->build($this->query, $this->request);
$filterQueries = $this->query->getFilterQueries();

$this->assertCount(2, $filterQueries);

$filterQuery = current($filterQueries);
$this->assertEquals('dateA', $filterQuery->getKey());
$this->assertEquals('dateA:[2016-01-01T00:00:00Z TO 2016-01-02T00:00:00Z]', $filterQuery->getQuery());

$filterQuery = next($filterQueries);
$this->assertEquals('dateB', $filterQuery->getKey());
$this->assertEquals('dateB:["2016-01-01T00:00:00Z" TO "2016-01-02T00:00:00Z"]', $filterQuery->getQuery());
}

public function testQueryWithEmptyFilter()
{
$this->request->addParam('fq', '');
Expand Down

0 comments on commit e93978f

Please sign in to comment.