Expand search with a bunch of keywords for #510

This commit is contained in:
James Cole 2017-02-18 20:10:03 +01:00
parent f0cb63fd48
commit 5073fd937c
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
6 changed files with 426 additions and 133 deletions

View File

@ -15,6 +15,8 @@ namespace FireflyIII\Http\Controllers;
use FireflyIII\Support\Search\SearchInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use View;
/**
* Class SearchController
@ -30,44 +32,66 @@ class SearchController extends Controller
{
parent::__construct();
$this->middleware(
function ($request, $next) {
View::share('mainTitleIcon', 'fa-search');
View::share('title', trans('firefly.search'));
return $next($request);
}
);
}
/**
* Results always come in the form of an array [results, count, fullCount]
*
* @param Request $request
* @param SearchInterface $searcher
*
* @return $this
* @return View
*/
public function index(Request $request, SearchInterface $searcher)
{
$minSearchLen = 1;
$subTitle = null;
$query = null;
$result = [];
$title = trans('firefly.search');
$limit = 20;
$mainTitleIcon = 'fa-search';
// yes, hard coded values:
$minSearchLen = 1;
$limit = 20;
// set limit for search:
$searcher->setLimit($limit);
// ui stuff:
$subTitle = '';
// query stuff
$query = null;
$result = [];
$rawQuery = $request->get('q');
$hasModifiers = true;
$modifiers = [];
if (!is_null($request->get('q')) && strlen($request->get('q')) >= $minSearchLen) {
$query = trim(strtolower($request->get('q')));
$words = explode(' ', $query);
$subTitle = trans('firefly.search_results_for', ['query' => $query]);
// parse query, find modifiers:
// set limit for search
$searcher->setLimit($limit);
$searcher->parseQuery($request->get('q'));
$transactions = $searcher->searchTransactions($words);
$accounts = $searcher->searchAccounts($words);
$categories = $searcher->searchCategories($words);
$budgets = $searcher->searchBudgets($words);
$tags = $searcher->searchTags($words);
$result = ['transactions' => $transactions, 'accounts' => $accounts, 'categories' => $categories, 'budgets' => $budgets, 'tags' => $tags];
$transactions = $searcher->searchTransactions();
$accounts = new Collection;
$categories = new Collection;
$tags = new Collection;
$budgets = new Collection;
// no special search thing?
if (!$searcher->hasModifiers()) {
$hasModifiers = false;
$accounts = $searcher->searchAccounts();
$categories = $searcher->searchCategories();
$budgets = $searcher->searchBudgets();
$tags = $searcher->searchTags();
}
$query = $searcher->getWordsAsString();
$subTitle = trans('firefly.search_results_for', ['query' => $query]);
$result = ['transactions' => $transactions, 'accounts' => $accounts, 'categories' => $categories, 'budgets' => $budgets, 'tags' => $tags];
}
return view('search.index', compact('title', 'subTitle', 'limit', 'mainTitleIcon', 'query', 'result'));
return view('search.index', compact('rawQuery', 'hasModifiers', 'modifiers', 'subTitle', 'limit', 'query', 'result'));
}
}

View File

