Fixed doc_id handling in current of And queries
diff --git a/lib/Krawfish/Corpus/And.pm b/lib/Krawfish/Corpus/And.pm
index 562ac4b..705a0bd 100644
--- a/lib/Krawfish/Corpus/And.pm
+++ b/lib/Krawfish/Corpus/And.pm
@@ -13,7 +13,8 @@
   my $class = shift;
   bless {
     first => shift,
-    second => shift
+    second => shift,
+    doc_id => undef
   }, $class;
 };
 
@@ -34,7 +35,10 @@
   my $first = $self->{first}->current;
   my $second = $self->{second}->current;
 
-  return unless $first || $second;
+  unless ($first || $second) {
+     $self->{doc_id} = undef;
+     return;
+  };
 
   while ($first && $second) {
 
@@ -69,7 +73,7 @@
   };
 
   $self->{doc_id} = undef;
-  return 0;
+  return;
 };
 
 
diff --git a/lib/Krawfish/Koral/Corpus.pm b/lib/Krawfish/Koral/Corpus.pm
index f6a978a..5eaf1f8 100644
--- a/lib/Krawfish/Koral/Corpus.pm
+++ b/lib/Krawfish/Koral/Corpus.pm
@@ -6,7 +6,7 @@
 use strict;
 use warnings;
 
-use constant DEBUG => 0;
+use constant DEBUG => 1;
 
 # Creation of virtual corpus
 
@@ -25,32 +25,59 @@
 # Query Planning methods and attributes #
 #########################################
 
