diff --git a/service.inc b/service.inc
index 0b0a567..356214d 100644
--- a/service.inc
+++ b/service.inc
@@ -611,31 +611,47 @@ class SearchApiSolrService extends SearchApiAbstractService {
    *   A Solr query string representing the same keys.
    */
   protected function flattenKeys(array $keys) {
+    // Extract search terms.
     $k = array();
+
     foreach (element_children($keys) as $i) {
       $key = $keys[$i];
-      if (!$key) {
-        continue;
-      }
       if (is_array($key)) {
+        // Render nested queries.
         $k[] = $this->flattenKeys($key);
       }
-      else {
-        $key = trim($key);
-        $key = SearchApiSolrConnection::phrase($key);
-        $k[] = $key;
+      elseif ($key) {
+        // Escape keys.
+        $k[] = SearchApiSolrConnection::phrase(trim($key));
       }
     }
-    if (!$k) {
+
+    // Don't apply conjunction, negation, etc. to empty queries.
+    if (empty($k)) {
       return '';
     }
-    if ($keys['#conjunction'] == 'OR') {
-      $k = '((' . implode(') OR (', $k) . '))';
-      return empty($keys['#negation']) ? $k : '-' . $k;
+
+    // Note that our default query handler, "dismax", does not understand any
+    // special syntax beyond grouping terms as phrases ("") and making
+    // terms/phrases mandatory (+) or prohibited (-). It is quite possible that
+    // the only processing we should do here is negation, like this: -"$query"
+    // http://wiki.apache.org/solr/DisMaxQParserPlugin#Query_Syntax
+
+    // Apply the conjunction type.
+    if (!empty($keys['#conjunction']) && in_array($keys['#conjunction'], array('AND', 'OR'))) {
+      $conjunction = ') ' . $keys['#conjunction'] . ' (';
+      $query = '((' . implode($conjunction, $k) . '))';
+    }
+    else {
+      $query = implode(' ', $k);
+    }
+
+    // Apply negation, if requested.
+    if (!empty($keys['#negation'])) {
+      $query = '-(' . $query . ')';
     }
 
-    $k = implode(' ', $k);
-    return empty($keys['#negation']) ? $k : '-(' . $k . ')';
+    return $query;
   }
 
   /**