@ -0,0 +1,108 @@
<?php
/**
* Modifier.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Support\Search;
use FireflyIII\Models\Transaction;
use Log;
use Steam;
class Modifier
{
/**
* @param Transaction $transaction
* @param string $amount
* @param int $expected
*
* @return bool
*/
public static function amountCompare(Transaction $transaction, string $amount, int $expected): bool
{
$amount = Steam::positive($amount);
$transactionAmount = Steam::positive($transaction->transaction_amount);
$compare = bccomp($amount, $transactionAmount);
Log::debug(sprintf('%s vs %s is %d', $amount, $transactionAmount, $compare));
return $compare === $expected;
}
/**
* @param Transaction $transaction
* @param string $amount
*
* @return bool
*/
public static function amountIs(Transaction $transaction, string $amount): bool
{
return self::amountCompare($transaction, $amount, 0);
}
/**
* @param Transaction $transaction
* @param string $amount
*
* @return bool
*/
public static function amountLess(Transaction $transaction, string $amount): bool
{
return self::amountCompare($transaction, $amount, 1);
}
/**
* @param Transaction $transaction
* @param string $amount
*
* @return bool
*/
public static function amountMore(Transaction $transaction, string $amount): bool
{
return self::amountCompare($transaction, $amount, -1);
}
/**
* @param Transaction $transaction
* @param string $destination
*
* @return bool
*/
public static function destination(Transaction $transaction, string $destination): bool
{
return self::stringCompare($transaction->opposing_account_name, $destination);
}
/**
* @param Transaction $transaction
* @param string $source
*
* @return bool
*/
public static function source(Transaction $transaction, string $source): bool
{
return self::stringCompare($transaction->account_name, $source);
}
/**
* @param string $haystack
* @param string $needle
*
* @return bool
*/
public static function stringCompare(string $haystack, string $needle): bool
{
$res = !(strpos(strtolower($haystack), strtolower($needle)) === false);
Log::debug(sprintf('"%s" is in "%s"? %s', $needle, $haystack, var_export($res, true)));
return $res;
}
}

View File

