blob: 7ea009cf7c794b0a43ba1d66de00e09accb5317d [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;
Akron373df822016-12-28 15:25:14 +010010ok_index($index, {
11 id => 2,
12 author => 'Peter',
13 genre => 'novel',
14 age => 4
15} => [qw/aa bb/], 'Add complex document');
16ok_index($index, {
17 id => 3,
18 author => 'Peter',
19 genre => 'novel',
20 age => 3
21} => [qw/aa bb/], 'Add complex document');
22ok_index($index, {
23 id => 5,
24 author => 'Peter',
25 genre => 'newsletter',
26 age => 4
27} => [qw/aa bb/], 'Add complex document');
28ok_index($index, {
29 id => 6,
30 author => 'Michael',
31 genre => 'newsletter',
32 age => 7
33} => [qw/aa bb/], 'Add complex document');
Akron349747d2016-12-05 11:05:53 +010034
35ok(my $cb = Krawfish::Koral::Corpus::Builder->new, 'Create CorpusBuilder');
36
37ok(my $query = $cb->field_and(
38 $cb->string('author')->eq('Peter'),
39 $cb->string('age')->eq('4')
40), 'Create corpus query');
41
Akron0e782bc2017-05-14 14:04:41 +020042is($query->to_string, 'age=4&author=Peter', 'Stringification');
Akron373df822016-12-28 15:25:14 +010043ok(!$query->is_negative, 'Check negativity');
Akron349747d2016-12-05 11:05:53 +010044
Akron2ea61aa2017-06-03 16:30:23 +020045ok(my $plan = $query->root_normalize->optimize($index), 'Planning');
Akron349747d2016-12-05 11:05:53 +010046
Akron0e782bc2017-05-14 14:04:41 +020047is($plan->to_string, "and('age:4','author:Peter')", 'Stringification');
Akron349747d2016-12-05 11:05:53 +010048
49ok($plan->next, 'Init vc');
50is($plan->current->to_string, '[0]', 'First doc');
51ok($plan->next, 'More next');
52is($plan->current->to_string, '[2]', 'First doc');
53ok(!$plan->next, 'No more next');
54
Akron349747d2016-12-05 11:05:53 +010055# Complex virtual corpus
56ok($query = $cb->field_or(
57 $cb->field_and(
58 $cb->string('author')->eq('Peter'),
59 $cb->string('age')->eq(3)
60 ),
61 $cb->string('id')->eq(2)
62), 'Create corpus query');
63
Akron0e782bc2017-05-14 14:04:41 +020064is($query->to_string, '(age=3&author=Peter)|id=2', 'Stringification');
Akron373df822016-12-28 15:25:14 +010065ok(!$query->is_negative, 'Check negativity');
Akron349747d2016-12-05 11:05:53 +010066
Akron2ea61aa2017-06-03 16:30:23 +020067ok($plan = $query->root_normalize->optimize($index), 'Planning');
Akron349747d2016-12-05 11:05:53 +010068
Akron0e782bc2017-05-14 14:04:41 +020069is($plan->to_string, "or(and('age:3','author:Peter'),'id:2')", 'Stringification');
Akron349747d2016-12-05 11:05:53 +010070
71ok($plan->next, 'Init vc');
72is($plan->current->to_string, '[0]', 'First doc');
73ok($plan->next, 'More next');
74is($plan->current->to_string, '[1]', 'First doc');
75ok(!$plan->next, 'No more next');
76
Akron373df822016-12-28 15:25:14 +010077# Complex virtual corpus with negation
78ok($query = $cb->field_and(
79 $cb->string('author')->eq('Peter'),
80 $cb->string('age')->ne(4)
81),
82, 'Create corpus query');
83
Akron0e782bc2017-05-14 14:04:41 +020084is($query->to_string, 'age!=4&author=Peter', 'Stringification');
Akron373df822016-12-28 15:25:14 +010085ok(!$query->is_negative, 'Check negativity');
86
Akron2ea61aa2017-06-03 16:30:23 +020087ok(my $norm = $query->root_normalize, 'Plan logically');
88is($norm->to_string, "author=Peter&!age=4", 'Stringification');
Akron373df822016-12-28 15:25:14 +010089
Akron2ea61aa2017-06-03 16:30:23 +020090ok(my $opt = $norm->optimize($index), 'Planning');
91is($opt->to_string, "andNot('author:Peter','age:4')", 'Stringification');
92
93
94
95ok($opt->next, 'Init vc');
96is($opt->current->to_string, '[1]', 'First doc');
97ok(!$opt->next, 'No more next');
Akron373df822016-12-28 15:25:14 +010098
99
100# Complex virtual corpus with negation
101ok($query = $cb->field_and(
102 $cb->string('author')->ne('Peter'),
103 $cb->string('age')->ne(4)
104),
105, 'Create corpus query');
106
Akron0e782bc2017-05-14 14:04:41 +0200107is($query->to_string, 'age!=4&author!=Peter', 'Stringification');
Akron2c6c7162017-05-15 18:15:33 +0200108ok(!$query->is_negative, 'Check negativity');
Akron2ea61aa2017-06-03 16:30:23 +0200109#ok($query->planned_tree, 'Plan the tree');
110#is($query->to_string, '!(age=4|author=Peter)', 'Planned tree stringification');
Akrond3355ba2017-05-17 21:16:35 +0200111
Akron2ea61aa2017-06-03 16:30:23 +0200112
113ok($plan = $query->root_normalize, 'Planning');
114is($plan->to_string, "[1]&!(age=4|author=Peter)", 'Stringification');
115
116ok($plan = $plan->optimize($index), 'Optimizing');
Akron0e782bc2017-05-14 14:04:41 +0200117is($plan->to_string, "not(or('age:4','author:Peter'))", 'Stringification');
Akron373df822016-12-28 15:25:14 +0100118
Akrond3355ba2017-05-17 21:16:35 +0200119done_testing;
120__END__
121
122
Akron2ea61aa2017-06-03 16:30:23 +0200123
124
125
Akron373df822016-12-28 15:25:14 +0100126ok($plan->next, 'More next');
127is($plan->current->to_string, '[3]', 'First doc');
128ok(!$plan->next, 'No more next');
129
130# Complex virtual corpus with negation
131ok($query = $cb->field_and(
132 $cb->string('genre')->eq('novel'),
133 $cb->string('author')->ne('Peter'),
134 $cb->string('age')->ne(4)
135),
136, 'Create corpus query');
137
Akron2ea61aa2017-06-03 16:30:23 +0200138done_testing;
139__END__
140
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__