From e93978fb72e1df79e6e50f6d544e4bc23a7b58bf Mon Sep 17 00:00:00 2001 From: Bas de Nooijer Date: Wed, 9 Nov 2016 14:46:50 +0100 Subject: [PATCH] Fix issue with ranges and quotes in filterquery key parsing --- .../Solarium/QueryBuilder/QueryBuilder.php | 4 ++- .../QueryBuilder/Tests/QueryBuilderTest.php | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/library/Solarium/QueryBuilder/QueryBuilder.php b/library/Solarium/QueryBuilder/QueryBuilder.php index 0328f1b..dd43267 100644 --- a/library/Solarium/QueryBuilder/QueryBuilder.php +++ b/library/Solarium/QueryBuilder/QueryBuilder.php @@ -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; diff --git a/tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php b/tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php index a70da04..e976466 100644 --- a/tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php +++ b/tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php @@ -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', '');