-sub prepare_for {
+sub plan_for {
+  my ($self, $index) = @_;
+  $self
+    ->normalize
+    ->finalize
+    ->refer
+    ->inflate
+    ->cache
+    ->optimize($index);
+};
+
+
+# This will remove classes
+# in subcorpora
+sub plan_without_classes_for {
+  warn 'Not yet implemented';
   shift->plan_for(@_);
 };
 
 
-# Rewrite query to actual query
-sub plan_for {
-  warn 'DEPRECATED'
-};
-
-
+# Normalize the query
 sub normalize {
   $_[0];
 };
 
-sub memoize {
+
+# Refer to common subqueries
+sub refer {
   $_[0];
 };
 
+
+# Expand regular expressions ...
+sub inflate {
+  $_[0];
+};
+
+
+# Check for cached subqueries
+sub cache {
+  $_[0];
+};
+
+
+# Optimize for an index
 sub optimize;
 
+
 # Normalize to be on the root
 sub finalize {
   my $self = shift;
 
-  print_log('kq_corpus', 'Finalize tree') if DEBUG;
+  print_log('kq_corpus', 'Finalize tree or field') if DEBUG;
 
   if ($self->is_negative) {
 
@@ -58,12 +85,16 @@
 
     # Toggle negativity
     $self->is_negative(0);
+
+  print_log('kq_corpus', 'Do an "andNot" on any') if DEBUG;
     return $self->builder->field_and_not(
       $self->builder->any,
       $self
     );
   }
 
+  print_log('kq_corpus', 'Do an "and" on any') if DEBUG;
+
   return $self->builder->field_and(
     $self->builder->any,
     $self
@@ -79,13 +110,6 @@
 };
 
 
-# This will remove classes
-# in subcorpora
-sub plan_without_classes_for {
-  warn 'Not yet implemented';
-  shift->plan_for(@_);
-};
-
 
 sub is_negative {
   my $self = shift;
diff --git a/lib/Krawfish/Koral/Corpus/AndNot.pm b/lib/Krawfish/Koral/Corpus/AndNot.pm
index daeceef..fce1664 100644
--- a/lib/Krawfish/Koral/Corpus/AndNot.pm
+++ b/lib/Krawfish/Koral/Corpus/AndNot.pm
@@ -5,7 +5,7 @@
 use strict;
 use warnings;
 
-use constant DEBUG => 0;
+use constant DEBUG => 1;
 
 sub new {
   my $class = shift;
@@ -97,40 +97,4 @@
 };
 
 
-
-
-
-
-
-sub plan_for {
-
-  warn 'DEPRECATED';
-  my ($self, $index) = @_;
-  my $pos = $self->{pos};
-  my $neg = $self->{neg};
-
-  if (DEBUG) {
-    print_log('kq_andnot', 'Plan andnot') if DEBUG;
-  };
-
-  # Get the positive query
-  my $pos_query = $pos->plan_for($index);
-
-  if ($pos_query->freq == 0) {
-    return Krawfish::Query::Nothing->new;
-  };
-
-  # Get the negative query
-  my $neg_query = $neg->plan_for($index);
-
-  if ($neg_query->freq == 0) {
-    return $pos_query;
-  };
-
-  return Krawfish::Corpus::Without->new($pos_query, $neg_query);
-};
-
-
-
-
 1;
diff --git a/lib/Krawfish/Koral/Corpus/Cache.pm b/lib/Krawfish/Koral/Corpus/Cache.pm
index 39812d6..1a56a9d 100644
--- a/lib/Krawfish/Koral/Corpus/Cache.pm
+++ b/lib/Krawfish/Koral/Corpus/Cache.pm
@@ -9,7 +9,7 @@
 sub new {
   my $class = shift;
   bless {
-    span => shift
+    corpus => shift
   }, $class;
 };
 
@@ -17,12 +17,13 @@
   'cache';
 };
 
-sub plan_for {
+
+sub optimize {
   my ($self, $index) = @_;
 
   my $query;
-  unless ($query = $self->{span}->plan_for($index)) {
-    $self->copy_info_from($self->{span});
+  unless ($query = $self->{corpus}->plan_for($index)) {
+    $self->copy_info_from($self->{corpus});
     return;
   };
 
@@ -35,12 +36,12 @@
 
 
 sub to_koral_fragment {
-  return $_[0]->{span}->to_koral_fragment;
+  return $_[0]->{corpus}->to_koral_fragment;
 };
 
 
 sub to_string {
-  return $_[0]->{span}->to_string;
+  return $_[0]->{corpus}->to_string;
 };
 
 
diff --git a/lib/Krawfish/Koral/Corpus/Class.pm b/lib/Krawfish/Koral/Corpus/Class.pm
index 3e5f811..e91a697 100644
--- a/lib/Krawfish/Koral/Corpus/Class.pm
+++ b/lib/Krawfish/Koral/Corpus/Class.pm
@@ -45,6 +45,7 @@
   $self;
 };
 
+
 sub optimize {
   my ($self, $index) = @_;
 
@@ -56,24 +57,6 @@
     $corpus,
     $self->number
   );
-
-};
-
-
-
-sub plan_for {
-  my ($self, $index) = @_;
-
-  warn 'DEPRECATED!';
-
-  # Plan corpus
-  my $corpus = $self->operand->plan_for($index);
-
-  # Create class query
-  return Krawfish::Corpus::Class->new(
-    $corpus,
-    $self->number
-  );
 };
 
 
diff --git a/lib/Krawfish/Koral/Corpus/Field.pm b/lib/Krawfish/Koral/Corpus/Field.pm
index d2dd6e6..43c0be1 100644
--- a/lib/Krawfish/Koral/Corpus/Field.pm
+++ b/lib/Krawfish/Koral/Corpus/Field.pm
@@ -5,6 +5,8 @@
 use strict;
 use warnings;
 
+use constant DEBUG => 1;
+
 # TODO:
 #   - Check for valid parameters
 #   - Only support positive terms
@@ -114,32 +116,23 @@
 sub optimize {
   my ($self, $index) = @_;
 
-  # Positive field
-  Krawfish::Corpus::Field->new(
-    $index,
-    $self->to_term
-  );
-};
-
-sub plan_for {
-  my ($self, $index) = @_;
-
   # Negative field
   if ($self->is_negative) {
-    return Krawfish::Corpus::Negation->new(
-      $index,
-      Krawfish::Corpus::Field->new(
-        $index,
-        $self->to_term
-      )
-    );
+    warn 'Fields are not allowed to be negative';
+    return;
   };
 
   # Positive field
-  Krawfish::Corpus::Field->new(
+  my $query = Krawfish::Corpus::Field->new(
     $index,
     $self->to_term
   );
+
+  if ($query->freq == 0) {
+    return Krawfish::Query::Nothing->new;
+  };
+
+  return $query;
 };
 
 
diff --git a/lib/Krawfish/Koral/Corpus/FieldGroup.pm b/lib/Krawfish/Koral/Corpus/FieldGroup.pm
index 7a6d397..67a4416 100644
--- a/lib/Krawfish/Koral/Corpus/FieldGroup.pm
+++ b/lib/Krawfish/Koral/Corpus/FieldGroup.pm
@@ -120,6 +120,8 @@
   my $i = 0;
   my $first = $ops->[$i];
 
+  print_log('kq_fgroup', 'Initial query is ' . $self->to_string) if DEBUG;
+
   my $query = $first->optimize($index);
   $i++;
 
@@ -166,6 +168,11 @@
           $query,
           $next
         );
+      }
+
+      # One operand is not existing
+      else {
+        return Krawfish::Query::Nothing->new;
       };
     };
   }
@@ -228,6 +235,10 @@
 };
 
 
