5 use OpenSRF::Utils::JSON;
26 return QueryParser::Canonicalize::abstract_query2str_impl(
27 $self->parse_tree->to_abstract_query(@_)
32 sub facet_class_count {
34 return @{$self->facet_classes};
37 sub search_class_count {
39 return @{$self->search_classes};
44 return @{$self->filters};
49 return @{$self->modifiers};
54 $class = ref($class) || $class;
56 $parser_config{$class}{custom_data} ||= {};
57 return $parser_config{$class}{custom_data};
62 $class = ref($class) || $class;
64 $parser_config{$class}{operators} ||= {};
65 return $parser_config{$class}{operators};
70 $class = ref($class) || $class;
72 $parser_config{$class}{filters} ||= [];
73 return $parser_config{$class}{filters};
76 sub filter_callbacks {
78 $class = ref($class) || $class;
80 $parser_config{$class}{filter_callbacks} ||= {};
81 return $parser_config{$class}{filter_callbacks};
86 $class = ref($class) || $class;
88 $parser_config{$class}{modifiers} ||= [];
89 return $parser_config{$class}{modifiers};
94 $class = ref($class) || $class;
98 my $self = bless {} => $class;
100 for my $o (keys %{QueryParser->operators}) {
101 $class->operator($o => QueryParser->operator($o)) unless ($class->operator($o));
104 for my $opt ( keys %opts) {
105 $self->$opt( $opts{$opt} ) if ($self->can($opt));
113 my $pkg = ref($self) || $self;
114 return do{$pkg.'::query_plan'}->new( QueryParser => $self, @_ );
117 sub add_search_filter {
119 $pkg = ref($pkg) || $pkg;
121 my $callback = shift;
123 return $filter if (grep { $_ eq $filter } @{$pkg->filters});
124 push @{$pkg->filters}, $filter;
125 $pkg->filter_callbacks->{$filter} = $callback if ($callback);
129 sub add_search_modifier {
131 $pkg = ref($pkg) || $pkg;
132 my $modifier = shift;
134 return $modifier if (grep { $_ eq $modifier } @{$pkg->modifiers});
135 push @{$pkg->modifiers}, $modifier;
139 sub add_facet_class {
141 $pkg = ref($pkg) || $pkg;
144 return $class if (grep { $_ eq $class } @{$pkg->facet_classes});
146 push @{$pkg->facet_classes}, $class;
147 $pkg->facet_fields->{$class} = [];
152 sub add_search_class {
154 $pkg = ref($pkg) || $pkg;
157 return $class if (grep { $_ eq $class } @{$pkg->search_classes});
159 push @{$pkg->search_classes}, $class;
160 $pkg->search_fields->{$class} = [];
161 $pkg->default_search_class( $pkg->search_classes->[0] ) if (@{$pkg->search_classes} == 1);
168 $class = ref($class) || $class;
172 return undef unless ($opname);
174 $parser_config{$class}{operators} ||= {};
175 $parser_config{$class}{operators}{$opname} = $op if ($op);
177 return $parser_config{$class}{operators}{$opname};
182 $class = ref($class) || $class;
185 $parser_config{$class}{facet_classes} ||= [];
186 $parser_config{$class}{facet_classes} = $classes if (ref($classes) && @$classes);
187 return $parser_config{$class}{facet_classes};
192 $class = ref($class) || $class;
195 $parser_config{$class}{classes} ||= [];
196 $parser_config{$class}{classes} = $classes if (ref($classes) && @$classes);
197 return $parser_config{$class}{classes};
200 sub add_query_normalizer {
202 $pkg = ref($pkg) || $pkg;
206 my $params = shift || [];
208 # do not add if function AND params are identical to existing member
209 return $func if (grep {
210 $_->{function} eq $func and
211 OpenSRF::Utils::JSON->perl2JSON($_->{params}) eq OpenSRF::Utils::JSON->perl2JSON($params)
212 } @{$pkg->query_normalizers->{$class}->{$field}});
214 push(@{$pkg->query_normalizers->{$class}->{$field}}, { function => $func, params => $params });
219 sub query_normalizers {
221 $pkg = ref($pkg) || $pkg;
226 $parser_config{$pkg}{normalizers} ||= {};
229 $parser_config{$pkg}{normalizers}{$class}{$field} ||= [];
230 return $parser_config{$pkg}{normalizers}{$class}{$field};
232 return $parser_config{$pkg}{normalizers}{$class};
236 return $parser_config{$pkg}{normalizers};
239 sub add_filter_normalizer {
241 $pkg = ref($pkg) || $pkg;
244 my $params = shift || [];
246 return $func if (grep { $_ eq $func } @{$pkg->filter_normalizers->{$filter}});
248 push(@{$pkg->filter_normalizers->{$filter}}, { function => $func, params => $params });
253 sub filter_normalizers {
255 $pkg = ref($pkg) || $pkg;
259 $parser_config{$pkg}{filter_normalizers} ||= {};
261 $parser_config{$pkg}{filter_normalizers}{$filter} ||= [];
262 return $parser_config{$pkg}{filter_normalizers}{$filter};
265 return $parser_config{$pkg}{filter_normalizers};
268 sub default_search_class {
270 $pkg = ref($pkg) || $pkg;
272 $QueryParser::parser_config{$pkg}{default_class} = $pkg->add_search_class( $class ) if $class;
274 return $QueryParser::parser_config{$pkg}{default_class};
277 sub remove_facet_class {
279 $pkg = ref($pkg) || $pkg;
282 return $class if (!grep { $_ eq $class } @{$pkg->facet_classes});
284 $pkg->facet_classes( [ grep { $_ ne $class } @{$pkg->facet_classes} ] );
285 delete $QueryParser::parser_config{$pkg}{facet_fields}{$class};
290 sub remove_search_class {
292 $pkg = ref($pkg) || $pkg;
295 return $class if (!grep { $_ eq $class } @{$pkg->search_classes});
297 $pkg->search_classes( [ grep { $_ ne $class } @{$pkg->search_classes} ] );
298 delete $QueryParser::parser_config{$pkg}{fields}{$class};
303 sub add_facet_field {
305 $pkg = ref($pkg) || $pkg;
309 $pkg->add_facet_class( $class );
311 return { $class => $field } if (grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
313 push @{$pkg->facet_fields->{$class}}, $field;
315 return { $class => $field };
320 $class = ref($class) || $class;
322 $parser_config{$class}{facet_fields} ||= {};
323 return $parser_config{$class}{facet_fields};
326 sub add_search_field {
328 $pkg = ref($pkg) || $pkg;
332 $pkg->add_search_class( $class );
334 return { $class => $field } if (grep { $_ eq $field } @{$pkg->search_fields->{$class}});
336 push @{$pkg->search_fields->{$class}}, $field;
338 return { $class => $field };
343 $class = ref($class) || $class;
345 $parser_config{$class}{fields} ||= {};
346 return $parser_config{$class}{fields};
349 sub add_search_class_alias {
351 $pkg = ref($pkg) || $pkg;
355 $pkg->add_search_class( $class );
357 return { $class => $alias } if (grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
359 push @{$pkg->search_class_aliases->{$class}}, $alias;
361 return { $class => $alias };
364 sub search_class_aliases {
366 $class = ref($class) || $class;
368 $parser_config{$class}{class_map} ||= {};
369 return $parser_config{$class}{class_map};
372 sub add_search_field_alias {
374 $pkg = ref($pkg) || $pkg;
379 return { $class => { $field => $alias } } if (grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
381 push @{$pkg->search_field_aliases->{$class}{$field}}, $alias;
383 return { $class => { $field => $alias } };
386 sub search_field_aliases {
388 $class = ref($class) || $class;
390 $parser_config{$class}{field_alias_map} ||= {};
391 return $parser_config{$class}{field_alias_map};
394 sub remove_facet_field {
396 $pkg = ref($pkg) || $pkg;
400 return { $class => $field } if (!$pkg->facet_fields->{$class} || !grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
402 $pkg->facet_fields->{$class} = [ grep { $_ ne $field } @{$pkg->facet_fields->{$class}} ];
404 return { $class => $field };
407 sub remove_search_field {
409 $pkg = ref($pkg) || $pkg;
413 return { $class => $field } if (!$pkg->search_fields->{$class} || !grep { $_ eq $field } @{$pkg->search_fields->{$class}});
415 $pkg->search_fields->{$class} = [ grep { $_ ne $field } @{$pkg->search_fields->{$class}} ];
417 return { $class => $field };
420 sub remove_search_field_alias {
422 $pkg = ref($pkg) || $pkg;
427 return { $class => { $field => $alias } } if (!$pkg->search_field_aliases->{$class}{$field} || !grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
429 $pkg->search_field_aliases->{$class}{$field} = [ grep { $_ ne $alias } @{$pkg->search_field_aliases->{$class}{$field}} ];
431 return { $class => { $field => $alias } };
434 sub remove_search_class_alias {
436 $pkg = ref($pkg) || $pkg;
440 return { $class => $alias } if (!$pkg->search_class_aliases->{$class} || !grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
442 $pkg->search_class_aliases->{$class} = [ grep { $_ ne $alias } @{$pkg->search_class_aliases->{$class}} ];
444 return { $class => $alias };
450 $self->{_debug} = $q if (defined $q);
451 return $self->{_debug};
457 $self->{_query} = $q if (defined $q);
458 return $self->{_query};
464 $self->{_parse_tree} = $q if (defined $q);
465 return $self->{_parse_tree};
471 $self->{_top} = $q if (defined $q);
472 return $self->{_top};
477 my $pkg = ref($self) || $self;
478 warn " ** parse package is $pkg\n" if $self->debug;
481 # $self->query( shift() )
485 $self->decompose( $self->query( shift() ) );
487 if ($self->floating_plan) {
488 $self->floating_plan->add_node( $self->parse_tree );
489 $self->parse_tree( $self->floating_plan );
496 my $pkg = ref($self) || $self;
498 warn " ** decompose package is $pkg\n" if $self->debug;
501 my $current_class = shift || $self->default_search_class;
503 my $recursing = shift || 0;
504 my $phrase_helper = shift || 0;
506 # Build the search class+field uber-regexp
507 my $search_class_re = '^\s*(';
511 for my $class ( keys %{$pkg->search_field_aliases} ) {
512 warn " *** ... Looking for search fields in $class\n" if $self->debug;
514 for my $field ( keys %{$pkg->search_field_aliases->{$class}} ) {
515 warn " *** ... Looking for aliases of $field\n" if $self->debug;
517 for my $alias ( @{$pkg->search_field_aliases->{$class}{$field}} ) {
518 my $aliasr = qr/$alias/;
519 s/(^|\s+)$aliasr\|/$1$class\|$field#$alias\|/g;
520 s/(^|\s+)$aliasr[:=]/$1$class\|$field#$alias:/g;
521 warn " *** Rewriting: $alias ($aliasr) as $class\|$field\n" if $self->debug;
525 $search_class_re .= '|' unless ($first_class);
527 $search_class_re .= $class . '(?:[|#][^:|]+)*';
528 $seen_classes{$class} = 1;
531 for my $class ( keys %{$pkg->search_class_aliases} ) {
533 for my $alias ( @{$pkg->search_class_aliases->{$class}} ) {
534 my $aliasr = qr/$alias/;
535 s/(^|[^|])\b$aliasr\|/$1$class#$alias\|/g;
536 s/(^|[^|])\b$aliasr[:=]/$1$class#$alias:/g;
537 warn " *** Rewriting: $alias ($aliasr) as $class\n" if $self->debug;
540 if (!$seen_classes{$class}) {
541 $search_class_re .= '|' unless ($first_class);
544 $search_class_re .= $class . '(?:[|#][^:|]+)*';
545 $seen_classes{$class} = 1;
548 $search_class_re .= '):';
550 warn " ** Rewritten query: $_\n" if $self->debug;
551 warn " ** Search class RE: $search_class_re\n" if $self->debug;
553 my $required_re = $pkg->operator('required');
554 $required_re = qr/\Q$required_re\E/;
556 my $disallowed_re = $pkg->operator('disallowed');
557 $disallowed_re = qr/\Q$disallowed_re\E/;
559 my $and_re = $pkg->operator('and');
560 $and_re = qr/^\s*\Q$and_re\E/;
562 my $or_re = $pkg->operator('or');
563 $or_re = qr/^\s*\Q$or_re\E/;
565 my $group_start = $pkg->operator('group_start');
566 my $group_start_re = qr/^\s*\Q$group_start\E/;
568 my $group_end = $pkg->operator('group_end');
569 my $group_end_re = qr/^\s*\Q$group_end\E/;
571 my $float_start = $pkg->operator('float_start');
572 my $float_start_re = qr/^\s*\Q$float_start\E/;
574 my $float_end = $pkg->operator('float_end');
575 my $float_end_re = qr/^\s*\Q$float_end\E/;
577 my $modifier_tag_re = $pkg->operator('modifier');
578 $modifier_tag_re = qr/^\s*\Q$modifier_tag_re\E/;
581 # Build the filter and modifier uber-regexps
582 my $facet_re = '^\s*(-?)((?:' . join( '|', @{$pkg->facet_classes}) . ')(?:\|\w+)*)\[(.+?)\]';
583 warn " ** Facet RE: $facet_re\n" if $self->debug;
585 my $filter_re = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . ')\(([^()]+)\)';
586 my $filter_as_class_re = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . '):\s*(\S+)';
588 my $modifier_re = '^\s*'.$modifier_tag_re.'(' . join( '|', @{$pkg->modifiers}) . ')\b';
589 my $modifier_as_class_re = '^\s*(' . join( '|', @{$pkg->modifiers}) . '):\s*(\S+)';
591 my $struct = shift || $self->new_plan( level => $recursing );
592 $self->parse_tree( $struct ) if (!$self->parse_tree);
597 while (!$remainder) {
598 if (/^\s*$/) { # end of an explicit group
600 } elsif (/$float_end_re/) { # end of an explicit group
601 warn "Encountered explicit float end\n" if $self->debug;
607 } elsif (/$group_end_re/) { # end of an explicit group
608 warn "Encountered explicit group end\n" if $self->debug;
611 $remainder = $struct->top_plan ? '' : $';
614 } elsif ($self->filter_count && /$filter_re/) { # found a filter
615 warn "Encountered search filter: $1$2 set to $3\n" if $self->debug;
617 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
621 my $params = [ split '[,]+', $3 ];
623 if ($pkg->filter_callbacks->{$filter}) {
624 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
625 $_ = "$replacement $_" if ($replacement);
627 $struct->new_filter( $filter => $params, $negate );
632 } elsif ($self->filter_count && /$filter_as_class_re/) { # found a filter
633 warn "Encountered search filter: $1$2 set to $3\n" if $self->debug;
635 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
639 my $params = [ split '[,]+', $3 ];
641 if ($pkg->filter_callbacks->{$filter}) {
642 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
643 $_ = "$replacement $_" if ($replacement);
645 $struct->new_filter( $filter => $params, $negate );
649 } elsif ($self->modifier_count && /$modifier_re/) { # found a modifier
650 warn "Encountered search modifier: $1\n" if $self->debug;
653 if (!$struct->top_plan) {
654 warn " Search modifiers only allowed at the top level of the query\n" if $self->debug;
656 $struct->new_modifier($1);
660 } elsif ($self->modifier_count && /$modifier_as_class_re/) { # found a modifier
661 warn "Encountered search modifier: $1\n" if $self->debug;
666 if (!$struct->top_plan) {
667 warn " Search modifiers only allowed at the top level of the query\n" if $self->debug;
668 } elsif ($2 =~ /^[ty1]/i) {
669 $struct->new_modifier($mod);
673 } elsif (/$float_start_re/) { # start of an explicit float
674 warn "Encountered explicit float start\n" if $self->debug;
676 $self->floating_plan( $self->new_plan( floating => 1 ) ) if (!$self->floating_plan);
677 # pass the floating_plan struct to be modified by the float'ed chunk
678 my ($floating_plan, $subremainder) = $self->new->decompose( $', undef, undef, undef, $self->floating_plan);
682 } elsif (/$group_start_re/) { # start of an explicit group
683 warn "Encountered explicit group start\n" if $self->debug;
685 my ($substruct, $subremainder) = $self->decompose( $', $current_class, $recursing + 1 );
686 $struct->add_node( $substruct ) if ($substruct);
690 } elsif (/$and_re/) { # ANDed expression
692 next if ($last_type eq 'AND');
693 next if ($last_type eq 'OR');
694 warn "Encountered AND\n" if $self->debug;
697 my ($RHS, $subremainder) = $self->decompose( $group_start.$_.$group_end, $current_class, $recursing + 1 );
700 $struct = $self->new_plan( level => $recursing, joiner => '&' );
701 $struct->add_node($_) for ($LHS, $RHS);
703 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
706 } elsif (/$or_re/) { # ORed expression
708 next if ($last_type eq 'AND');
709 next if ($last_type eq 'OR');
710 warn "Encountered OR\n" if $self->debug;
713 my ($RHS, $subremainder) = $self->decompose( $group_start.$_.$group_end, $current_class, $recursing + 1 );
716 $struct = $self->new_plan( level => $recursing, joiner => '|' );
717 $struct->add_node($_) for ($LHS, $RHS);
719 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
722 } elsif ($self->facet_class_count && /$facet_re/) { # changing current class
723 warn "Encountered facet: $1$2 => $3\n" if $self->debug;
725 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
727 my $facet_value = [ split '\s*#\s*', $3 ];
728 $struct->new_facet( $facet => $facet_value, $negate );
732 } elsif ($self->search_class_count && /$search_class_re/) { # changing current class
734 if ($last_type eq 'CLASS') {
735 $struct->remove_last_node( $current_class );
736 warn "Encountered class change with no searches!\n" if $self->debug;
739 warn "Encountered class change: $1\n" if $self->debug;
741 $current_class = $struct->classed_node( $1 )->requested_class();
744 $last_type = 'CLASS';
745 } elsif (/^\s*($required_re|$disallowed_re)?"([^"]+)"/) { # phrase, always anded
746 warn 'Encountered' . ($1 ? " ['$1' modified]" : '') . " phrase: $2\n" if $self->debug;
748 my $req_ness = $1 || '';
751 if (!$phrase_helper) {
752 warn "Recursing into decompose with the phrase as a subquery\n" if $self->debug;
754 my ($substruct, $subremainder) = $self->decompose( qq/$req_ness"$phrase"/, $current_class, $recursing + 1, 1 );
755 $struct->add_node( $substruct ) if ($substruct);
758 warn "Directly parsing the phrase subquery\n" if $self->debug;
759 $struct->joiner( '&' );
761 my $class_node = $struct->classed_node($current_class);
763 if ($req_ness eq $pkg->operator('disallowed')) {
764 $class_node->add_dummy_atom( node => $class_node );
765 $class_node->add_unphrase( $phrase );
767 #$phrase =~ s/(^|\s)\b/$1-/g;
769 $class_node->add_phrase( $phrase );
777 # } elsif (/^\s*$required_re([^\s"]+)/) { # phrase, always anded
778 # warn "Encountered required atom (mini phrase): $1\n" if $self->debug;
782 # my $class_node = $struct->classed_node($current_class);
783 # $class_node->add_phrase( $phrase );
785 # $struct->joiner( '&' );
788 } elsif (/^\s*([^$group_end\s]+)/o && /^\s*([^$float_end\s]+)/o) { # atom
789 warn "Encountered atom: $1\n" if $self->debug;
790 warn "Remainder: $'\n" if $self->debug;
798 my $class_node = $struct->classed_node($current_class);
800 my $prefix = ($atom =~ s/^$disallowed_re//o) ? '!' : '';
801 my $truncate = ($atom =~ s/\*$//o) ? '*' : '';
803 if ($atom ne '' and !grep { $atom =~ /^\Q$_\E+$/ } ('&','|','-','+')) { # throw away & and |, not allowed in tsquery, and not really useful anyway
804 # $class_node->add_phrase( $atom ) if ($atom =~ s/^$required_re//o);
805 # $class_node->add_unphrase( $atom ) if ($prefix eq '!');
807 $class_node->add_fts_atom( $atom, suffix => $truncate, prefix => $prefix, node => $class_node );
808 $struct->joiner( '&' );
817 scalar(@{$struct->query_nodes}) == 0 &&
818 scalar(@{$struct->filters}) == 0 &&
821 return $struct if !wantarray;
822 return ($struct, $remainder);
825 sub find_class_index {
829 my ($class_part, @field_parts) = split '\|', $class;
830 $class_part ||= $class;
832 for my $idx ( 0 .. scalar(@$query) - 1 ) {
833 next unless ref($$query[$idx]);
834 return $idx if ( $$query[$idx]{requested_class} && $class eq $$query[$idx]{requested_class} );
837 push(@$query, { classname => $class_part, (@field_parts ? (fields => \@field_parts) : ()), requested_class => $class, ftsquery => [], phrases => [] });
844 $self->{core_limit} = $l if ($l);
845 return $self->{core_limit};
851 $self->{superpage} = $l if ($l);
852 return $self->{superpage};
858 $self->{superpage_size} = $l if ($l);
859 return $self->{superpage_size};
863 #-------------------------------
864 package QueryParser::_util;
866 # At this level, joiners are always & or |. This is not
867 # the external, configurable representation of joiners that
868 # defaults to # && and ||.
872 return (not ref $str and ($str eq '&' or $str eq '|'));
875 sub default_joiner { '&' }
877 # 0 for different, 1 for the same.
878 sub compare_abstract_atoms {
879 my ($left, $right) = @_;
881 foreach (qw/prefix suffix content/) {
882 no warnings; # undef can stand in for '' here
883 return 0 unless $left->{$_} eq $right->{$_};
889 sub fake_abstract_atom_from_phrase {
892 my $qp_class = shift || 'QueryParser';
897 $QueryParser::parser_config{$qp_class}{operators}{disallowed} .
902 "type" => "atom", "prefix" => $prefix, "suffix" => '"',
907 sub find_arrays_in_abstract {
911 foreach my $key (keys %$hash) {
912 if (ref $hash->{$key} eq "ARRAY") {
913 push @arrays, $hash->{$key};
914 foreach (@{$hash->{$key}}) {
915 push @arrays, find_arrays_in_abstract($_);
923 #-------------------------------
924 package QueryParser::Canonicalize; # not OO
926 sub _abstract_query2str_filter {
928 my $qp_class = shift || 'QueryParser';
929 my $qpconfig = $QueryParser::parser_config{$qp_class};
933 $f->{negate} ? $qpconfig->{operators}{disallowed} : "",
935 join(",", @{$f->{args}})
939 sub _abstract_query2str_modifier {
941 my $qp_class = shift || 'QueryParser';
942 my $qpconfig = $QueryParser::parser_config{$qp_class};
944 return $qpconfig->{operators}{modifier} . $f;
947 # This should produce an equivalent query to the original, given an
949 sub abstract_query2str_impl {
950 my $abstract_query = shift;
951 my $depth = shift || 0;
953 my $qp_class ||= shift || 'QueryParser';
954 my $qpconfig = $QueryParser::parser_config{$qp_class};
956 my $fs = $qpconfig->{operators}{float_start};
957 my $fe = $qpconfig->{operators}{float_end};
958 my $gs = $qpconfig->{operators}{group_start};
959 my $ge = $qpconfig->{operators}{group_end};
960 my $and = $qpconfig->{operators}{and};
961 my $or = $qpconfig->{operators}{or};
966 if (exists $abstract_query->{type}) {
967 if ($abstract_query->{type} eq 'query_plan') {
968 $q .= join(" ", map { _abstract_query2str_filter($_, $qp_class) } @{$abstract_query->{filters}}) if
969 exists $abstract_query->{filters};
970 $needs_group += scalar(@{$abstract_query->{filters}}) if exists $abstract_query->{filters};
974 $q .= join(" ", map { _abstract_query2str_modifier($_, $qp_class) } @{$abstract_query->{modifiers}}) if
975 exists $abstract_query->{modifiers};
976 $needs_group += scalar(@{$abstract_query->{modifiers}}) if exists $abstract_query->{modifiers};
977 } elsif ($abstract_query->{type} eq 'node') {
978 if ($abstract_query->{alias}) {
979 $q .= " " . $abstract_query->{alias};
980 $q .= "|$_" foreach @{$abstract_query->{alias_fields}};
982 $q .= " " . $abstract_query->{class};
983 $q .= "|$_" foreach @{$abstract_query->{fields}};
986 } elsif ($abstract_query->{type} eq 'atom') {
987 my $prefix = $abstract_query->{prefix} || '';
988 $prefix = $qpconfig->{operators}{disallowed} if $prefix eq '!';
990 ($abstract_query->{content} || '') .
991 ($abstract_query->{suffix} || '');
993 } elsif ($abstract_query->{type} eq 'facet') {
994 # facet syntax [ # ] is hardcoded I guess?
995 my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
996 $q .= $prefix . $abstract_query->{name} . "[" .
997 join(" # ", @{$abstract_query->{values}}) . "]";
1002 if (exists $abstract_query->{children}) {
1004 my $op = (keys(%{$abstract_query->{children}}))[0];
1006 if ($abstract_query->{floating}) { # always the top node!
1007 my $sub_node = pop @{$abstract_query->{children}{$op}};
1009 $abstract_query->{floating} = 0;
1010 $q = $fs.abstract_query2str_impl($abstract_query,0,$qp_class).$fe;
1012 $abstract_query = $sub_node;
1015 if ($abstract_query && exists $abstract_query->{children}) {
1016 $op = (keys(%{$abstract_query->{children}}))[0];
1018 " " . ($op eq '&' ? '' : $or) . " ",
1020 abstract_query2str_impl($_, $depth + 1, $qp_class)
1021 } @{$abstract_query->{children}{$op}}
1023 $needs_group += scalar(@{$abstract_query->{children}{$op}});
1025 } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1026 my $op = (keys(%{$abstract_query}))[0];
1028 " " . ($op eq '&' ? '' : $or) . " ",
1030 abstract_query2str_impl($_, $depth + 1, $qp_class)
1031 } @{$abstract_query->{$op}}
1033 $needs_group += scalar(@{$abstract_query->{$op}});
1037 $q = $gs . $q . $ge if ($needs_group > 1 and $depth);
1042 #-------------------------------
1043 package QueryParser::query_plan;
1047 return undef unless ref($self);
1048 return $self->{QueryParser};
1053 $pkg = ref($pkg) || $pkg;
1054 my %args = (query => [], joiner => '&', @_);
1056 return bless \%args => $pkg;
1061 my $pkg = ref($self) || $self;
1062 my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1063 $self->add_node( $node );
1069 my $pkg = ref($self) || $self;
1074 my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1075 $self->add_node( $node );
1082 my $pkg = ref($self) || $self;
1087 my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1088 $self->add_filter( $node );
1094 sub _merge_filters {
1095 my $left_filter = shift;
1096 my $right_filter = shift;
1099 return undef unless $left_filter or $right_filter;
1100 return $right_filter unless $left_filter;
1101 return $left_filter unless $right_filter;
1103 my $args = $left_filter->{args} || [];
1106 push(@$args, @{$right_filter->{args}});
1109 # find the intersect values
1111 map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1112 $args = [ grep { $new_vals{$_} } @$args ];
1115 $left_filter->{args} = $args;
1116 return $left_filter;
1119 sub collapse_filters {
1123 # start by merging any filters at this level.
1124 # like-level filters are always ORed together
1127 my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1129 $cur_filter = shift @cur_filters;
1130 my $args = $cur_filter->{args} || [];
1131 $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1134 # next gather the collapsed filters from sub-plans and
1135 # merge them with our own
1137 my @subquery = @{$self->{query}};
1140 my $blob = shift @subquery;
1141 shift @subquery; # joiner
1142 next unless $blob->isa('QueryParser::query_plan');
1143 my $sub_filter = $blob->collapse_filters($name);
1144 $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1147 if ($self->QueryParser->debug) {
1148 my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1149 warn "collapse_filters($name) => [@args]\n";
1157 my $needle = shift;;
1158 return undef unless ($needle);
1160 my $filter = $self->collapse_filters($needle);
1162 warn "find_filter($needle) => " .
1163 (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n"
1164 if $self->QueryParser->debug;
1166 return $filter ? ($filter) : ();
1171 my $needle = shift;;
1172 return undef unless ($needle);
1173 return grep { $_->name eq $needle } @{ $self->modifiers };
1178 my $pkg = ref($self) || $self;
1181 my $node = do{$pkg.'::modifier'}->new( $name );
1182 $self->add_modifier( $node );
1189 my $requested_class = shift;
1192 for my $n (@{$self->{query}}) {
1193 next unless (ref($n) && $n->isa( 'QueryParser::query_plan::node' ));
1194 if ($n->requested_class eq $requested_class) {
1201 $node = $self->new_node;
1202 $node->requested_class( $requested_class );
1208 sub remove_last_node {
1210 my $requested_class = shift;
1212 my $old = pop(@{$self->query_nodes});
1213 pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1220 return $self->{query};
1227 $self->{query} ||= [];
1228 push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1229 push(@{$self->{query}}, $node);
1237 return $self->{level} ? 0 : 1;
1242 return $self->{level};
1249 $self->{joiner} = $joiner if ($joiner);
1250 return $self->{joiner};
1255 $self->{modifiers} ||= [];
1256 return $self->{modifiers};
1261 my $modifier = shift;
1263 $self->{modifiers} ||= [];
1264 $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1266 push(@{$self->{modifiers}}, $modifier);
1273 $self->{facets} ||= [];
1274 return $self->{facets};
1281 $self->{facets} ||= [];
1282 $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
1284 push(@{$self->{facets}}, $facet);
1291 $self->{filters} ||= [];
1292 return $self->{filters};
1299 $self->{filters} ||= [];
1301 push(@{$self->{filters}}, $filter);
1306 # %opts supports two options at this time:
1308 # If true, do not do anything to the phrases and unphrases
1309 # fields on any discovered nodes.
1311 # If true, also return the query parser config as part of the blob.
1312 # This will get set back to 0 before recursion to avoid repetition.
1313 sub to_abstract_query {
1317 my $pkg = ref $self->QueryParser || $self->QueryParser;
1319 my $abstract_query = {
1320 type => "query_plan",
1321 floating => $self->{floating},
1322 filters => [map { $_->to_abstract_query } @{$self->filters}],
1323 modifiers => [map { $_->to_abstract_query } @{$self->modifiers}]
1326 if ($opts{with_config}) {
1327 $opts{with_config} = 0;
1328 $abstract_query->{config} = $QueryParser::parser_config{$pkg};
1333 for my $qnode (@{$self->query_nodes}) {
1334 # Remember: qnode can be a joiner string, a node, or another query_plan
1336 if (QueryParser::_util::is_joiner($qnode)) {
1337 if ($abstract_query->{children}) {
1338 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
1339 next if $open_joiner eq $qnode;
1341 my $oldroot = $abstract_query->{children};
1343 $abstract_query->{children} = {$qnode => $kids};
1345 $abstract_query->{children} = {$qnode => $kids};
1348 push @$kids, $qnode->to_abstract_query(%opts);
1352 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
1353 return $abstract_query;
1357 #-------------------------------
1358 package QueryParser::query_plan::node;
1360 $Data::Dumper::Indent = 0;
1364 $pkg = ref($pkg) || $pkg;
1367 return bless \%args => $pkg;
1372 my $pkg = ref($self) || $self;
1373 return do{$pkg.'::atom'}->new( @_ );
1376 sub requested_class { # also split into classname, fields and alias
1382 my (undef, $alias) = split '#', $class;
1384 $class =~ s/#[^|]+//;
1385 ($alias, @afields) = split '\|', $alias;
1388 my @fields = @afields;
1389 my ($class_part, @field_parts) = split '\|', $class;
1390 for my $f (@field_parts) {
1391 push(@fields, $f) unless (grep { $f eq $_ } @fields);
1394 $class_part ||= $class;
1396 $self->{requested_class} = $class;
1397 $self->{alias} = $alias if $alias;
1398 $self->{alias_fields} = \@afields if $alias;
1399 $self->{classname} = $class_part;
1400 $self->{fields} = \@fields;
1403 return $self->{requested_class};
1410 $self->{plan} = $plan if ($plan);
1411 return $self->{plan};
1418 $self->{alias} = $alias if ($alias);
1419 return $self->{alias};
1426 $self->{alias_fields} = $alias if ($alias);
1427 return $self->{alias_fields};
1434 $self->{classname} = $class if ($class);
1435 return $self->{classname};
1442 $self->{fields} ||= [];
1443 $self->{fields} = \@fields if (@fields);
1444 return $self->{fields};
1451 $self->{phrases} ||= [];
1452 $self->{phrases} = \@phrases if (@phrases);
1453 return $self->{phrases};
1460 $self->{unphrases} ||= [];
1461 $self->{unphrases} = \@phrases if (@phrases);
1462 return $self->{unphrases};
1469 push(@{$self->phrases}, $phrase);
1478 push(@{$self->unphrases}, $phrase);
1485 my @query_atoms = @_;
1487 $self->{query_atoms} ||= [];
1488 $self->{query_atoms} = \@query_atoms if (@query_atoms);
1489 return $self->{query_atoms};
1497 my $content = $atom;
1500 $atom = $self->new_atom( content => $content, @parts );
1503 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1504 push(@{$self->query_atoms}, $atom);
1509 sub add_dummy_atom {
1513 my $atom = $self->new_atom( @parts, dummy => 1 );
1515 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1516 push(@{$self->query_atoms}, $atom);
1521 # This will find up to one occurence of @$short_list within @$long_list, and
1522 # replace it with the single atom $replacement.
1523 sub replace_phrase_in_abstract_query {
1524 my ($self, $short_list, $long_list, $replacement) = @_;
1528 my $goal = scalar @$short_list;
1530 for (my $i = 0; $i < scalar (@$long_list); $i++) {
1531 my $right = $long_list->[$i];
1533 if (QueryParser::_util::compare_abstract_atoms(
1534 $short_list->[scalar @already], $right
1537 } elsif (scalar @already) {
1542 if (scalar @already == $goal) {
1543 splice @$long_list, $already[0], scalar(@already), $replacement;
1552 sub to_abstract_query {
1556 my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
1558 my $abstract_query = {
1560 "alias" => $self->alias,
1561 "alias_fields" => $self->alias_fields,
1562 "class" => $self->classname,
1563 "fields" => $self->fields
1568 for my $qatom (@{$self->query_atoms}) {
1569 if (QueryParser::_util::is_joiner($qatom)) {
1570 if ($abstract_query->{children}) {
1571 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
1572 next if $open_joiner eq $qatom;
1574 my $oldroot = $abstract_query->{children};
1576 $abstract_query->{children} = {$qatom => $kids};
1578 $abstract_query->{children} = {$qatom => $kids};
1581 push @$kids, $qatom->to_abstract_query;
1585 if ($self->{phrases} and not $opts{no_phrases}) {
1586 for my $phrase (@{$self->{phrases}}) {
1587 # Phrases appear duplication in a real QP tree, and we don't want
1588 # that duplication in our abstract query. So for all our phrases,
1589 # break them into atoms as QP would, and remove any matching
1590 # sequences of atoms from our abstract query.
1592 my $tmptree = $self->{plan}->{QueryParser}->new(query => '"'.$phrase.'"')->parse->parse_tree;
1594 # For a well-behaved phrase, we should now have only one node
1595 # in the $tmptree query plan, and that node should have an
1596 # orderly list of atoms and joiners.
1598 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
1602 $tmplist = $tmptree->{query}->[0]->to_abstract_query(
1604 )->{children}->{'&'}->[0]->{children}->{'&'};
1609 QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
1611 last if $self->replace_phrase_in_abstract_query(
1614 QueryParser::_util::fake_abstract_atom_from_phrase($phrase, undef, $pkg)
1622 # Do the same as the preceding block for unphrases (negated phrases).
1623 if ($self->{unphrases} and not $opts{no_phrases}) {
1624 for my $phrase (@{$self->{unphrases}}) {
1625 my $tmptree = $self->{plan}->{QueryParser}->new(
1626 query => $QueryParser::parser_config{$pkg}{operators}{disallowed}.
1628 )->parse->parse_tree;
1631 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
1635 $tmplist = $tmptree->{query}->[0]->to_abstract_query(
1637 )->{children}->{'&'}->[0]->{children}->{'&'};
1642 QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
1644 last if $self->replace_phrase_in_abstract_query(
1647 QueryParser::_util::fake_abstract_atom_from_phrase($phrase, 1, $pkg)
1655 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
1656 return $abstract_query;
1659 #-------------------------------
1660 package QueryParser::query_plan::node::atom;
1664 $pkg = ref($pkg) || $pkg;
1667 return bless \%args => $pkg;
1672 return undef unless (ref $self);
1673 return $self->{node};
1678 return undef unless (ref $self);
1679 return $self->{content};
1684 return undef unless (ref $self);
1685 return $self->{prefix};
1690 return undef unless (ref $self);
1691 return $self->{suffix};
1694 sub to_abstract_query {
1698 (map { $_ => $self->$_ } qw/prefix suffix content/),
1702 #-------------------------------
1703 package QueryParser::query_plan::filter;
1707 $pkg = ref($pkg) || $pkg;
1710 return bless \%args => $pkg;
1715 return $self->{plan};
1720 return $self->{name};
1725 return $self->{negate};
1730 return $self->{args};
1733 sub to_abstract_query {
1737 map { $_ => $self->$_ } qw/name negate args/
1741 #-------------------------------
1742 package QueryParser::query_plan::facet;
1746 $pkg = ref($pkg) || $pkg;
1749 return bless \%args => $pkg;
1754 return $self->{plan};
1759 return $self->{name};
1764 return $self->{negate};
1769 return $self->{'values'};
1772 sub to_abstract_query {
1776 (map { $_ => $self->$_ } qw/name negate values/),
1781 #-------------------------------
1782 package QueryParser::query_plan::modifier;
1786 $pkg = ref($pkg) || $pkg;
1787 my $modifier = shift;
1790 return bless { name => $modifier, negate => $negate } => $pkg;
1795 return $self->{name};
1800 return $self->{negate};
1803 sub to_abstract_query {