Fix Solr Date Ranges

This commit is contained in:
Lucas Bartholemy 2024-03-29 09:07:52 +01:00
parent 432df3fe8b
commit 88fe07c878
2 changed files with 21 additions and 20 deletions

View File

@ -160,21 +160,8 @@ class ZendLucenceDriver extends AbstractDriver
}
if (!empty($request->dateFrom) || !empty($request->dateTo)) {
if (str_contains(get_called_class(), 'Solr')) {
$dateFrom = empty($request->dateFrom)
? new Term('*', 'created_at')
: new Term('"' . $request->dateFrom . ' 00:00:00"', 'created_at');
$dateTo = empty($request->dateTo)
? new Term('*', 'created_at')
: new Term('"' . $request->dateTo . ' 23:59:59"', 'created_at');
} else {
$dateFrom = empty($request->dateFrom)
? null
: new Term($request->dateFrom . ' 00:00:00', 'created_at');
$dateTo = empty($request->dateTo)
? null
: new Term($request->dateTo . ' 23:59:59', 'created_at');
}
$dateFrom = $this->convertRangeValue('created_at', $request->dateFrom, ' 00:00:00');
$dateTo = $this->convertRangeValue('created_at', $request->dateTo, ' 23:59:59');
$query->addSubquery(new Range($dateFrom, $dateTo, true), true);
}
@ -213,6 +200,21 @@ class ZendLucenceDriver extends AbstractDriver
return $query;
}
/**
* ZendLucene and Solr sometimes require a different format here.
* e.g. Sol needs a "*" instead of "null" and quoted dates with time
*
* @param string $field
* @param string|null $value
* @param string $suffix
* @return Term|null
*/
protected function convertRangeValue(string $field, ?string $value, string $suffix = ''): ?Term
{
return empty($value) ? null : new Term($value . $suffix, $field);
}
private function getIndex(): Index
{
if ($this->_index) {

View File

@ -65,15 +65,14 @@ abstract class AbstractDriverTestSuite extends HumHubDbTestCase
$space = Space::findOne(['id' => 1]);
$this->becomeUser('Admin');
(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Some Other']))->save();
(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Marabru Leav Test X']))->save();
(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Marabru Leav Y Test X']))->save();
// Short keywords
$this->assertEquals(0, count($this->getSearchResultByKeyword('R')->results));
$this->assertEquals(1, count($this->getSearchResultByKeyword('T')->results));
if ($this->searchDriver instanceof ZendLucenceDriver) {
// MysqlDriver can find only at least 2 char exist after "X"
$this->assertEquals(1, count($this->getSearchResultByKeyword('X')->results));
}
// Most search indexes do not index individual letters.
}
private function getSearchRequest(): SearchRequest