Filter fields
diff --git a/lib/Krawfish/Collection/Count.pm b/lib/Krawfish/Collection/Count.pm
index a776d54..a551833 100644
--- a/lib/Krawfish/Collection/Count.pm
+++ b/lib/Krawfish/Collection/Count.pm
@@ -24,11 +24,8 @@
# Get next item
if ($self->{query}->next) {
- # Get current posting
- my $current = $self->{query}->current;
-
- # Get current doc id
- my $doc_id = $current->doc_id;
+ # Get current posting doc_id
+ my $doc_id = $self->{query}->current->doc_id;
# Document is new
if (!defined($self->{doc_id}) || ($self->{doc_id} != $doc_id)) {
@@ -62,9 +59,10 @@
# TODO: Optimize
-sub to_end {
+sub finish {
my $self = shift;
while ($self->next) { };
+ return 1;
};
sub to_string {
diff --git a/lib/Krawfish/Collection/Fields.pm b/lib/Krawfish/Collection/Fields.pm
index fde4046..450f396 100644
--- a/lib/Krawfish/Collection/Fields.pm
+++ b/lib/Krawfish/Collection/Fields.pm
@@ -30,11 +30,19 @@
# Retrieve field data of the document
my $data = $fields->get($match->doc_id);
- # TODO:
# Filter fields!
+ if ($self->{fields}) {
+ my %fields;
+ foreach (@{$self->{fields}}) {
+ $fields{$_} = $data->{$_} if $data->{$_};
+ };
- # Add data to match
- $match->fields($data);
+ # Add data to match
+ $match->fields(\%fields);
+ }
+ else {
+ $match->fields($data);
+ };
$self->{match} = $match;
diff --git a/lib/Krawfish/Posting/Match.pm b/lib/Krawfish/Posting/Match.pm
index 7c8dd56..70957df 100644
--- a/lib/Krawfish/Posting/Match.pm
+++ b/lib/Krawfish/Posting/Match.pm
@@ -31,7 +31,7 @@
if ($self->payload->length) {
$str .= '$' . $self->payload->to_string;
};
- $str .= '=';
+ $str .= '|';
$str .= join ';', map {
$_ . '=' . _squote($self->{fields}->{$_})
} sort keys %{$self->{fields}};
diff --git a/t/collection/count.t b/t/collection/count.t
index 225f033..4d3605c 100644
--- a/t/collection/count.t
+++ b/t/collection/count.t
@@ -9,12 +9,20 @@
my $index = Krawfish::Index->new;
-ok(defined $index->add('t/data/doc1.jsonld'), 'Add new document');
-ok(defined $index->add('t/data/doc2.jsonld'), 'Add new document');
-ok(defined $index->add('t/data/doc3-segments.jsonld'), 'Add new document');
+ok_index($index, {
+ docID => 7
+} => [qw/aa bb/], 'Add complex document');
+ok_index($index, {
+ docID => 3,
+} => [qw/aa cc cc/], 'Add complex document');
+ok_index($index, {
+ docID => 1,
+} => [qw/aa bb/], 'Add complex document');
+
my $kq = Krawfish::Koral::Query::Builder->new;
-my $query = $kq->token('Der');
+
+my $query = $kq->token('bb');
# Get count object
ok(my $count = Krawfish::Collection::Count->new(
@@ -30,7 +38,23 @@
is($doc_freq, 2, 'Document frequency');
is($freq, 2, 'Occurrence frequency');
-is($count->to_string, "collectCounts('Der')", 'Get counts');
+is($count->to_string, "collectCounts('bb')", 'Get counts');
+
+
+$query = $kq->token('cc');
+
+# Get count object
+ok($count = Krawfish::Collection::Count->new(
+ $query->prepare_for($index)
+), 'Create count object');
+
+ok($count->finish, 'Finish');
+($doc_freq, $freq) = $count->frequencies;
+
+is($doc_freq, 1, 'Document frequency');
+is($freq, 2, 'Occurrence frequency');
+is($count->to_string, "collectCounts('cc')", 'Get counts');
+
done_testing;
__END__
diff --git a/t/collection/fields.t b/t/collection/fields.t
index 1345914..859202b 100644
--- a/t/collection/fields.t
+++ b/t/collection/fields.t
@@ -35,10 +35,10 @@
), 'Create count object');
ok($fields->next, 'Next');
-is($fields->current_match, "[0:0-1=corpus='corpus-2';docID='doc-1';license='free']",
+is($fields->current_match, "[0:0-1|corpus='corpus-2';license='free']",
'Current match');
ok($fields->next, 'Next');
-is($fields->current_match, "[1:0-1=corpus='corpus-3';docID='doc-2';license='closed']",
+is($fields->current_match, "[1:0-1|corpus='corpus-3';license='closed']",
'Current match');
ok(!$fields->next, 'No more next');
diff --git a/t/collection/snippet.t b/t/collection/snippet.t
index b1b3157..97a6db6 100644
--- a/t/collection/snippet.t
+++ b/t/collection/snippet.t
@@ -30,13 +30,13 @@
), 'Create count object');
ok($snippet->next, 'Next match');
-is($snippet->current_match->to_string, "[0:0-1=snippet='aa bb aa bb']", 'Current match');
+is($snippet->current_match->to_string, "[0:0-1|snippet='aa bb aa bb']", 'Current match');
ok($snippet->next, 'Next match');
-is($snippet->current_match->to_string, "[0:1-2=snippet='aa bb aa bb']", 'Current match');
+is($snippet->current_match->to_string, "[0:1-2|snippet='aa bb aa bb']", 'Current match');
ok($snippet->next, 'Next match');
-is($snippet->current_match->to_string, "[0:2-3=snippet='aa bb aa bb']", 'Current match');
+is($snippet->current_match->to_string, "[0:2-3|snippet='aa bb aa bb']", 'Current match');
ok($snippet->next, 'Next match');
-is($snippet->current_match->to_string, "[0:3-4=snippet='aa bb aa bb']", 'Current match');
+is($snippet->current_match->to_string, "[0:3-4|snippet='aa bb aa bb']", 'Current match');
ok(!$snippet->next, 'No more match');
diag 'Test further - with matches';
diff --git a/t/collection/sort.t b/t/collection/sort.t
index 39d62b5..8599279 100644
--- a/t/collection/sort.t
+++ b/t/collection/sort.t
@@ -10,16 +10,20 @@
my $index = Krawfish::Index->new;
ok_index($index, {
- docID => 7
+ docID => 7,
+ author => 'Carol'
} => [qw/aa bb/], 'Add complex document');
ok_index($index, {
docID => 3,
-} => [qw/aa bb/], 'Add complex document');
+ author => 'Arthur'
+} => [qw/aa bb cc/], 'Add complex document');
ok_index($index, {
docID => 1,
-} => [qw/aa bb/], 'Add complex document');
+ author => 'Bob'
+} => [qw/aa bb cc/], 'Add complex document');
my $kq = Krawfish::Koral::Query::Builder->new;
+
my $query = $kq->term_or('aa', 'bb');
# Get sort object
@@ -34,13 +38,6 @@
is($sort->current->doc_id, 2, 'Obj');
ok($sort->next, 'Next');
is($sort->current->doc_id, 2, 'Obj');
-
-is($sort->to_string, "collectSorted(['docID']:or('aa','bb'))", 'Get counts');
-
-done_testing;
-__END__
-
-
ok($sort->next, 'Next');
is($sort->current->doc_id, 1, 'Obj');
ok($sort->next, 'Next');
@@ -51,3 +48,30 @@
is($sort->current->doc_id, 0, 'Obj');
ok(!$sort->next, 'No more nexts');
+is($sort->to_string, "collectSorted(['docID']:or('aa','bb'))", 'Get counts');
+
+
+$query = $kq->term('cc');
+
+# Get sort object
+ok($sort = Krawfish::Collection::Sort->new(
+ $query->prepare_for($index),
+ $index,
+ ['author']
+), 'Create sort object');
+
+is($sort->freq, 2, 'List has frequency');
+ok($sort->next, 'Next');
+is($sort->current->doc_id, 1, 'Obj');
+ok($sort->next, 'Next');
+is($sort->current->doc_id, 2, 'Obj');
+ok(!$sort->next, 'No more nexts');
+
+is($sort->to_string, "collectSorted(['author']:'cc')", 'Get counts');
+
+
+done_testing;
+__END__
+
+
+