Tag search

by Martin Seiler (modified: 2007 Mar 08)

You might have noticed the new link Tag search which directs you to a new page where you are asked to enter a query.

Julian and I have implemented a simple tag query language to be able to perform more complex searches over the tags: tcp -udp This query will return all content which has the tag "tcp" but is not tagged with "udp" yielding in content only related to the TCP.

Here is another query simply to show you some other syntax: tcp xor not tcp

The features so far are:

  • Simple but powerful syntax
  • Auto completion
  • Highlighted error messages


After little success with PHP backends for lemon and JLex (the generated code did not work) we used the generators (lexer, parser) from the PEAR library.

The next hurdle was the code generation for MySQL. First we translate the strings into integer IDs to get faster query execution.This was the easy part.

The table layout was predefined by the taxonomy module. The first attempt was using SQL UNION and INTERSECT set operations to implement the boolean and, or and xor. It turned out, that MySQL up to version 5 does not support INTERSECT.

So only UNION could be used for the or. I decided to implement the and by doing a join and then using the WHERE clause "t1='term1' and t2='term2'". After that it took a long while to figure out the proper SQL-Slang for MySQL.

At some places it was necessary to do something likeSELECT * FROM (SELECT ... real query ...) AS a_namein order to have no unnamed sub-queries. The error message was really bad but Google (once again) knew the answer :-)

After that we decided to add some sugar. This includes highlighting the parts of an erroneous query and, even more important, a powerful auto-completion using AJAX functionality built into Drupal. Julian said its implementation worked like a charm.

I hope you enjoy it!