[COMPONENT][Search] Update Search to be able to search for either notes or actors

This commit is contained in:
Hugo Sales 2021-10-10 05:44:10 +01:00 committed by Diogo Peralta Cordeiro
parent d575f8aef5
commit ce0cfa7a63
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 92 additions and 43 deletions

View File

@ -32,18 +32,28 @@ class Search extends Controller
public function handle(Request $request) public function handle(Request $request)
{ {
$q = $this->string('q'); $q = $this->string('q');
$criteria = Parser::parse($q); [$note_criteria, $actor_criteria] = Parser::parse($q);
$qb = DB::createQueryBuilder(); $note_qb = DB::createQueryBuilder();
$qb->select('note')->from('App\Entity\Note', 'note'); $actor_qb = DB::createQueryBuilder();
Event::handle('SeachQueryAddJoins', [&$qb]); $note_qb->select('note')->from('App\Entity\Note', 'note');
$qb->addCriteria($criteria); $actor_qb->select('actor')->from('App\Entity\Actor', 'actor');
$query = $qb->getQuery(); Event::handle('SeachQueryAddJoins', [&$note_qb, &$actor_qb]);
$results = $query->execute(); $notes = $actors = [];
if (!is_null($note_criteria)) {
$note_qb->addCriteria($note_criteria);
$notes = $note_qb->getQuery()->execute();
} else {
if (!is_null($actor_criteria)) {
$actor_qb->addCriteria($actor_criteria);
$actors = $actor_qb->getQuery()->execute();
}
}
return [ return [
'_template' => 'search/show.html.twig', '_template' => 'search/show.html.twig',
'results' => $results, 'notes' => $notes,
'actors' => $actors,
]; ];
} }
} }

View File