@ -14,6 +14,7 @@ declare(strict_types = 1);
namespace FireflyIII\Support\Search;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
@ -34,19 +35,64 @@ class Search implements SearchInterface
{
/** @var int */
private $limit = 100;
/** @var Collection */
private $modifiers;
/** @var User */
private $user;
/** @var array */
private $validModifiers = [];
/** @var array */
private $words = [];
/**
* Search constructor.
*/
public function __construct()
{
$this->modifiers = new Collection;
$this->validModifiers = config('firefly.search_modifiers');
}
/**
* @return string
*/
public function getWordsAsString(): string
{
return join(' ', $this->words);
}
/**
* @return bool
*/
public function hasModifiers(): bool
{
return $this->modifiers->count() > 0;
}
/**
* @param string $query
*/
public function parseQuery(string $query)
{
$filteredQuery = $query;
$pattern = '/[a-z_]*:[0-9a-z.]*/i';
$matches = [];
preg_match_all($pattern, $query, $matches);
foreach ($matches[0] as $match) {
$this->extractModifier($match);
$filteredQuery = str_replace($match, '', $filteredQuery);
}
$filteredQuery = trim(str_replace(['"', "'"], '', $filteredQuery));
$this->words = array_map('trim', explode(' ', $filteredQuery));
}
/**
* The search will assume that the user does not have so many accounts
* that this search should be paginated.
*
* @param array $words
*
* @return Collection
*/
public function searchAccounts(array $words): Collection
public function searchAccounts(): Collection
{
$words = $this->words;
$accounts = $this->user->accounts()
->accountTypeIn([AccountType::DEFAULT, AccountType::ASSET, AccountType::EXPENSE, AccountType::REVENUE, AccountType::BENEFICIARY])
->get(['accounts.*']);
@ -67,14 +113,13 @@ class Search implements SearchInterface
}
/**
* @param array $words
*
* @return Collection
*/
public function searchBudgets(array $words): Collection
public function searchBudgets(): Collection
{
/** @var Collection $set */
$set = auth()->user()->budgets()->get();
$set = auth()->user()->budgets()->get();
$words = $this->words;
/** @var Collection $result */
$result = $set->filter(
function (Budget $budget) use ($words) {
@ -92,14 +137,11 @@ class Search implements SearchInterface
}
/**
* Search assumes the user does not have that many categories. So no paginated search.
*
* @param array $words
*
* @return Collection
*/
public function searchCategories(array $words): Collection
public function searchCategories(): Collection
{
$words = $this->words;
$categories = $this->user->categories()->get();
/** @var Collection $result */
$result = $categories->filter(
@ -117,15 +159,12 @@ class Search implements SearchInterface
}
/**
*
* @param array $words
*
* @return Collection
*/
public function searchTags(array $words): Collection
public function searchTags(): Collection
{
$tags = $this->user->tags()->get();
$words = $this->words;
$tags = $this->user->tags()->get();
/** @var Collection $result */
$result = $tags->filter(
function (Tag $tag) use ($words) {
@ -142,11 +181,9 @@ class Search implements SearchInterface
}
/**
* @param array $words
*
* @return Collection
*/
public function searchTransactions(array $words): Collection
public function searchTransactions(): Collection
{
$pageSize = 100;
$processed = 0;
@ -156,20 +193,17 @@ class Search implements SearchInterface
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAllAssetAccounts()->setLimit($pageSize)->setPage($page);
$set = $collector->getPaginatedJournals();
$collector->setAllAssetAccounts()->setLimit($pageSize)->setPage($page)->withOpposingAccount();
$set = $collector->getPaginatedJournals();
$words = $this->words;
Log::debug(sprintf('Found %d journals to check. ', $set->count()));
// Filter transactions that match the given triggers.
$filtered = $set->filter(
function (Transaction $transaction) use ($words) {
// check descr of journal:
if ($this->strpos_arr(strtolower(strval($transaction->description)), $words)) {
return $transaction;
}
// check descr of transaction
if ($this->strpos_arr(strtolower(strval($transaction->transaction_description)), $words)) {
if ($this->matchModifiers($transaction)) {
return $transaction;
}
@ -201,6 +235,8 @@ class Search implements SearchInterface
} while (!$reachedEndOfList && !$foundEnough);
$result = $result->slice(0, $this->limit);
var_dump($result->toArray());
exit;
return $result;
}
@ -221,6 +257,79 @@ class Search implements SearchInterface
$this->user = $user;
}
/**
* @param string $string
*/
private function extractModifier(string $string)
{
$parts = explode(':', $string);
if (count($parts) === 2 && strlen(trim(strval($parts[0]))) > 0 && strlen(trim(strval($parts[1])))) {
$type = trim(strval($parts[0]));
$value = trim(strval($parts[1]));
if (in_array($type, $this->validModifiers)) {
// filter for valid type
$this->modifiers->push(['type' => $type, 'value' => $value,]);
}
}
}
/**
* @param Transaction $transaction
*
* @return bool
* @throws FireflyException
*/
private function matchModifiers(Transaction $transaction): bool
{
Log::debug(sprintf('Now at transaction #%d', $transaction->id));
// first "modifier" is always the text of the search:
// check descr of journal:
if (!$this->strpos_arr(strtolower(strval($transaction->description)), $this->words)
&& !$this->strpos_arr(strtolower(strval($transaction->transaction_description)), $this->words)
) {
Log::debug('Description does not match', $this->words);
return false;
}
// then a for-each and a switch for every possible other thingie.
foreach ($this->modifiers as $modifier) {
switch ($modifier['type']) {
default:
throw new FireflyException(sprintf('Search modifier "%s" is not (yet) supported. Sorry!', $modifier['type']));
break;
case 'amount_is':
$res = Modifier::amountIs($transaction, $modifier['value']);
Log::debug(sprintf('Amount is %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'amount_less':
$res = Modifier::amountLess($transaction, $modifier['value']);
Log::debug(sprintf('Amount less than %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'amount_more':
$res = Modifier::amountMore($transaction, $modifier['value']);
Log::debug(sprintf('Amount more than %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'source':
$res = Modifier::source($transaction, $modifier['value']);
Log::debug(sprintf('Source is %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'destination':
$res = Modifier::destination($transaction, $modifier['value']);
Log::debug(sprintf('Destination is %s? %s', $modifier['value'], var_export($res, true)));
break;
}
if ($res === false) {
return $res;
}
}
return true;
}
/**
* @param string $haystack
* @param array $needle

View File

@ -24,40 +24,49 @@ use Illuminate\Support\Collection;
interface SearchInterface
{
/**
* @param array $words
*
* @return Collection
* @return string
*/
public function searchAccounts(array $words): Collection;
public function getWordsAsString(): string;
/**
* @param array $words
*
* @return Collection
* @return bool
*/
public function searchBudgets(array $words): Collection;
public function hasModifiers(): bool;
/**
* @param array $words
*
* @return Collection
* @param string $query
*/
public function searchCategories(array $words): Collection;
public function parseQuery(string $query);
/**
*
* @param array $words
*
* @return Collection
*/
public function searchTags(array $words): Collection;
public function searchAccounts(): Collection;
/**
* @param array $words
*
* @return Collection
*/
public function searchTransactions(array $words): Collection;
public function searchBudgets(): Collection;
/**
* @return Collection
*/
public function searchCategories(): Collection;
/**
* @return Collection
*/
public function searchTags(): Collection;
/**
* @return Collection
*/
public function searchTransactions(): Collection;
/**
* @param int $limit
*/
public function setLimit(int $limit);
/**
* @param User $user

View File

@ -209,4 +209,6 @@ return [
],
'default_currency' => 'EUR',
'default_language' => 'en_US',
'search_modifiers' => ['amount_is', 'amount_less', 'amount_more', 'source', 'destination', 'category', 'budget', 'tag', 'bill', 'type', 'date_on',
'date_before', 'date_after', 'has_attachments', 'notes',],
];

View File

@ -16,15 +16,38 @@
<div class="row">
<div class="col-lg-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">{{ 'advanced_search'|_ }}</h3>
</div>
<div class="box-body">
<p>
There are several modifiers that you can use in your search to narrow down the results.
If you use any of these, the search will <em>only</em> return transactions.
Please click the <i class="fa fa-question-circle"></i>-icon for more information.
</p>
{# search form #}
<form class="form-horizontal" action="{{ route('search.index') }}" method="get">
<div class="form-group">
<label for="query" class="col-sm-1 control-label">Query</label>
<div class="col-sm-10">
<input type="text" name="q" id="query" value="{{ rawQuery }}" class="form-control" placeholder="{{ rawQuery }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<button type="submit" class="btn btn-default"><i class="fa fa-search"></i> Search</button>
</div>
</div>
</form>
</div>
</div>
<p>{{ trans('firefly.results_limited', {count: limit}) }}</p>
</div>
</div>
<div class="row">
{% if result.transactions|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
{% if hasModifiers %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'transactions'|_ }}</h3>
@ -37,68 +60,86 @@
</div>
</div>
</div>
{% endif %}
{% if result.categories|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'categories'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_categories', {count: result.categories|length}) }}
</p>
{% include 'search.partials.categories' %}
</div>
{% else %}
<div class="row">
{% if result.transactions|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'transactions'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_transactions', {count: result.transactions|length}) }}
</p>
{% include 'search.partials.transactions' with {'transactions' : result.transactions} %}
</div>
</div>
</div>
</div>
{% endif %}
{% if result.tags|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'tags'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_tags', {count: result.tags|length}) }}
</p>
{% include 'search.partials.tags' %}
{% endif %}
{% if result.categories|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'categories'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_categories', {count: result.categories|length}) }}
</p>
{% include 'search.partials.categories' %}
</div>
</div>
</div>
</div>
{% endif %}
{% if result.accounts|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'accounts'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_accounts', {count: result.accounts|length}) }}
</p>
{% include 'search.partials.accounts' %}
{% endif %}
{% if result.tags|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'tags'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_tags', {count: result.tags|length}) }}
</p>
{% include 'search.partials.tags' %}
</div>
</div>
</div>
</div>
{% endif %}
{% if result.budgets|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'budgets'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_budgets', {count: result.budgets|length}) }}
</p>
{% include 'search.partials.budgets' %}
{% endif %}
{% if result.accounts|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'accounts'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_accounts', {count: result.accounts|length}) }}
</p>
{% include 'search.partials.accounts' %}
</div>
</div>
</div>
</div>
{% endif %}
</div>
{% endif %}
{% if result.budgets|length > 0 %}
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'budgets'|_ }}</h3>
</div>
<div class="box-body">
<p>
{{ trans('firefly.search_found_budgets', {count: result.budgets|length}) }}
</p>
{% include 'search.partials.budgets' %}
</div>
</div>
</div>
{% endif %}
</div>
{% endif %}
{% endif %}