blob: 3d2889dc1642ef72611ceabb92a8523970db0b12 [file] [log] [blame]
Akron349747d2016-12-05 11:05:53 +01001use Test::More;
2use Test::Krawfish;
3use strict;
4use warnings;
5
6use_ok('Krawfish::Koral::Corpus::Builder');
7use_ok('Krawfish::Index');
8
9my $index = Krawfish::Index->new;
Akron56422cf2017-08-16 14:17:01 +020010ok_index($index, {
Akron92d9f672017-08-16 12:23:11 +020011 integer_id => 2,
Akron373df822016-12-28 15:25:14 +010012 author => 'Peter',
13 genre => 'novel',
Akron92d9f672017-08-16 12:23:11 +020014 integer_age => 4
Akron373df822016-12-28 15:25:14 +010015} => [qw/aa bb/], 'Add complex document');
Akron7b4e4d92017-09-25 12:18:29 +020016
Akron56422cf2017-08-16 14:17:01 +020017ok_index($index, {
Akron92d9f672017-08-16 12:23:11 +020018 integer_id => 3,
Akron373df822016-12-28 15:25:14 +010019 author => 'Peter',
20 genre => 'novel',
Akron92d9f672017-08-16 12:23:11 +020021 integer_age => 3
Akron373df822016-12-28 15:25:14 +010022} => [qw/aa bb/], 'Add complex document');
Akron56422cf2017-08-16 14:17:01 +020023ok_index($index, {
Akron92d9f672017-08-16 12:23:11 +020024 integer_id => 5,
Akron373df822016-12-28 15:25:14 +010025 author => 'Peter',
26 genre => 'newsletter',
Akron92d9f672017-08-16 12:23:11 +020027 integer_age => 4
Akron373df822016-12-28 15:25:14 +010028} => [qw/aa bb/], 'Add complex document');
Akron56422cf2017-08-16 14:17:01 +020029ok_index($index, {
Akron92d9f672017-08-16 12:23:11 +020030 integer_id => 6,
Akron373df822016-12-28 15:25:14 +010031 author => 'Michael',
32 genre => 'newsletter',
Akron92d9f672017-08-16 12:23:11 +020033 integer_age => 7
Akron373df822016-12-28 15:25:14 +010034} => [qw/aa bb/], 'Add complex document');
Akron349747d2016-12-05 11:05:53 +010035
36ok(my $cb = Krawfish::Koral::Corpus::Builder->new, 'Create CorpusBuilder');
37
Akronb945c572017-07-23 14:55:00 +020038ok(my $query = $cb->bool_and(
Akron349747d2016-12-05 11:05:53 +010039 $cb->string('author')->eq('Peter'),
40 $cb->string('age')->eq('4')
41), 'Create corpus query');
42
Akron0e782bc2017-05-14 14:04:41 +020043is($query->to_string, 'age=4&author=Peter', 'Stringification');
Akron373df822016-12-28 15:25:14 +010044ok(!$query->is_negative, 'Check negativity');
Akron349747d2016-12-05 11:05:53 +010045
Akron7b4e4d92017-09-25 12:18:29 +020046
Akron3ab2e972017-08-02 19:10:10 +020047ok(my $plan = $query->normalize->identify($index->dict)->optimize($index->segment), 'Planning');
Akron349747d2016-12-05 11:05:53 +010048
Akron92d9f672017-08-16 12:23:11 +020049is($plan->to_string, "and(#2,#6)", 'Stringification');
Akron349747d2016-12-05 11:05:53 +010050
51ok($plan->next, 'Init vc');
52is($plan->current->to_string, '[0]', 'First doc');
53ok($plan->next, 'More next');
54is($plan->current->to_string, '[2]', 'First doc');
55ok(!$plan->next, 'No more next');
56
Akron349747d2016-12-05 11:05:53 +010057# Complex virtual corpus
Akronb945c572017-07-23 14:55:00 +020058ok($query = $cb->bool_or(
59 $cb->bool_and(
Akron349747d2016-12-05 11:05:53 +010060 $cb->string('author')->eq('Peter'),
61 $cb->string('age')->eq(3)
62 ),
63 $cb->string('id')->eq(2)
64), 'Create corpus query');
65
Akron0e782bc2017-05-14 14:04:41 +020066is($query->to_string, '(age=3&author=Peter)|id=2', 'Stringification');
Akron373df822016-12-28 15:25:14 +010067ok(!$query->is_negative, 'Check negativity');
Akron349747d2016-12-05 11:05:53 +010068
Akron3ab2e972017-08-02 19:10:10 +020069ok($plan = $query->normalize->identify($index->dict)->optimize($index->segment), 'Planning');
Akron349747d2016-12-05 11:05:53 +010070
Akron92d9f672017-08-16 12:23:11 +020071is($plan->to_string, "or(#8,and(#2,#13))", 'Stringification');
Akron349747d2016-12-05 11:05:53 +010072
73ok($plan->next, 'Init vc');
74is($plan->current->to_string, '[0]', 'First doc');
75ok($plan->next, 'More next');
76is($plan->current->to_string, '[1]', 'First doc');
77ok(!$plan->next, 'No more next');
78
Akron373df822016-12-28 15:25:14 +010079# Complex virtual corpus with negation
Akronb945c572017-07-23 14:55:00 +020080ok($query = $cb->bool_and(
Akron373df822016-12-28 15:25:14 +010081 $cb->string('author')->eq('Peter'),
82 $cb->string('age')->ne(4)
83),
84, 'Create corpus query');
85
Akronb3e17982017-07-19 17:45:50 +020086
Akron0e782bc2017-05-14 14:04:41 +020087is($query->to_string, 'age!=4&author=Peter', 'Stringification');
Akron373df822016-12-28 15:25:14 +010088ok(!$query->is_negative, 'Check negativity');
89
Akrond7f5bd32017-06-06 16:13:11 +020090ok(my $norm = $query->normalize, 'Plan logically');
Akron7a97d3f2017-06-07 18:28:50 +020091is($norm->to_string, "(author=Peter&!age=4)", 'Stringification');
Akron373df822016-12-28 15:25:14 +010092
Akron3ab2e972017-08-02 19:10:10 +020093ok(my $opt = $norm->identify($index->dict)->optimize($index->segment), 'Planning');
Akron92d9f672017-08-16 12:23:11 +020094is($opt->to_string, "andNot(#2,#6)", 'Stringification');
Akron2ea61aa2017-06-03 16:30:23 +020095
96
Akron2ea61aa2017-06-03 16:30:23 +020097ok($opt->next, 'Init vc');
98is($opt->current->to_string, '[1]', 'First doc');
99ok(!$opt->next, 'No more next');
Akron373df822016-12-28 15:25:14 +0100100
101
102# Complex virtual corpus with negation
Akronb945c572017-07-23 14:55:00 +0200103ok($query = $cb->bool_and(
Akron373df822016-12-28 15:25:14 +0100104 $cb->string('author')->ne('Peter'),
105 $cb->string('age')->ne(4)
106),
107, 'Create corpus query');
108
Akron0e782bc2017-05-14 14:04:41 +0200109is($query->to_string, 'age!=4&author!=Peter', 'Stringification');
Akron2c6c7162017-05-15 18:15:33 +0200110ok(!$query->is_negative, 'Check negativity');
Akrond3355ba2017-05-17 21:16:35 +0200111
Akron2ea61aa2017-06-03 16:30:23 +0200112
Akron1811acc2017-06-07 02:13:16 +0200113# Plan a query and finalize it
Akrond7f5bd32017-06-06 16:13:11 +0200114ok($plan = $query->normalize, 'Planning');
115is($plan->to_string, "!(age=4|author=Peter)", 'Stringification');
116ok($plan = $plan->finalize, 'Planning');
Akron7a97d3f2017-06-07 18:28:50 +0200117is($plan->to_string, "([1]&!(age=4|author=Peter))", 'Stringification');
Akron3ab2e972017-08-02 19:10:10 +0200118ok($plan = $plan->identify($index->dict)->optimize($index->segment), 'Optimizing');
Akron92d9f672017-08-16 12:23:11 +0200119is($plan->to_string, "andNot([1],or(#6,#2))", 'Stringification');
Akrond7f5bd32017-06-06 16:13:11 +0200120
Akron1811acc2017-06-07 02:13:16 +0200121ok($plan->next, 'More next');
122is($plan->current->to_string, '[3]', 'First doc');
123ok(!$plan->next, 'No more next');
Akrond7f5bd32017-06-06 16:13:11 +0200124
Akron373df822016-12-28 15:25:14 +0100125
Akrond3355ba2017-05-17 21:16:35 +0200126done_testing;
127__END__
128
129
Akron2ea61aa2017-06-03 16:30:23 +0200130
131
132
Akron373df822016-12-28 15:25:14 +0100133
134# Complex virtual corpus with negation
Akronb945c572017-07-23 14:55:00 +0200135ok($query = $cb->bool_and(
Akron373df822016-12-28 15:25:14 +0100136 $cb->string('genre')->eq('novel'),
137 $cb->string('author')->ne('Peter'),
138 $cb->string('age')->ne(4)
139),
140, 'Create corpus query');
141
Akron0e782bc2017-05-14 14:04:41 +0200142ok(!$query->has_classes, 'Contains classes');
143
144is($query->to_string, 'age!=4&author!=Peter&genre=novel', 'Stringification');
Akron373df822016-12-28 15:25:14 +0100145ok(!$query->is_negative, 'Check negativity');
146ok($plan = $query->plan_for($index), 'Planning');
Akron0e782bc2017-05-14 14:04:41 +0200147is($plan->to_string, "without('genre:novel',or('age:4','author:Peter'))",
Akron373df822016-12-28 15:25:14 +0100148 'Stringification');
149
Akronf9260ac2017-05-12 22:29:34 +0200150diag 'Test further';
151
152# Especially:
153# - First operand is negative, second is positive
154# etc.
155# - First operands have freq=0, first valid is negative
156
Akron349747d2016-12-05 11:05:53 +0100157
158done_testing;
159__END__