@ -34,8 +34,10 @@ abstract class Parser
* recognises either spaces (currently `or`, should be fuzzy match), `OR` or `|` (`or`) and `AND` or `&` (`and`) * recognises either spaces (currently `or`, should be fuzzy match), `OR` or `|` (`or`) and `AND` or `&` (`and`)
* *
* TODO Better fuzzy match, implement exact match with quotes and nesting with parens * TODO Better fuzzy match, implement exact match with quotes and nesting with parens
*
* @return Criteria[]
*/ */
public static function parse(string $input, int $level = 0): Criteria public static function parse(string $input, int $level = 0): array
{ {
if ($level === 0) { if ($level === 0) {
$input = trim(preg_replace(['/\s+/', '/\s+AND\s+/', '/\s+OR\s+/'], [' ', '&', '|'], $input), ' |&'); $input = trim(preg_replace(['/\s+/', '/\s+AND\s+/', '/\s+OR\s+/'], [' ', '&', '|'], $input), ' |&');
@ -45,18 +47,20 @@ abstract class Parser
$lenght = mb_strlen($input); $lenght = mb_strlen($input);
$stack = []; $stack = [];
$eb = Criteria::expr(); $eb = Criteria::expr();
$criteria = []; $note_criteria_arr = [];
$parts = []; $actor_criteria_arr = [];
$note_parts = [];
$actor_parts = [];
$last_op = null; $last_op = null;
$connect_parts = /** $connect_parts = /**
* Merge $parts into $criteria * Merge $parts into $criteria_arr
*/ */
function (bool $force = false) use ($eb, &$parts, $last_op, &$criteria) { function (array &$parts, array &$criteria_arr, bool $force = false) use ($eb, $last_op) {
foreach ([' ' => 'orX', '|' => 'orX', '&' => 'andX'] as $op => $func) { foreach ([' ' => 'orX', '|' => 'orX', '&' => 'andX'] as $op => $func) {
if ($last_op === $op || $force) { if ($last_op === $op || $force) {
$criteria[] = $eb->{$func}(...$parts); $criteria_arr[] = $eb->{$func}(...$parts);
$parts = []; $note_parts = [];
break; break;
} }
} }
@ -69,17 +73,27 @@ abstract class Parser
foreach (['&', '|', ' '] as $delimiter) { foreach (['&', '|', ' '] as $delimiter) {
if ($input[$index] === $delimiter || $end = ($index === $lenght - 1)) { if ($input[$index] === $delimiter || $end = ($index === $lenght - 1)) {
$term = substr($input, $left, $end ? null : $right - $left); $term = substr($input, $left, $end ? null : $right - $left);
$res = null; $note_res = $actor_res = null;
$ret = Event::handle('SearchCreateExpression', [$eb, $term, &$res]); $ret = Event::handle('SearchCreateExpression', [$eb, $term, &$note_res, &$actor_res]);
if (is_null($res) || $ret == Event::next) { if ((is_null($note_res) && is_null($actor_res)) || $ret == Event::next) {
throw new ServerException("No one claimed responsibility for a match term: {$term}"); throw new ServerException("No one claimed responsibility for a match term: {$term}");
} else {
if (!is_null($note_res)) {
$note_parts[] = $note_res;
} else {
if (!is_null($actor_res)) {
$actor_parts[] = $actor_res;
} else {
throw new ServerException('Unexpected state in Search parser');
}
}
} }
$parts[] = $res;
$right = $left = $index + 1; $right = $left = $index + 1;
if (!is_null($last_op) && $last_op !== $delimiter) { if (!is_null($last_op) && $last_op !== $delimiter) {
$connect_parts(force: false); $connect_parts($note_parts, $note_criteria_arr, force: false);
$connect_parts($actor_parts, $actor_criteria_arr, force: false);
} else { } else {
$last_op = $delimiter; $last_op = $delimiter;
} }
@ -92,10 +106,17 @@ abstract class Parser
} }
} }
if (!empty($parts)) { $note_criteria = $actor_criteria = null;
$connect_parts(force: true); if (!empty($note_parts)) {
$connect_parts($note_parts, $note_criteria_arr, force: true);
$note_criteria = new Criteria($eb->orX(...$note_criteria_arr));
} else {
if (!empty($actor_parts)) {
$connect_parts($actor_parts, $actor_criteria_arr, force: true);
$actor_criteria = new Criteria($eb->orX(...$actor_criteria_arr));
}
} }
return new Criteria($eb->orX(...$criteria)); return [$note_criteria, $actor_criteria];
} }
} }

View File

@ -1,7 +1,7 @@
{% for res in results %} {% for note in notes %}
{% if res is instanceof('App\\Entity\\Note') %} {% include '/cards/note/view.html.twig' with {'note': note} %}
{% include '/cards/note/view.html.twig' with {'note': res} %} {% endfor %}
{% else %}
{{ dump(res) }} {% for actor in actors %}
{% endif %} {% include 'actor/view.html.twig' with {'actor': actor} %}
{% endfor %} {% endfor %}

View File

@ -90,18 +90,36 @@ class Tag extends Component
return substr(Formatting::slugify($tag), 0, self::MAX_TAG_LENGTH); return substr(Formatting::slugify($tag), 0, self::MAX_TAG_LENGTH);
} }
public function onSearchCreateExpression(ExpressionBuilder $eb, string $term, &$expr) /**
* Populate $note_expr with an expression to match a tag, if the term looks like a tag
*
* $term /^(note|tag|people|actor)/ means we want to match only either a note or an actor
*
* @param mixed $note_expr
* @param mixed $actor_expr
*/
public function onSearchCreateExpression(ExpressionBuilder $eb, string $term, &$note_expr, &$actor_expr)
{ {
if (preg_match(self::TAG_REGEX, $term)) { $search_term = str_contains($term, ':#') ? explode(':', $term)[1] : $term;
$expr = $eb->eq('note_tag.tag', $term); $temp_note_expr = $eb->eq('note_tag.tag', $search_term);
return Event::stop; $temp_actor_expr = $eb->eq('actor_tag.tag', $search_term);
if (Formatting::startsWith($term, ['note', 'tag'])) {
$note_expr = $temp_note_expr;
} else { } else {
if (Formatting::startsWith($term, ['people', 'actor'])) {
$actor_expr = $temp_actor_expr;
} else {
$note_expr = $temp_note_expr;
$actor_expr = $temp_actor_expr;
return Event::next; return Event::next;
} }
} }
return Event::stop;
}
public function onSeachQueryAddJoins(QueryBuilder &$qb) public function onSeachQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb)
{ {
$qb->join('App\Entity\NoteTag', 'note_tag', Expr\Join::WITH, 'note_tag.note_id = note.id'); $note_qb->join('App\Entity\NoteTag', 'note_tag', Expr\Join::WITH, 'note_tag.note_id = note.id');
$actor_qb->join('App\Entity\ActorTag', 'actor_tag', Expr\Join::WITH, 'actor_tag.tagger = actor.id');
} }
} }