| Akron | 9491d29 | 2017-11-30 16:14:31 +0100 | [diff] [blame] | 1 | use Test::More; |
| 2 | use Test::Krawfish; |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | use_ok('Krawfish::Koral'); |
| 7 | use_ok('Krawfish::Index'); |
| 8 | use_ok('Krawfish::Index::Segment'); |
| 9 | |
| 10 | ok(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 | |
| 15 | ok($index->introduce_field('id', 'NUM'), 'Introduce field as sortable'); |
| 16 | ok($index->introduce_field('author', 'DE'), 'Introduce field as sortable'); |
| 17 | |
| 18 | ok_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) |
| 28 | ok_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 |
| 39 | ok($index->segment(0)->commit, 'Commit to all segments'); |
| 40 | ok($index->segment(1)->commit, 'Commit to all segments'); |
| 41 | |
| 42 | my $koral = Krawfish::Koral->new; |
| 43 | my $qb = $koral->query_builder; |
| 44 | my $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 | |
| 65 | ok(my $cluster_q = $koral->to_query, ''); |
| 66 | |
| 67 | is($cluster_q->to_string, "fields('author':filter((aabb)|(aacc),[1]))", 'Stringification'); |
| 68 | |
| 69 | my $node_q = $cluster_q->identify($index->dict); |
| 70 | |
| 71 | is($node_q->to_string(1), "fields(#2:filter((#10#12)|(#10#14),[1]))", 'Stringification'); |
| 72 | |
| 73 | ok(my $seg_q_1 = $node_q->optimize($index->segment(0)), 'Run on index 1'); |
| 74 | ok(my $seg_q_2 = $node_q->optimize($index->segment(1)), 'Run on index 2'); |
| 75 | |
| Akron | d8d76e8 | 2017-11-30 16:31:46 +0100 | [diff] [blame^] | 76 | # No segment specific optimization |
| Akron | 9491d29 | 2017-11-30 16:14:31 +0100 | [diff] [blame] | 77 | is($seg_q_1->to_string(1), |
| Akron | d8d76e8 | 2017-11-30 16:31:46 +0100 | [diff] [blame^] | 78 | 'eFields(#2:or(constr(pos=2:#10,filter(#12,[1])),constr(pos=2:#10,filter(#14,[1]))))', |
| Akron | 9491d29 | 2017-11-30 16:14:31 +0100 | [diff] [blame] | 79 | 'Stringification 1'); |
| 80 | |
| Akron | d8d76e8 | 2017-11-30 16:31:46 +0100 | [diff] [blame^] | 81 | # There is no 'cc' in segment 1 - removed |
| Akron | 9491d29 | 2017-11-30 16:14:31 +0100 | [diff] [blame] | 82 | is($seg_q_2->to_string(1), |
| Akron | d8d76e8 | 2017-11-30 16:31:46 +0100 | [diff] [blame^] | 83 | 'eFields(#2:constr(pos=2:#10,filter(#12,[1])))', |
| Akron | 9491d29 | 2017-11-30 16:14:31 +0100 | [diff] [blame] | 84 | 'Stringification 2'); |
| 85 | |
| 86 | is($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 | |
| 90 | is($seg_q_2->compile->inflate($index->dict)->to_string, |
| 91 | "[matches=[0:0-2|fields:'author'='Michael']]", |
| 92 | 'Stringification'); |
| 93 | |
| 94 | |
| 95 | done_testing; |
| 96 | __END__ |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | done_testing; |
| 104 | __END__ |