5 use OpenSRF::Utils::JSON;
10 QueryParser - basic QueryParser class
15 my $QParser = QueryParser->new(%args);
19 Main entrypoint into the QueryParser functionality.
25 # Note that the first key must match the name of the package.
26 our %parser_config = (
47 return QueryParser::Canonicalize::abstract_query2str_impl(
48 $self->parse_tree->to_abstract_query(@_)
53 =head2 facet_class_count
55 $count = $QParser->facet_class_count();
58 sub facet_class_count {
60 return @{$self->facet_classes};
63 =head2 search_class_count
65 $count = $QParser->search_class_count();
68 sub search_class_count {
70 return @{$self->search_classes};
75 $count = $QParser->filter_count();
80 return @{$self->filters};
85 $count = $QParser->modifier_count();
90 return @{$self->modifiers};
95 $data = $QParser->custom_data($class);
100 $class = ref($class) || $class;
102 $parser_config{$class}{custom_data} ||= {};
103 return $parser_config{$class}{custom_data};
108 $operators = $QParser->operators();
110 Returns hashref of the configured operators.
115 $class = ref($class) || $class;
117 $parser_config{$class}{operators} ||= {};
118 return $parser_config{$class}{operators};
121 sub allow_nested_modifiers {
124 $class = ref($class) || $class;
126 $parser_config{$class}{allow_nested_modifiers} = $v if (defined $v);
127 return $parser_config{$class}{allow_nested_modifiers};
132 $filters = $QParser->filters();
134 Returns arrayref of the configured filters.
139 $class = ref($class) || $class;
141 $parser_config{$class}{filters} ||= [];
142 return $parser_config{$class}{filters};
145 =head2 filter_callbacks
147 $filter_callbacks = $QParser->filter_callbacks();
149 Returns hashref of the configured filter callbacks.
152 sub filter_callbacks {
154 $class = ref($class) || $class;
156 $parser_config{$class}{filter_callbacks} ||= {};
157 return $parser_config{$class}{filter_callbacks};
162 $modifiers = $QParser->modifiers();
164 Returns arrayref of the configured modifiers.
169 $class = ref($class) || $class;
171 $parser_config{$class}{modifiers} ||= [];
172 return $parser_config{$class}{modifiers};
177 $QParser = QueryParser->new(%args);
179 Creates a new QueryParser object.
184 $class = ref($class) || $class;
188 my $self = bless {} => $class;
190 for my $o (keys %{QueryParser->operators}) {
191 $class->operator($o => QueryParser->operator($o)) unless ($class->operator($o));
194 for my $opt ( keys %opts) {
195 $self->$opt( $opts{$opt} ) if ($self->can($opt));
203 $query_plan = $QParser->new_plan();
205 Create a new query plan.
210 my $pkg = ref($self) || $self;
211 return do{$pkg.'::query_plan'}->new( QueryParser => $self, @_ );
214 =head2 add_search_filter
216 $QParser->add_search_filter($filter, [$callback]);
218 Adds a filter with the specified name and an optional callback to the
219 QueryParser configuration.
222 sub add_search_filter {
224 $pkg = ref($pkg) || $pkg;
226 my $callback = shift;
228 return $filter if (grep { $_ eq $filter } @{$pkg->filters});
229 push @{$pkg->filters}, $filter;
230 $pkg->filter_callbacks->{$filter} = $callback if ($callback);
234 =head2 add_search_modifier
236 $QParser->add_search_modifier($modifier);
238 Adds a modifier with the specified name to the QueryParser configuration.
241 sub add_search_modifier {
243 $pkg = ref($pkg) || $pkg;
244 my $modifier = shift;
246 return $modifier if (grep { $_ eq $modifier } @{$pkg->modifiers});
247 push @{$pkg->modifiers}, $modifier;
251 =head2 add_facet_class
253 $QParser->add_facet_class($facet_class);
255 Adds a facet class with the specified name to the QueryParser configuration.
258 sub add_facet_class {
260 $pkg = ref($pkg) || $pkg;
263 return $class if (grep { $_ eq $class } @{$pkg->facet_classes});
265 push @{$pkg->facet_classes}, $class;
266 $pkg->facet_fields->{$class} = [];
271 =head2 add_search_class
273 $QParser->add_search_class($class);
275 Adds a search class with the specified name to the QueryParser configuration.
278 sub add_search_class {
280 $pkg = ref($pkg) || $pkg;
283 return $class if (grep { $_ eq $class } @{$pkg->search_classes});
285 push @{$pkg->search_classes}, $class;
286 $pkg->search_fields->{$class} = [];
287 $pkg->default_search_class( $pkg->search_classes->[0] ) if (@{$pkg->search_classes} == 1);
292 =head2 add_search_modifier
294 $op = $QParser->operator($operator, [$newvalue]);
296 Retrieves or sets value for the specified operator. Valid operators and
297 their defaults are as follows:
305 =item * group_start => (
307 =item * group_end => )
309 =item * required => +
311 =item * disallowed => -
313 =item * modifier => #
321 $class = ref($class) || $class;
325 return undef unless ($opname);
327 $parser_config{$class}{operators} ||= {};
328 $parser_config{$class}{operators}{$opname} = $op if ($op);
330 return $parser_config{$class}{operators}{$opname};
335 $classes = $QParser->facet_classes([\@newclasses]);
337 Returns arrayref of all configured facet classes after optionally
338 replacing configuration.
343 $class = ref($class) || $class;
346 $parser_config{$class}{facet_classes} ||= [];
347 $parser_config{$class}{facet_classes} = $classes if (ref($classes) && @$classes);
348 return $parser_config{$class}{facet_classes};
351 =head2 search_classes
353 $classes = $QParser->search_classes([\@newclasses]);
355 Returns arrayref of all configured search classes after optionally
356 replacing the previous configuration.
361 $class = ref($class) || $class;
364 $parser_config{$class}{classes} ||= [];
365 $parser_config{$class}{classes} = $classes if (ref($classes) && @$classes);
366 return $parser_config{$class}{classes};
369 =head2 add_query_normalizer
371 $function = $QParser->add_query_normalizer($class, $field, $func, [\@params]);
375 sub add_query_normalizer {
377 $pkg = ref($pkg) || $pkg;
381 my $params = shift || [];
383 # do not add if function AND params are identical to existing member
384 return $func if (grep {
385 $_->{function} eq $func and
386 OpenSRF::Utils::JSON->perl2JSON($_->{params}) eq OpenSRF::Utils::JSON->perl2JSON($params)
387 } @{$pkg->query_normalizers->{$class}->{$field}});
389 push(@{$pkg->query_normalizers->{$class}->{$field}}, { function => $func, params => $params });
394 =head2 query_normalizers
396 $normalizers = $QParser->query_normalizers($class, $field);
398 Returns a list of normalizers associated with the specified search class
402 sub query_normalizers {
404 $pkg = ref($pkg) || $pkg;
409 $parser_config{$pkg}{normalizers} ||= {};
412 $parser_config{$pkg}{normalizers}{$class}{$field} ||= [];
413 return $parser_config{$pkg}{normalizers}{$class}{$field};
415 return $parser_config{$pkg}{normalizers}{$class};
419 return $parser_config{$pkg}{normalizers};
422 =head2 add_filter_normalizer
424 $normalizer = $QParser->add_filter_normalizer($filter, $func, [\@params]);
426 Adds a normalizer function to the specified filter.
429 sub add_filter_normalizer {
431 $pkg = ref($pkg) || $pkg;
434 my $params = shift || [];
436 return $func if (grep { $_ eq $func } @{$pkg->filter_normalizers->{$filter}});
438 push(@{$pkg->filter_normalizers->{$filter}}, { function => $func, params => $params });
443 =head2 filter_normalizers
445 $normalizers = $QParser->filter_normalizers($filter);
447 Return arrayref of normalizer functions associated with the specified filter.
450 sub filter_normalizers {
452 $pkg = ref($pkg) || $pkg;
456 $parser_config{$pkg}{filter_normalizers} ||= {};
458 $parser_config{$pkg}{filter_normalizers}{$filter} ||= [];
459 return $parser_config{$pkg}{filter_normalizers}{$filter};
462 return $parser_config{$pkg}{filter_normalizers};
465 =head2 default_search_class
467 $default_class = $QParser->default_search_class([$class]);
469 Set or return the default search class.
472 sub default_search_class {
474 $pkg = ref($pkg) || $pkg;
476 $QueryParser::parser_config{$pkg}{default_class} = $pkg->add_search_class( $class ) if $class;
478 return $QueryParser::parser_config{$pkg}{default_class};
481 =head2 remove_facet_class
483 $QParser->remove_facet_class($class);
485 Remove the specified facet class from the configuration.
488 sub remove_facet_class {
490 $pkg = ref($pkg) || $pkg;
493 return $class if (!grep { $_ eq $class } @{$pkg->facet_classes});
495 $pkg->facet_classes( [ grep { $_ ne $class } @{$pkg->facet_classes} ] );
496 delete $QueryParser::parser_config{$pkg}{facet_fields}{$class};
501 =head2 remove_search_class
503 $QParser->remove_search_class($class);
505 Remove the specified search class from the configuration.
508 sub remove_search_class {
510 $pkg = ref($pkg) || $pkg;
513 return $class if (!grep { $_ eq $class } @{$pkg->search_classes});
515 $pkg->search_classes( [ grep { $_ ne $class } @{$pkg->search_classes} ] );
516 delete $QueryParser::parser_config{$pkg}{fields}{$class};
521 =head2 add_facet_field
523 $QParser->add_facet_field($class, $field);
525 Adds the specified field (and facet class if it doesn't already exist)
526 to the configuration.
529 sub add_facet_field {
531 $pkg = ref($pkg) || $pkg;
535 $pkg->add_facet_class( $class );
537 return { $class => $field } if (grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
539 push @{$pkg->facet_fields->{$class}}, $field;
541 return { $class => $field };
546 $fields = $QParser->facet_fields($class);
548 Returns arrayref with list of fields for specified facet class.
553 $class = ref($class) || $class;
555 $parser_config{$class}{facet_fields} ||= {};
556 return $parser_config{$class}{facet_fields};
559 =head2 add_search_field
561 $QParser->add_search_field($class, $field);
563 Adds the specified field (and facet class if it doesn't already exist)
564 to the configuration.
567 sub add_search_field {
569 $pkg = ref($pkg) || $pkg;
573 $pkg->add_search_class( $class );
575 return { $class => $field } if (grep { $_ eq $field } @{$pkg->search_fields->{$class}});
577 push @{$pkg->search_fields->{$class}}, $field;
579 return { $class => $field };
584 $fields = $QParser->search_fields();
586 Returns arrayref with list of configured search fields.
591 $class = ref($class) || $class;
593 $parser_config{$class}{fields} ||= {};
594 return $parser_config{$class}{fields};
597 =head2 add_search_class_alias
599 $QParser->add_search_class_alias($class, $alias);
602 sub add_search_class_alias {
604 $pkg = ref($pkg) || $pkg;
608 $pkg->add_search_class( $class );
610 return { $class => $alias } if (grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
612 push @{$pkg->search_class_aliases->{$class}}, $alias;
614 return { $class => $alias };
617 =head2 search_class_aliases
619 $aliases = $QParser->search_class_aliases($class);
622 sub search_class_aliases {
624 $class = ref($class) || $class;
626 $parser_config{$class}{class_map} ||= {};
627 return $parser_config{$class}{class_map};
630 =head2 add_search_field_alias
632 $QParser->add_search_field_alias($class, $field, $alias);
635 sub add_search_field_alias {
637 $pkg = ref($pkg) || $pkg;
642 return { $class => { $field => $alias } } if (grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
644 push @{$pkg->search_field_aliases->{$class}{$field}}, $alias;
646 return { $class => { $field => $alias } };
649 =head2 search_field_aliases
651 $aliases = $QParser->search_field_aliases();
654 sub search_field_aliases {
656 $class = ref($class) || $class;
658 $parser_config{$class}{field_alias_map} ||= {};
659 return $parser_config{$class}{field_alias_map};
662 =head2 remove_facet_field
664 $QParser->remove_facet_field($class, $field);
667 sub remove_facet_field {
669 $pkg = ref($pkg) || $pkg;
673 return { $class => $field } if (!$pkg->facet_fields->{$class} || !grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
675 $pkg->facet_fields->{$class} = [ grep { $_ ne $field } @{$pkg->facet_fields->{$class}} ];
677 return { $class => $field };
680 =head2 remove_search_field
682 $QParser->remove_search_field($class, $field);
685 sub remove_search_field {
687 $pkg = ref($pkg) || $pkg;
691 return { $class => $field } if (!$pkg->search_fields->{$class} || !grep { $_ eq $field } @{$pkg->search_fields->{$class}});
693 $pkg->search_fields->{$class} = [ grep { $_ ne $field } @{$pkg->search_fields->{$class}} ];
695 return { $class => $field };
698 =head2 remove_search_field_alias
700 $QParser->remove_search_field_alias($class, $field, $alias);
703 sub remove_search_field_alias {
705 $pkg = ref($pkg) || $pkg;
710 return { $class => { $field => $alias } } if (!$pkg->search_field_aliases->{$class}{$field} || !grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
712 $pkg->search_field_aliases->{$class}{$field} = [ grep { $_ ne $alias } @{$pkg->search_field_aliases->{$class}{$field}} ];
714 return { $class => { $field => $alias } };
717 =head2 remove_search_class_alias
719 $QParser->remove_search_class_alias($class, $alias);
722 sub remove_search_class_alias {
724 $pkg = ref($pkg) || $pkg;
728 return { $class => $alias } if (!$pkg->search_class_aliases->{$class} || !grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
730 $pkg->search_class_aliases->{$class} = [ grep { $_ ne $alias } @{$pkg->search_class_aliases->{$class}} ];
732 return { $class => $alias };
737 $debug = $QParser->debug([$debug]);
739 Return or set whether debugging output is enabled.
745 $self->{_debug} = $q if (defined $q);
746 return $self->{_debug};
751 $query = $QParser->query([$query]);
753 Return or set the query.
759 $self->{_query} = " $q " if (defined $q);
760 return $self->{_query};
765 $parse_tree = $QParser->parse_tree([$parse_tree]);
767 Return or set the parse tree associated with the QueryParser.
773 $self->{_parse_tree} = $q if (defined $q);
774 return $self->{_parse_tree};
780 $self->{_top} = $q if (defined $q);
781 return $self->{_top};
786 $QParser->parse([$query]);
788 Parse the specified query, or the query already associated with the QueryParser
793 our $last_class = '';
798 my $pkg = ref($self) || $self;
799 warn " ** parse package is $pkg\n" if $self->debug;
801 # Reset at each top-level parsing request
807 $self->decompose( $self->query( shift() ) );
809 if ($self->floating_plan) {
810 $self->floating_plan->add_node( $self->parse_tree );
811 $self->parse_tree( $self->floating_plan );
814 warn "Query tree before pullup:\n" . Dumper($self->parse_tree) if $self->debug;
815 $self->parse_tree( $self->parse_tree->pullup );
816 $self->parse_tree->plan_level(0);
823 ($struct, $remainder) = $QParser->decompose($querystring, [$current_class], [$recursing], [$phrase_helper]);
825 This routine does the heavy work of parsing the query string recursively.
826 Returns the top level query plan, or the query plan from a lower level plus
827 the portion of the query string that needs to be processed at a higher level.
830 our $_compiled_decomposer = {};
833 my $pkg = ref($self) || $self;
835 my $r = $$_compiled_decomposer{$pkg};
836 my $compiled = defined($r);
839 my $current_class = shift || $self->default_search_class;
841 my $recursing = shift || 0;
842 my $phrase_helper = shift || 0;
844 warn ' 'x$recursing." ** QP: decompose package is $pkg" if $self->debug;
847 $r = $$_compiled_decomposer{$pkg} = {};
848 warn ' 'x$recursing." ** Compiling decomposer\n" if $self->debug;
850 # Build the search class+field uber-regexp
851 $$r{search_class_re} = '^\s*(';
853 warn ' 'x$recursing." ** Decomposer already compiled\n" if $self->debug;
859 for my $class ( keys %{$pkg->search_field_aliases} ) {
860 warn ' 'x$recursing." *** ... Looking for search fields in $class\n" if $self->debug;
862 for my $field ( keys %{$pkg->search_field_aliases->{$class}} ) {
863 warn ' 'x$recursing." *** ... Looking for aliases of $field\n" if $self->debug;
865 for my $alias ( @{$pkg->search_field_aliases->{$class}{$field}} ) {
866 my $aliasr = qr/$alias/;
867 s/(^|\s+)$aliasr\|/$1$class\|$field#$alias\|/g;
868 s/(^|\s+)$aliasr[:=]/$1$class\|$field#$alias:/g;
869 warn ' 'x$recursing." *** Rewriting: $alias ($aliasr) as $class\|$field\n" if $self->debug;
874 $$r{search_class_re} .= '|' unless ($first_class);
876 $$r{search_class_re} .= $class . '(?:[|#][^:|]+)*';
877 $seen_classes{$class} = 1;
881 for my $class ( keys %{$pkg->search_class_aliases} ) {
883 for my $alias ( @{$pkg->search_class_aliases->{$class}} ) {
884 my $aliasr = qr/$alias/;
885 s/(^|[^|])\b$aliasr\|/$1$class#$alias\|/g;
886 s/(^|[^|])\b$aliasr[:=]/$1$class#$alias:/g;
887 warn ' 'x$recursing." *** Rewriting: $alias ($aliasr) as $class\n" if $self->debug;
890 if (!$compiled and !$seen_classes{$class}) {
891 $$r{search_class_re} .= '|' unless ($first_class);
894 $$r{search_class_re} .= $class . '(?:[|#][^:|]+)*';
895 $seen_classes{$class} = 1;
898 $$r{search_class_re} .= '):' if (!$compiled);
900 warn ' 'x$recursing." ** Rewritten query: $_\n" if $self->debug;
902 my $group_start = $pkg->operator('group_start');
903 my $group_end = $pkg->operator('group_end');
905 warn ' 'x$recursing." ** Search class RE: $$r{search_class_re}\n" if $self->debug;
907 my $required_op = $pkg->operator('required');
908 $$r{required_re} = qr/\Q$required_op\E/;
910 my $disallowed_op = $pkg->operator('disallowed');
911 $$r{disallowed_re} = qr/\Q$disallowed_op\E/;
913 my $negated_op = $pkg->operator('negated');
914 $$r{negated_re} = qr/\Q$negated_op\E/;
916 my $and_op = $pkg->operator('and');
917 $$r{and_re} = qr/^\s*\Q$and_op\E/;
919 my $or_op = $pkg->operator('or');
920 $$r{or_re} = qr/^\s*\Q$or_op\E/;
922 $$r{group_start_re} = qr/^\s*($$r{negated_re}|$$r{disallowed_re})?\Q$group_start\E/;
924 $$r{group_end_re} = qr/^\s*\Q$group_end\E/;
926 my $float_start = $pkg->operator('float_start');
927 $$r{float_start_re} = qr/^\s*\Q$float_start\E/;
929 my $float_end = $pkg->operator('float_end');
930 $$r{float_end_re} = qr/^\s*\Q$float_end\E/;
932 $$r{atom_re} = qr/.+?(?=\Q$float_start\E|\Q$group_start\E|\Q$float_end\E|\Q$group_end\E|\s|"|$)/;
934 my $modifier_tag = $pkg->operator('modifier');
935 $$r{modifier_tag_re} = qr/^\s*\Q$modifier_tag\E/;
937 # Group start/end normally are ( and ), but can be overridden.
938 # We thus include ( and ) specifically due to filters, as well as : for classes.
939 $$r{phrase_cleanup_re} = qr/\s*(\Q$required_op\E|\Q$disallowed_op\E|\Q$and_op\E|\Q$or_op\E|\Q$group_start\E|\Q$group_end\E|\Q$float_start\E|\Q$float_end\E|\Q$modifier_tag\E|\Q$negated_op\E|:|\(|\))/;
941 # Build the filter and modifier uber-regexps
942 $$r{facet_re} = '^\s*(-?)((?:' . join( '|', @{$pkg->facet_classes}) . ')(?:\|\w+)*)\[(.+?)\](?!\[)';
944 $$r{filter_re} = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . ')\(([^()]+)\)';
945 $$r{filter_as_class_re} = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . '):\s*(\S+)';
947 $$r{modifier_re} = '^\s*'.$$r{modifier_tag_re}.'(' . join( '|', @{$pkg->modifiers}) . ')\b';
948 $$r{modifier_as_class_re} = '^\s*(' . join( '|', @{$pkg->modifiers}) . '):\s*(\S+)';
952 my $struct = shift || $self->new_plan( level => $recursing );
953 $self->parse_tree( $struct ) if (!$self->parse_tree);
958 while (!$remainder) {
960 warn ' 'x$recursing."Start of the loop. loop: $loops last_type: $last_type, joiner: ".$struct->joiner.", struct: $struct\n" if $self->debug;
961 if ($loops > 1000) { # the most magical of numbers...
962 warn ' 'x$recursing." got to $loops loops; aborting\n" if $self->debug;
965 if ($last_type eq 'FEND' and $fstart and $fstart != $struct) { # fall back further
968 } elsif ($last_type eq 'FEND') {
973 if (/^\s*$/) { # end of an explicit group
976 } elsif (/$$r{float_end_re}/) { # end of an explicit group
977 warn ' 'x$recursing."Encountered explicit float end, remainder: $'\n" if $self->debug;
985 } elsif (/$$r{group_end_re}/) { # end of an explicit group
986 warn ' 'x$recursing."Encountered explicit group end, remainder: $'\n" if $self->debug;
992 } elsif ($self->filter_count && /$$r{filter_re}/) { # found a filter
993 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
995 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
999 my $params = [ split '[,]+', $3 ];
1001 if ($pkg->filter_callbacks->{$filter}) {
1002 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
1003 $_ = "$replacement $_" if ($replacement);
1005 $struct->new_filter( $filter => $params, $negate );
1010 } elsif ($self->filter_count && /$$r{filter_as_class_re}/) { # found a filter
1011 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
1013 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1017 my $params = [ split '[,]+', $3 ];
1019 if ($pkg->filter_callbacks->{$filter}) {
1020 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
1021 $_ = "$replacement $_" if ($replacement);
1023 $struct->new_filter( $filter => $params, $negate );
1027 } elsif ($self->modifier_count && /$$r{modifier_re}/) { # found a modifier
1028 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1031 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1032 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1034 $struct->new_modifier($1);
1038 } elsif ($self->modifier_count && /$$r{modifier_as_class_re}/) { # found a modifier
1039 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1044 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1045 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1046 } elsif ($2 =~ /^[ty1]/i) {
1047 $struct->new_modifier($mod);
1051 } elsif (/$$r{float_start_re}/) { # start of an explicit float
1052 warn ' 'x$recursing."Encountered explicit float start\n" if $self->debug;
1056 $last_class = $current_class;
1057 $current_class = undef;
1059 $self->floating_plan( $self->new_plan( floating => 1 ) ) if (!$self->floating_plan);
1061 # pass the floating_plan struct to be modified by the float'ed chunk
1062 my ($floating_plan, $subremainder) = $self->new( debug => $self->debug )->decompose( $', undef, undef, undef, $self->floating_plan);
1064 warn ' 'x$recursing."Remainder after explicit float: $_\n" if $self->debug;
1066 $current_class = $last_class;
1069 } elsif (/$$r{group_start_re}/) { # start of an explicit group
1070 warn ' 'x$recursing."Encountered explicit group start\n" if $self->debug;
1072 if ($last_type eq 'CLASS') {
1073 warn ' 'x$recursing."Previous class change generated an empty node. Removing...\n" if $self->debug;
1074 $struct->remove_last_node;
1078 my ($substruct, $subremainder) = $self->decompose( $', $current_class, $recursing + 1 );
1079 $substruct->negate(1) if ($substruct && $negate);
1080 $substruct->explicit(1) if ($substruct);
1081 $struct->add_node( $substruct ) if ($substruct);
1083 warn ' 'x$recursing."Query remainder after bool group: $_\n" if $self->debug;
1087 } elsif (/$$r{and_re}/) { # ANDed expression
1089 warn ' 'x$recursing."Encountered AND\n" if $self->debug;
1090 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1091 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1094 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1096 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1097 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 1 );
1100 warn ' 'x$recursing."RHS built\n" if $self->debug;
1101 warn ' 'x$recursing."Post-AND remainder: $subremainder\n" if $self->debug;
1103 my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '&' );
1105 if ($LHS->floating) {
1106 $wrapper->{query} = $LHS->{query};
1107 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '&' );
1108 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1109 $LHS->{query} = [$outer_wrapper];
1112 $wrapper->add_node($_) for ($LHS, $RHS);
1113 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1114 $struct = $self->new_plan( level => $recursing );
1115 $struct->add_node($wrapper);
1118 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1121 } elsif (/$$r{or_re}/) { # ORed expression
1123 warn ' 'x$recursing."Encountered OR\n" if $self->debug;
1124 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1125 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1128 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1130 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1131 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 2 );
1134 warn ' 'x$recursing."RHS built\n" if $self->debug;
1135 warn ' 'x$recursing."Post-OR remainder: $subremainder\n" if $self->debug;
1137 my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1139 if ($LHS->floating) {
1140 $wrapper->{query} = $LHS->{query};
1141 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1142 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1143 $LHS->{query} = [$outer_wrapper];
1146 $wrapper->add_node($_) for ($LHS, $RHS);
1147 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1148 $struct = $self->new_plan( level => $recursing );
1149 $struct->add_node($wrapper);
1152 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1155 } elsif ($self->facet_class_count && /$$r{facet_re}/) { # changing current class
1156 warn ' 'x$recursing."Encountered facet: $1$2 => $3\n" if $self->debug;
1158 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1160 my $facet_value = [ split '\s*\]\[\s*', $3 ];
1161 $struct->new_facet( $facet => $facet_value, $negate );
1165 } elsif ($self->search_class_count && /$$r{search_class_re}/) { # changing current class
1167 if ($last_type eq 'CLASS') {
1168 $struct->remove_last_node( $current_class );
1169 warn ' 'x$recursing."Encountered class change with no searches!\n" if $self->debug;
1172 warn ' 'x$recursing."Encountered class change: $1\n" if $self->debug;
1174 $current_class = $struct->classed_node( $1 )->requested_class();
1177 $last_type = 'CLASS';
1178 } elsif (/^\s*($$r{required_re}|$$r{disallowed_re}|$$r{negated_re})?"([^"]+)(?:"|$)/) { # phrase, always anded
1179 warn ' 'x$recursing.'Encountered' . ($1 ? " ['$1' modified]" : '') . " phrase: $2\n" if $self->debug;
1181 my $req_ness = $1 || '';
1182 $req_ness = $pkg->operator('disallowed') if ($req_ness eq $pkg->operator('negated'));
1185 if (!$phrase_helper) {
1186 warn ' 'x$recursing."Recursing into decompose with the phrase as a subquery\n" if $self->debug;
1188 my ($substruct, $subremainder) = $self->decompose( qq/$req_ness"$phrase"/, $current_class, $recursing + 1, 1 );
1189 $struct->add_node( $substruct ) if ($substruct);
1192 warn ' 'x$recursing."Directly parsing the phrase [ $phrase ] subquery\n" if $self->debug;
1193 $struct->joiner( '&' );
1195 my $class_node = $struct->classed_node($current_class);
1197 if ($req_ness eq $pkg->operator('disallowed')) {
1198 $class_node->negate(1);
1200 $class_node->add_phrase( $phrase );
1202 # Save $' before we clean up $phrase
1205 # Cleanup the phrase to make it so that we don't parse things in it as anything other than atoms
1206 $phrase =~ s/$$r{phrase_cleanup_re}/ /g;
1208 $_ = $phrase . $temp_val;
1214 } elsif (/^\s*($$r{required_re}|$$r{disallowed_re})($$r{atom_re})/) { # convert require/disallow word to {un}phrase
1215 warn ' 'x$recursing."Encountered required atom (mini phrase), transforming for phrase parse: $1\n" if $self->debug;
1217 $_ = $1 . '"' . $2 . '"' . $';
1220 } elsif (/^\s*($$r{atom_re})/) { # atom
1221 warn ' 'x$recursing."Encountered atom: $1\n" if $self->debug;
1222 warn ' 'x$recursing."Remainder: $'\n" if $self->debug;
1230 my $class_node = $struct->classed_node($current_class);
1232 my $prefix = ($atom =~ s/^$$r{negated_re}//o) ? '!' : '';
1233 my $truncate = ($atom =~ s/\*$//o) ? '*' : '';
1235 if ($atom ne '' and !grep { $atom =~ /^\Q$_\E+$/ } ('&','|')) { # throw away & and |, not allowed in tsquery, and not really useful anyway
1236 # $class_node->add_phrase( $atom ) if ($atom =~ s/^$$r{required_re}//o);
1238 $class_node->add_fts_atom( $atom, suffix => $truncate, prefix => $prefix, node => $class_node );
1239 $struct->joiner( '&' );
1244 warn ' 'x$recursing."Cannot parse: $_\n" if $self->debug;
1253 scalar(@{$struct->query_nodes}) == 0 &&
1254 scalar(@{$struct->filters}) == 0 &&
1257 return $struct if !wantarray;
1258 return ($struct, $remainder);
1261 =head2 find_class_index
1263 $index = $QParser->find_class_index($class, $query);
1266 sub find_class_index {
1270 my ($class_part, @field_parts) = split '\|', $class;
1271 $class_part ||= $class;
1273 for my $idx ( 0 .. scalar(@$query) - 1 ) {
1274 next unless ref($$query[$idx]);
1275 return $idx if ( $$query[$idx]{requested_class} && $class eq $$query[$idx]{requested_class} );
1278 push(@$query, { classname => $class_part, (@field_parts ? (fields => \@field_parts) : ()), requested_class => $class, ftsquery => [], phrases => [] });
1284 $limit = $QParser->core_limit([$limit]);
1286 Return and/or set the core_limit.
1292 $self->{core_limit} = $l if ($l);
1293 return $self->{core_limit};
1298 $superpage = $QParser->superpage([$superpage]);
1300 Return and/or set the superpage.
1306 $self->{superpage} = $l if ($l);
1307 return $self->{superpage};
1310 =head2 superpage_size
1312 $size = $QParser->superpage_size([$size]);
1314 Return and/or set the superpage size.
1317 sub superpage_size {
1320 $self->{superpage_size} = $l if ($l);
1321 return $self->{superpage_size};
1325 #-------------------------------
1326 package QueryParser::_util;
1328 # At this level, joiners are always & or |. This is not
1329 # the external, configurable representation of joiners that
1330 # defaults to # && and ||.
1334 return (not ref $str and ($str eq '&' or $str eq '|'));
1337 sub default_joiner { '&' }
1339 # 0 for different, 1 for the same.
1340 sub compare_abstract_atoms {
1341 my ($left, $right) = @_;
1343 foreach (qw/prefix suffix content/) {
1344 no warnings; # undef can stand in for '' here
1345 return 0 unless $left->{$_} eq $right->{$_};
1351 sub fake_abstract_atom_from_phrase {
1354 my $qp_class = shift || 'QueryParser';
1359 $QueryParser::parser_config{$qp_class}{operators}{disallowed} .
1364 "type" => "atom", "prefix" => $prefix, "suffix" => '"',
1365 "content" => $phrase
1369 sub find_arrays_in_abstract {
1373 foreach my $key (keys %$hash) {
1374 if (ref $hash->{$key} eq "ARRAY") {
1375 push @arrays, $hash->{$key};
1376 foreach (@{$hash->{$key}}) {
1377 push @arrays, find_arrays_in_abstract($_);
1385 #-------------------------------
1386 package QueryParser::Canonicalize; # not OO
1389 sub _abstract_query2str_filter {
1391 my $qp_class = shift || 'QueryParser';
1392 my $qpconfig = $QueryParser::parser_config{$qp_class};
1396 $f->{negate} ? $qpconfig->{operators}{disallowed} : "",
1398 join(",", @{$f->{args}})
1402 sub _abstract_query2str_modifier {
1404 my $qp_class = shift || 'QueryParser';
1405 my $qpconfig = $QueryParser::parser_config{$qp_class};
1407 return $qpconfig->{operators}{modifier} . $f;
1411 my $children = shift;
1412 my $op = (keys %$children)[0];
1413 return @{$$children{$op}};
1417 # This should produce an equivalent query to the original, given an
1419 sub abstract_query2str_impl {
1420 my $abstract_query = shift;
1421 my $depth = shift || 0;
1423 my $qp_class ||= shift || 'QueryParser';
1424 my $force_qp_node = shift || 0;
1425 my $qpconfig = $QueryParser::parser_config{$qp_class};
1427 my $fs = $qpconfig->{operators}{float_start};
1428 my $fe = $qpconfig->{operators}{float_end};
1429 my $gs = $qpconfig->{operators}{group_start};
1430 my $ge = $qpconfig->{operators}{group_end};
1431 my $and = $qpconfig->{operators}{and};
1432 my $or = $qpconfig->{operators}{or};
1433 my $ng = $qpconfig->{operators}{negated};
1440 if (exists $abstract_query->{type}) {
1441 if ($abstract_query->{type} eq 'query_plan') {
1442 $q .= join(" ", map { _abstract_query2str_filter($_, $qp_class) } @{$abstract_query->{filters}}) if
1443 exists $abstract_query->{filters};
1445 $q .= ($q ? ' ' : '') . join(" ", map { _abstract_query2str_modifier($_, $qp_class) } @{$abstract_query->{modifiers}}) if
1446 exists $abstract_query->{modifiers};
1448 $size = _kid_list($abstract_query->{children});
1449 if ($abstract_query->{negate}) {
1453 $isnode = 1 if ($size > 1 and ($force_qp_node or $depth));
1454 #warn "size: $size, depth: $depth, isnode: $isnode, AQ: ".Dumper($abstract_query);
1455 } elsif ($abstract_query->{type} eq 'node') {
1456 if ($abstract_query->{alias}) {
1457 $q .= ($q ? ' ' : '') . $abstract_query->{alias};
1458 $q .= "|$_" foreach @{$abstract_query->{alias_fields}};
1460 $q .= ($q ? ' ' : '') . $abstract_query->{class};
1461 $q .= "|$_" foreach @{$abstract_query->{fields}};
1465 } elsif ($abstract_query->{type} eq 'atom') {
1466 my $prefix = $abstract_query->{prefix} || '';
1467 $prefix = $qpconfig->{operators}{negated} if $prefix eq '!';
1468 $q .= ($q ? ' ' : '') . $prefix .
1469 ($abstract_query->{content} || '') .
1470 ($abstract_query->{suffix} || '');
1471 } elsif ($abstract_query->{type} eq 'facet') {
1472 my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
1473 $q .= ($q ? ' ' : '') . $prefix . $abstract_query->{name} . "[" .
1474 join("][", @{$abstract_query->{values}}) . "]";
1478 my $next_depth = int($size > 1);
1480 if (exists $abstract_query->{children}) {
1482 my $op = (keys(%{$abstract_query->{children}}))[0];
1484 if ($abstract_query->{floating}) { # always the top node!
1485 my $sub_node = pop @{$abstract_query->{children}{$op}};
1487 $abstract_query->{floating} = 0;
1488 $q = $fs . " " . abstract_query2str_impl($abstract_query,0,$qp_class, 1) . $fe. " ";
1490 $abstract_query = $sub_node;
1493 if ($abstract_query && exists $abstract_query->{children}) {
1494 $op = (keys(%{$abstract_query->{children}}))[0];
1495 $q .= ($q ? ' ' : '') . join(
1496 ($op eq '&' ? ' ' : " $or "),
1498 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1499 } @{$abstract_query->{children}{$op}}
1502 } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1503 my $op = (keys(%{$abstract_query}))[0];
1504 $q .= ($q ? ' ' : '') . join(
1505 ($op eq '&' ? ' ' : " $or "),
1507 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1508 } @{$abstract_query->{$op}}
1512 $q = "$gs$q$ge" if ($isnode);
1513 $q = $negate . $q if ($q);;
1518 #-------------------------------
1519 package QueryParser::query_plan;
1521 $Data::Dumper::Indent = 0;
1523 sub get_abstract_data {
1526 return $self->{abstract_data}{$key};
1529 sub set_abstract_data {
1533 $self->{abstract_data}{$key} = $value;
1538 return @{$self->filters} == 0 &&
1539 @{$self->modifiers} == 0 &&
1540 @{[map { @{$_->phrases} } grep { ref($_) && $_->isa('QueryParser::query_plan::node')} @{$self->query_nodes}]} == 0
1545 my( $left, $right ) = @_;
1546 return 0 if scalar @$left != scalar @$right;
1548 @hash{ @$left, @$right } = ();
1549 return scalar keys %hash == scalar @$left;
1554 my $current_joiner = shift;
1556 # burrow down until we our kids have no subqueries
1557 my $downlink_joiner;
1558 for my $qnode (@{ $self->query_nodes }) {
1559 $downlink_joiner = $qnode if (!ref($qnode));
1560 if (ref($qnode) && $qnode->can('pullup')) {
1561 $qnode->pullup($downlink_joiner);
1565 warn "Entering pullup depth ". $self->plan_level . "\n"
1566 if $self->QueryParser->debug;
1568 my $old_qnodes = $self->query_nodes; # we will ignore all but ::node objects in this list
1569 warn @$old_qnodes . " plans at pullup depth ". $self->plan_level . "\n"
1570 if $self->QueryParser->debug;
1572 # PASS 0: If I only have one child, collapse filters/modifiers into me
1573 if (@$old_qnodes == 1) {
1574 my $kid = $$old_qnodes[0];
1575 if ($kid->isa('QueryParser::query_plan')) {
1576 $self->add_filter($_) foreach @{$kid->filters};
1577 $self->add_facet($_) foreach @{$kid->facets};
1578 $self->add_modifier($_) foreach @{$kid->modifiers};
1579 $kid->{filters} = [];
1580 $kid->{facets} = [];
1581 $kid->{modifiers} = [];
1583 my $kid_qnodes = $kid->query_nodes;
1584 if (@$kid_qnodes == 1) { # And if my kid is a plan with only one node, absorb that
1585 my $a = $$kid_qnodes[0];
1586 if ($a->isa('QueryParser::query_plan')) {
1587 $self->{query} = [$a];
1594 # PASS 1: loop, attempting to pull up simple nodes
1601 while (my $p = shift(@$old_qnodes)) {
1603 # joiners and ::node's get pushed onto the stack of new nodes
1604 if (!ref($p) or !$p->isa('QueryParser::query_plan')) {
1605 push @new_nodes, $p;
1609 # keep explicit and floating plans
1610 if ($p->explicit or $p->floating) {
1611 push @new_nodes, $p;
1615 if ($p->atoms_only) {
1617 # 1-node plans get pulled up regardless of the plan's joiner
1618 if (@{$p->query_nodes} == 1) {
1619 for my $a (@{$p->query_nodes}) {
1620 if (ref($a) and $a->can('plan')) {
1623 push @new_nodes, $a;
1628 # gather the joiners
1629 my %joiners = ( '&' => 0, '|' => 0 );
1630 my @nodelist = @{$p->query_nodes};
1631 while (my $n = shift(@nodelist)) {
1632 next if ref($n); # only look at joiners
1636 if (!($joiners{'&'} > 0 and $joiners{'|'} > 0)) { # mix of joiners? stop
1637 if ($joiners{$self->joiner} > 0) { # needs to be our joiner in use
1638 for my $a (@{$p->query_nodes}) {
1639 if (ref($a) and $a->can('plan')) {
1642 push @new_nodes, $a;
1649 # default is to keep the whole plan
1650 push @new_nodes, $p;
1653 warn @new_nodes . " nodes after pullup of simple nodes at depth ". $self->plan_level . "\n"
1654 if $self->QueryParser->debug;
1656 # PASS 2: merge adjacent ::node's
1658 my $sync_node = $prev_joiner = undef;
1659 $old_qnodes = [@new_nodes];
1661 while ( my $n = shift(@$old_qnodes) ) {
1665 $prev_joiner = $current_joiner;
1666 $current_joiner = $n;
1667 warn "Joiner, recording it. [$prev_joiner => $current_joiner]\n" if $self->QueryParser->debug;
1671 # ::plan's etc get pushed onto the stack of new nodes
1672 if (!$n->isa('QueryParser::query_plan::node')) {
1673 push @new_nodes, $current_joiner if (@new_nodes);
1674 push @new_nodes, $n;
1676 warn "Not a ::node, pushing onto the stack [$n]\n" if $self->QueryParser->debug;
1680 # grab the current target node
1682 warn "No sync_node, picking a new one\n" if $self->QueryParser->debug;
1684 push @new_nodes, $current_joiner if (@new_nodes);
1685 push @new_nodes, $n;
1689 if (@{$n->query_atoms} == 0) {
1690 warn "weird ... empty node ...skipping\n" if $self->QueryParser->debug;
1691 push @new_nodes, $current_joiner if (@new_nodes);
1696 my $sync_joiner = $sync_node->effective_joiner;
1697 my $n_joiner = $n->effective_joiner;
1699 # plans of a different class or field set stay where they are
1700 if ($sync_node->classname ne $n->classname or !_identical($sync_node->fields,$n->fields)) {
1701 warn "Class/Field change! Need a new sync_node\n" if $self->QueryParser->debug;
1702 push @new_nodes, $current_joiner;
1703 push @new_nodes, $n;
1709 if (!$sync_joiner or !$n_joiner) { # a node has a mix ... can't merge either
1710 warn "Mixed joiners, need a new sync_node\n" if $self->QueryParser->debug;
1711 push @new_nodes, $current_joiner;
1712 push @new_nodes, $n;
1716 } elsif ($sync_joiner ne $n_joiner) { # different joiners, can't merge
1717 warn "Differing joiners, need a new sync_node\n" if $self->QueryParser->debug;
1718 push @new_nodes, $current_joiner;
1719 push @new_nodes, $n;
1725 # we can push the next ::node's atoms onto our stack
1726 push @{$sync_node->query_atoms}, $current_joiner;
1727 for my $a (@{$n->query_atoms}) {
1729 $a->{node} = $sync_node;
1731 push @{$sync_node->query_atoms}, $a;
1734 warn "Merged ".@{$n->query_atoms}." atoms into sync_node\n" if $self->QueryParser->debug;
1739 push @new_nodes, $sync_node if ($dangling && $sync_node != $new_nodes[-1]);
1741 warn @new_nodes . " nodes at pullup depth ". $self->plan_level . " after compression\n"
1742 if $self->QueryParser->debug;
1744 $self->{query} = \@new_nodes;
1750 return undef unless ref($self);
1751 return $self->{QueryParser};
1756 $pkg = ref($pkg) || $pkg;
1757 my %args = (abstract_data => {}, query => [], joiner => '&', @_);
1759 return bless \%args => $pkg;
1764 my $pkg = ref($self) || $self;
1765 my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1766 $self->add_node( $node );
1772 my $pkg = ref($self) || $self;
1777 my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1778 $self->add_node( $node );
1785 my $pkg = ref($self) || $self;
1790 my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1791 $self->add_filter( $node );
1797 sub _merge_filters {
1798 my $left_filter = shift;
1799 my $right_filter = shift;
1802 return undef unless $left_filter or $right_filter;
1803 return $right_filter unless $left_filter;
1804 return $left_filter unless $right_filter;
1806 my $args = $left_filter->{args} || [];
1809 push(@$args, @{$right_filter->{args}});
1812 # find the intersect values
1814 map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1815 $args = [ grep { $new_vals{$_} } @$args ];
1818 $left_filter->{args} = $args;
1819 return $left_filter;
1822 sub collapse_filters {
1826 # start by merging any filters at this level.
1827 # like-level filters are always ORed together
1830 my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1832 $cur_filter = shift @cur_filters;
1833 my $args = $cur_filter->{args} || [];
1834 $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1837 # next gather the collapsed filters from sub-plans and
1838 # merge them with our own
1840 my @subquery = @{$self->{query}};
1843 my $blob = shift @subquery;
1844 shift @subquery; # joiner
1845 next unless $blob->isa('QueryParser::query_plan');
1846 my $sub_filter = $blob->collapse_filters($name);
1847 $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1850 if ($self->QueryParser->debug) {
1851 my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1852 warn "collapse_filters($name) => [@args]\n";
1860 my $needle = shift;;
1861 return undef unless ($needle);
1863 my $filter = $self->collapse_filters($needle);
1865 warn "find_filter($needle) => " .
1866 (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n"
1867 if $self->QueryParser->debug;
1869 return $filter ? ($filter) : ();
1874 my $needle = shift;;
1875 return undef unless ($needle);
1876 return grep { $_->name eq $needle } @{ $self->modifiers };
1881 my $pkg = ref($self) || $self;
1884 my $node = do{$pkg.'::modifier'}->new( $name );
1885 $self->add_modifier( $node );
1892 my $requested_class = shift;
1895 for my $n (@{$self->{query}}) {
1896 next unless (ref($n) && $n->isa( 'QueryParser::query_plan::node' ));
1897 if ($n->requested_class eq $requested_class) {
1904 $node = $self->new_node;
1905 $node->requested_class( $requested_class );
1911 sub remove_last_node {
1913 my $requested_class = shift;
1915 my $old = pop(@{$self->query_nodes});
1916 pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1923 return $self->{query};
1929 $self->{floating} = $f if (defined $f);
1930 return $self->{floating};
1936 $self->{explicit} = $f if (defined $f);
1937 return $self->{explicit};
1944 $self->{query} ||= [];
1946 push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1947 push(@{$self->{query}}, $node);
1956 return $self->{level} ? 0 : 1;
1963 if (defined $level) {
1964 $self->{level} = $level;
1965 for (@{$self->query_nodes}) {
1966 $_->plan_level($level + 1) if (ref and $_->isa('QueryParser::query_plan'));
1970 return $self->{level};
1977 $self->{joiner} = $joiner if ($joiner);
1978 return $self->{joiner};
1983 $self->{modifiers} ||= [];
1984 return $self->{modifiers};
1989 my $modifier = shift;
1991 $self->{modifiers} ||= [];
1992 $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1994 push(@{$self->{modifiers}}, $modifier);
2001 $self->{facets} ||= [];
2002 return $self->{facets};
2009 $self->{facets} ||= [];
2010 $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
2012 push(@{$self->{facets}}, $facet);
2019 $self->{filters} ||= [];
2020 return $self->{filters};
2027 $self->{filters} ||= [];
2029 push(@{$self->{filters}}, $filter);
2038 $self->{negate} = $negate if (defined $negate);
2040 return $self->{negate};
2043 # %opts supports two options at this time:
2045 # If true, do not do anything to the phrases
2046 # fields on any discovered nodes.
2048 # If true, also return the query parser config as part of the blob.
2049 # This will get set back to 0 before recursion to avoid repetition.
2050 sub to_abstract_query {
2054 my $pkg = ref $self->QueryParser || $self->QueryParser;
2056 my $abstract_query = {
2057 type => "query_plan",
2058 floating => $self->floating,
2059 level => $self->plan_level,
2060 filters => [map { $_->to_abstract_query } @{$self->filters}],
2061 modifiers => [map { $_->to_abstract_query } @{$self->modifiers}],
2062 negate => $self->negate
2065 if ($opts{with_config}) {
2066 $opts{with_config} = 0;
2067 $abstract_query->{config} = $QueryParser::parser_config{$pkg};
2072 for my $qnode (@{$self->query_nodes}) {
2073 # Remember: qnode can be a joiner string, a node, or another query_plan
2075 if (QueryParser::_util::is_joiner($qnode)) {
2076 if ($abstract_query->{children}) {
2077 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2078 next if $open_joiner eq $qnode;
2080 my $oldroot = $abstract_query->{children};
2082 $abstract_query->{children} = {$qnode => $kids};
2084 $abstract_query->{children} = {$qnode => $kids};
2087 push @$kids, $qnode->to_abstract_query(%opts);
2091 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2092 $$abstract_query{additional_data} = $self->{abstract_data}
2093 if (keys(%{$self->{abstract_data}}));
2095 return $abstract_query;
2099 #-------------------------------
2100 package QueryParser::query_plan::node;
2102 $Data::Dumper::Indent = 0;
2104 sub effective_joiner {
2107 my @nodelist = @{$node->query_atoms};
2108 return $node->plan->joiner if (@nodelist == 1);
2110 # gather the joiners
2111 my %joiners = ( '&' => 0, '|' => 0 );
2112 while (my $n = shift(@nodelist)) {
2113 next if ref($n); # only look at joiners
2117 if (!($joiners{'&'} > 0 and $joiners{'|'} > 0)) { # no mix of joiners
2118 return '|' if ($joiners{'|'});
2127 $pkg = ref($pkg) || $pkg;
2130 return bless \%args => $pkg;
2135 my $pkg = ref($self) || $self;
2136 return do{$pkg.'::atom'}->new( @_ );
2139 sub requested_class { # also split into classname, fields and alias
2145 my (undef, $alias) = split '#', $class;
2147 $class =~ s/#[^|]+//;
2148 ($alias, @afields) = split '\|', $alias;
2151 my @fields = @afields;
2152 my ($class_part, @field_parts) = split '\|', $class;
2153 for my $f (@field_parts) {
2154 push(@fields, $f) unless (grep { $f eq $_ } @fields);
2157 $class_part ||= $class;
2159 $self->{requested_class} = $class;
2160 $self->{alias} = $alias if $alias;
2161 $self->{alias_fields} = \@afields if $alias;
2162 $self->{classname} = $class_part;
2163 $self->{fields} = \@fields;
2166 return $self->{requested_class};
2173 $self->{plan} = $plan if ($plan);
2174 return $self->{plan};
2181 $self->{alias} = $alias if ($alias);
2182 return $self->{alias};
2189 $self->{alias_fields} = $alias if ($alias);
2190 return $self->{alias_fields};
2197 $self->{classname} = $class if ($class);
2198 return $self->{classname};
2205 $self->{fields} ||= [];
2206 $self->{fields} = \@fields if (@fields);
2207 return $self->{fields};
2214 $self->{phrases} ||= [];
2215 $self->{phrases} = \@phrases if (@phrases);
2216 return $self->{phrases};
2223 push(@{$self->phrases}, $phrase);
2232 $self->{negate} = $negate if (defined $negate);
2234 return $self->{negate};
2239 my @query_atoms = @_;
2241 $self->{query_atoms} ||= [];
2242 $self->{query_atoms} = \@query_atoms if (@query_atoms);
2243 return $self->{query_atoms};
2251 my $content = $atom;
2254 $atom = $self->new_atom( content => $content, @parts );
2257 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
2258 push(@{$self->query_atoms}, $atom);
2263 sub add_dummy_atom {
2267 my $atom = $self->new_atom( @parts, dummy => 1 );
2269 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
2270 push(@{$self->query_atoms}, $atom);
2275 # This will find up to one occurence of @$short_list within @$long_list, and
2276 # replace it with the single atom $replacement.
2277 sub replace_phrase_in_abstract_query {
2278 my ($self, $short_list, $long_list, $replacement) = @_;
2282 my $goal = scalar @$short_list;
2284 for (my $i = 0; $i < scalar (@$long_list); $i++) {
2285 my $right = $long_list->[$i];
2287 if (QueryParser::_util::compare_abstract_atoms(
2288 $short_list->[scalar @already], $right
2291 } elsif (scalar @already) {
2296 if (scalar @already == $goal) {
2297 splice @$long_list, $already[0], scalar(@already), $replacement;
2306 sub to_abstract_query {
2310 my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
2312 my $abstract_query = {
2314 "alias" => $self->alias,
2315 "alias_fields" => $self->alias_fields,
2316 "class" => $self->classname,
2317 "fields" => $self->fields
2320 $self->abstract_node_additions($abstract_query)
2321 if ($self->can('abstract_node_additions'));
2325 for my $qatom (@{$self->query_atoms}) {
2326 if (QueryParser::_util::is_joiner($qatom)) {
2327 if ($abstract_query->{children}) {
2328 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2329 next if $open_joiner eq $qatom;
2331 my $oldroot = $abstract_query->{children};
2333 $abstract_query->{children} = {$qatom => $kids};
2335 $abstract_query->{children} = {$qatom => $kids};
2338 push @$kids, $qatom->to_abstract_query;
2342 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2344 if ($self->{phrases} and not $opts{no_phrases}) {
2345 for my $phrase (@{$self->{phrases}}) {
2346 # Phrases appear duplication in a real QP tree, and we don't want
2347 # that duplication in our abstract query. So for all our phrases,
2348 # break them into atoms as QP would, and remove any matching
2349 # sequences of atoms from our abstract query.
2351 my $tmp_prefix = '';
2352 $tmp_prefix = $QueryParser::parser_config{$pkg}{operators}{disallowed} if ($self->{negate});
2354 my $tmptree = $self->{plan}->{QueryParser}->new(query => $tmp_prefix.'"'.$phrase.'"')->parse->parse_tree;
2356 # For a well-behaved phrase, we should now have only one node
2357 # in the $tmptree query plan, and that node should have an
2358 # orderly list of atoms and joiners.
2360 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2364 $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2366 )->{children}->{'&'}->[0]->{children}->{'&'};
2371 QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
2373 last if $self->replace_phrase_in_abstract_query(
2376 QueryParser::_util::fake_abstract_atom_from_phrase($phrase, $self->{negate}, $pkg)
2384 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2385 return $abstract_query;
2388 #-------------------------------
2389 package QueryParser::query_plan::node::atom;
2393 $pkg = ref($pkg) || $pkg;
2396 return bless \%args => $pkg;
2401 return undef unless (ref $self);
2402 return $self->{node};
2407 return undef unless (ref $self);
2408 return $self->{content};
2413 return undef unless (ref $self);
2414 return $self->{prefix};
2419 return undef unless (ref $self);
2420 return $self->{suffix};
2423 sub to_abstract_query {
2427 (map { $_ => $self->$_ } qw/prefix suffix content/),
2431 #-------------------------------
2432 package QueryParser::query_plan::filter;
2436 $pkg = ref($pkg) || $pkg;
2439 return bless \%args => $pkg;
2444 return $self->{plan};
2449 return $self->{name};
2454 return $self->{negate};
2459 return $self->{args};
2462 sub to_abstract_query {
2466 map { $_ => $self->$_ } qw/name negate args/
2470 #-------------------------------
2471 package QueryParser::query_plan::facet;
2475 $pkg = ref($pkg) || $pkg;
2478 return bless \%args => $pkg;
2483 return $self->{plan};
2488 return $self->{name};
2493 return $self->{negate};
2498 return $self->{'values'};
2501 sub to_abstract_query {
2505 (map { $_ => $self->$_ } qw/name negate values/),
2510 #-------------------------------
2511 package QueryParser::query_plan::modifier;
2515 $pkg = ref($pkg) || $pkg;
2516 my $modifier = shift;
2519 return bless { name => $modifier, negate => $negate } => $pkg;
2524 return $self->{name};
2529 return $self->{negate};
2532 sub to_abstract_query {