If your users are advanced enough to know the hierarchy of a query, any graphical interface you give them has to be fluid enough not to get in their way. I think an interface based on dragging and dropping elements to create an implicit hierarchy is ideal. Here's an extended annotated visual example of how a user might construct the query (A and B) or ((not C) or D):
Drop A onto panel.
+---+
| A |
+---+
Drop "and" after A.
+-------------------+
| +---+ +-----+ |
| | A | and | ... | |
| +---+ +-----+ |
+-------------------+
Drop B onto "...".
+-----------------+
| +---+ +---+ |
| | A | and | B | |
| +---+ +---+ |
+-----------------+
Drop "or" after "and".
+--------------------------------+
| +-----------------+ |
| | +---+ +---+ | +-----+ |
| | | A | and | B | | or | ... | |
| | +---+ +---+ | +-----+ |
| +-----------------+ |
+--------------------------------+
Drop C onto "...".
+------------------------------+
| +-----------------+ |
| | +---+ +---+ | +---+ |
| | | A | and | B | | or | C | |
| | +---+ +---+ | +---+ |
| +-----------------+ |
+------------------------------+
Drop "not" onto C.
+--------------------------------------+
| +-----------------+ +-----------+ |
| | +---+ +---+ | | +---+ | |
| | | A | and | B | | or | not | C | | |
| | +---+ +---+ | | +---+ | |
| +-----------------+ +-----------+ |
+--------------------------------------+
Drop "or" after "not C".
+-----------------------------------------------------+
| +--------------------------+ |
| +-----------------+ | +-----------+ | |
| | +---+ +---+ | | | +---+ | +-----+ | |
| | | A | and | B | | or | | not | C | | or | ... | | |
| | +---+ +---+ | | | +---+ | +-----+ | |
| +-----------------+ | +-----------+ | |
| +--------------------------+ |
+-----------------------------------------------------+
Drop D onto "...".
+---------------------------------------------------+
| +------------------------+ |
| +-----------------+ | +-----------+ | |
| | +---+ +---+ | | | +---+ | +---+ | |
| | | A | and | B | | or | | not | C | | or | D | | |
| | +---+ +---+ | | | +---+ | +---+ | |
| +-----------------+ | +-----------+ | |
| +------------------------+ |
+---------------------------------------------------+
Individual query elements (A, B, etc.) are constructed before being dropped into the panel, using combo boxes or whatever elements are necessary. Small margins and alternating colours could make this very readable, as well as display rules that make, for example, a chain of ors display at a single level:
+-------------------------+
| +---+ +---+ +---+ |
| | A | or | B | or | C | |
| +---+ +---+ +---+ |
+-------------------------+
Naturally, query elements could be expanded, collapsed, re-ordered, and edited after being dropped into the composing panel.
I'm not saying this is the easiest way to construct a system for this purpose. In fact, from a development standpoint, it's probably about as difficult as it can be. But it's the most efficient and intuitive thing I can come up with, and anyway it's basically just a clone of the Apple Mail rule UI but with more emphasis on hierarchy.
Hope this proves useful in your search for what's right.