Skip to content

Commit 0bb7e09

Browse files
author
floriansemm
committed
fix search in arrays
1 parent 240d981 commit 0bb7e09

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

Query/SolrQuery.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,7 @@ public function getQuery()
174174
$termCount = 1;
175175
foreach ($this->searchTerms as $fieldName => $fieldValue) {
176176

177-
178-
if ($this->useWildcards) {
179-
$fieldValue = '*' . $fieldValue . '*';
180-
}
181-
182-
$termParts = explode(' ', $fieldValue);
183-
if (count($termParts) > 1) {
184-
$fieldValue = '"'.$fieldValue.'"';
185-
}
177+
$fieldValue = $this->querifyFieldValue($fieldValue);
186178

187179
$term .= $fieldName . ':' . $fieldValue;
188180

@@ -198,4 +190,41 @@ public function getQuery()
198190
return $term;
199191
}
200192

193+
/**
194+
* Transforms array to string representation and adds quotes
195+
*
196+
* @param string $fieldValue
197+
*
198+
* @return string
199+
*/
200+
private function querifyFieldValue($fieldValue)
201+
{
202+
if (is_array($fieldValue) && count($fieldValue) > 1) {
203+
sort($fieldValue);
204+
205+
$qutoed = array_map(function($value) {
206+
return '"'. $value .'"';
207+
}, $fieldValue);
208+
209+
$fieldValue = implode(' TO ', $qutoed);
210+
$fieldValue = '['. $fieldValue . ']';
211+
212+
return $fieldValue;
213+
}
214+
215+
if (is_array($fieldValue) && count($fieldValue) === 1) {
216+
$fieldValue = array_pop($fieldValue);
217+
}
218+
219+
if ($this->useWildcards) {
220+
$fieldValue = '*' . $fieldValue . '*';
221+
}
222+
223+
$termParts = explode(' ', $fieldValue);
224+
if (count($termParts) > 1) {
225+
$fieldValue = '"'.$fieldValue.'"';
226+
}
227+
228+
return $fieldValue;
229+
}
201230
}

Tests/Query/SolrQueryTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,30 @@ public function testGetQuery_CustomQuery()
189189

190190
$this->assertEquals($expected, $solrQuery->getQuery());
191191
}
192+
193+
/**
194+
* @test
195+
*/
196+
public function searchInSetMultipleValues()
197+
{
198+
$solrQuery = $this->createQueryWithFieldMapping();
199+
$solrQuery->addSearchTerm('title', array('value2', 'value1'));
200+
201+
$expected = 'title_s:["value1" TO "value2"]';
202+
203+
$this->assertEquals($expected, $solrQuery->getQuery());
204+
}
205+
206+
/**
207+
* @test
208+
*/
209+
public function searchInSetSingleValues()
210+
{
211+
$solrQuery = $this->createQueryWithFieldMapping();
212+
$solrQuery->addSearchTerm('title', array('value #1'));
213+
214+
$expected = 'title_s:"value #1"';
215+
216+
$this->assertEquals($expected, $solrQuery->getQuery());
217+
}
192218
}

0 commit comments

Comments
 (0)