blob: 3ce06133e655f77b63255650e70bfc20a2c21b44 [file] [log] [blame]
Akron9491d292017-11-30 16:14:31 +01001use Test::More;
2use Test::Krawfish;
3use strict;
4use warnings;
5
6use_ok('Krawfish::Koral');
7use_ok('Krawfish::Index');
8use_ok('Krawfish::Index::Segment');
9
10ok(my $index = Krawfish::Index->new(':temp1:'), 'Create new index object');
11
12# Add secondary segment
13$index->add_segment(Krawfish::Index::Segment->new(':temp:'));
14
15ok($index->introduce_field('id', 'NUM'), 'Introduce field as sortable');
16ok($index->introduce_field('author', 'DE'), 'Introduce field as sortable');
17
18ok_index_koral($index => 0, test_doc({
19 id => 2,
20 author => 'Peter',
21 genre => 'novel',
22 age => 4
23} => [qw/aa bb aa cc/]), 'Add document to segment 1');
24
25
26# Add documents to second index
27# (normally the second index is not dynamic, so this is just temporary)
28ok_index_koral($index => 1, test_doc({
29 id => 5,
30 author => 'Michael',
31 genre => 'newsletter',
32 title => 'Your new way to success!',
33 age => 7
34} => [qw/aa bb/]), 'Add document to segment 1');
35
36
37# Normally commiting only works on the first segment,
38# so this is temporary
39ok($index->segment(0)->commit, 'Commit to all segments');
40ok($index->segment(1)->commit, 'Commit to all segments');
41
42my $koral = Krawfish::Koral->new;
43my $qb = $koral->query_builder;
44my $mb = $koral->compilation_builder;
45
46$koral->query(
47 $qb->bool_or(
48 $qb->seq(
49 $qb->token('aa'),
50 $qb->token('bb')
51 ),
52 $qb->seq(
53 $qb->token('aa'),
54 $qb->token('cc')
55 )
56 )
57);
58
59$koral->compilation(
60 $mb->enrich(
61 $mb->e_fields(qw/author/)
62 )
63);
64
65ok(my $cluster_q = $koral->to_query, '');
66
67is($cluster_q->to_string, "fields('author':filter((aabb)|(aacc),[1]))", 'Stringification');
68
69my $node_q = $cluster_q->identify($index->dict);
70
71is($node_q->to_string(1), "fields(#2:filter((#10#12)|(#10#14),[1]))", 'Stringification');
72
73ok(my $seg_q_1 = $node_q->optimize($index->segment(0)), 'Run on index 1');
74ok(my $seg_q_2 = $node_q->optimize($index->segment(1)), 'Run on index 2');
75
Akrond8d76e82017-11-30 16:31:46 +010076# No segment specific optimization
Akron9491d292017-11-30 16:14:31 +010077is($seg_q_1->to_string(1),
Akrond8d76e82017-11-30 16:31:46 +010078 'eFields(#2:or(constr(pos=2:#10,filter(#12,[1])),constr(pos=2:#10,filter(#14,[1]))))',
Akron9491d292017-11-30 16:14:31 +010079 'Stringification 1');
80
Akrond8d76e82017-11-30 16:31:46 +010081# There is no 'cc' in segment 1 - removed
Akron9491d292017-11-30 16:14:31 +010082is($seg_q_2->to_string(1),
Akrond8d76e82017-11-30 16:31:46 +010083 'eFields(#2:constr(pos=2:#10,filter(#12,[1])))',
Akron9491d292017-11-30 16:14:31 +010084 'Stringification 2');
85
86is($seg_q_1->compile->inflate($index->dict)->to_string,
87 "[matches=[0:0-2|fields:'author'='Peter'][0:2-4|fields:'author'='Peter']]",
88 'Stringification');
89
90is($seg_q_2->compile->inflate($index->dict)->to_string,
91 "[matches=[0:0-2|fields:'author'='Michael']]",
92 'Stringification');
93
94
95done_testing;
96__END__
97
98
99
100
101
102
103done_testing;
104__END__