Fix issue with tag search.

This commit is contained in:
James Cole 2024-01-01 11:31:14 +01:00
parent ab0b03dd14
commit 949ca4f79e
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
4 changed files with 24 additions and 8 deletions

View File

@ -681,13 +681,16 @@ trait MetaCollection
// this method adds a "postFilter" to the collector.
$list = $tags->pluck('tag')->toArray();
$list = array_map('strtolower', $list);
$filter = static function (array $object) use ($list): bool {
$filter = static function (array $object) use ($list): bool | array {
$return = $object;
unset($return['transactions']);
$return['transactions'] = [];
Log::debug(sprintf('Now in setAllTags(%s) filter', implode(', ', $list)));
$expectedTagCount = count($list);
$foundTagCount = 0;
foreach ($object['transactions'] as $transaction) {
$transactionTagCount = count($transaction['tags']);
app('log')->debug(sprintf('Transaction has %d tag(s)', $transactionTagCount));
app('log')->debug(sprintf('Transaction #%d has %d tag(s)', $transaction['transaction_journal_id'], $transactionTagCount));
if ($transactionTagCount < $expectedTagCount) {
app('log')->debug(sprintf('Transaction has %d tag(s), we expect %d tag(s), return false.', $transactionTagCount, $expectedTagCount));
@ -698,13 +701,18 @@ trait MetaCollection
if (in_array(strtolower($tag['name']), $list, true)) {
app('log')->debug(sprintf('Transaction has tag "%s" so count++.', $tag['name']));
++$foundTagCount;
$return['transactions'][] = $transaction;
}
}
}
Log::debug(sprintf('Found %d tags, need at least %d.', $foundTagCount, $expectedTagCount));
// found at least the expected tags.
return $foundTagCount >= $expectedTagCount;
$result = $foundTagCount >= $expectedTagCount;
if (true === $result) {
return $return;
}
return false;
};
$this->postFilters[] = $filter;

View File

@ -43,6 +43,7 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/**
* Class GroupCollector
@ -560,6 +561,7 @@ class GroupCollector implements GroupCollectorInterface
if (0 !== count($journalIds)) {
// make all integers.
$integerIDs = array_map('intval', $journalIds);
Log::debug(sprintf('GroupCollector: setJournalIds: %s', join(', ', $integerIDs)));
$this->query->whereIn('transaction_journals.id', $integerIDs);
}
@ -948,7 +950,14 @@ class GroupCollector implements GroupCollectorInterface
// skip other filters, continue to next item.
continue;
}
$nextCollection->push($item);
// if the result is a bool, use the unedited results.
if(true === $result) {
$nextCollection->push($item);
}
// if the result is an array, the filter has changed what's being returned.
if(is_array($result)) {
$nextCollection->push($result);
}
}
$currentCollection = $nextCollection;
app('log')->debug(sprintf('GroupCollector: postFilterCollection has %d transaction(s) left.', count($currentCollection)));

View File

@ -156,8 +156,6 @@ class OperatorQuerySearch implements SearchInterface
foreach ($query1->getNodes() as $searchNode) {
$this->handleSearchNode($searchNode);
}
$this->parseTagInstructions();
$this->collector->setSearchWords($this->words);
$this->collector->excludeSearchWords($this->prohibitedWords);
}
@ -199,6 +197,7 @@ class OperatorQuerySearch implements SearchInterface
public function searchTransactions(): LengthAwarePaginator
{
$this->parseTagInstructions();
if (0 === count($this->getWords()) && 0 === count($this->getOperators())) {
return new LengthAwarePaginator([], 0, 5, 1);
}

View File

@ -183,7 +183,7 @@ class SearchRuleEngine implements RuleEngineInterface
continue;
}
// if needs no context, value is different:
// if the trigger needs no context, value is different:
$needsContext = (bool) (config(sprintf('search.operators.%s.needs_context', $ruleTrigger->trigger_type)) ?? true);
if (false === $needsContext) {
app('log')->debug(sprintf('SearchRuleEngine:: add a rule trigger (no context): %s:true', $ruleTrigger->trigger_type));
@ -211,7 +211,7 @@ class SearchRuleEngine implements RuleEngineInterface
$searchEngine->setPage(1);
$searchEngine->setLimit(31337);
$searchEngine->setDate($date);
app('log')->debug('Search array', $searchArray);
foreach ($searchArray as $type => $searches) {
foreach ($searches as $value) {
$query = sprintf('%s:%s', $type, $value);