+1;
+
+
+__END__
 
 # Deprecated
 sub plan_for {
@@ -445,5 +456,3 @@
 
   return $query;
 };
-
-1;
diff --git a/lib/Krawfish/Koral/Query/Term.pm b/lib/Krawfish/Koral/Query/Term.pm
index e71b52f..236b9d3 100644
--- a/lib/Krawfish/Koral/Query/Term.pm
+++ b/lib/Krawfish/Koral/Query/Term.pm
@@ -13,7 +13,7 @@
 # TODO: Term building should be part of
 #   a utility class Krawfish::Util::Koral::Term or so
 
-use constant DEBUG => 0;
+use constant DEBUG => 1;
 
 sub new {
   my $class = shift;
@@ -148,6 +148,7 @@
   $_[0]->[5];
 };
 
+
 # Value of the term
 sub value {
   if ($_[1]) {
@@ -157,6 +158,7 @@
   $_[0]->[6];
 };
 
+
 sub filter_by {
   if ($_[1]) {
     $_[0]->[7] = $_[1];
@@ -165,10 +167,12 @@
   $_[0]->[7];
 };
 
+
 sub is_regex {
   return index($_[0]->match, '~') == -1 ? 0 : 1;
 };
 
+
 # Create koral fragment
 sub to_koral_fragment {
   my $self = shift;
@@ -192,11 +196,13 @@
   return $hash;
 };
 
+
 # TODO:
 #   Support fragment, where a term string
 #   may end with / or = to be used for
 #   suggestions
 
+
 # stringify term
 sub to_string {
   my ($self, $fragment) = @_;
@@ -257,6 +263,7 @@
   return $str . $term;
 };
 
+
 sub to_term_escaped {
   my $self = shift;
   my $term = $self->to_term;
@@ -266,6 +273,7 @@
   return $term;
 };
 
+
 sub plan_for {
   my $self = shift;
   my $index = shift;
@@ -281,7 +289,7 @@
 
     if (DEBUG) {
       print_log('regex', 'Expand /^' . $term . '$/');
-      print_log('regex', 'to ' . substr(join(',',@terms),0,50));
+      print_log('regex', 'to ' . substr(join(',', @terms), 0, 50));
     };
 
     return $self->builder->nothing unless @terms;
@@ -303,8 +311,12 @@
   # Term is filtered
   if ($self->filter_by) {
 
+    print_log('kq_term', 'Apply the term filter on ' . $self->filter_by->to_string) if DEBUG;
+
     my $filter = $self->filter_by->plan_for($index);
 
+    print_log('kq_term', 'Filter serialization is ' . $filter->to_string) if DEBUG;
+
     # Filter is empty
     return $self->builder->nothing if $filter->freq == 0;