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 warn "Query tree after pullup:\n" . Dumper($self->parse_tree) if $self->debug;
817 $self->parse_tree->plan_level(0);
824 ($struct, $remainder) = $QParser->decompose($querystring, [$current_class], [$recursing], [$phrase_helper]);
826 This routine does the heavy work of parsing the query string recursively.
827 Returns the top level query plan, or the query plan from a lower level plus
828 the portion of the query string that needs to be processed at a higher level.
831 our $_compiled_decomposer = {};
834 my $pkg = ref($self) || $self;
836 my $r = $$_compiled_decomposer{$pkg};
837 my $compiled = defined($r);
840 my $current_class = shift || $self->default_search_class;
842 my $recursing = shift || 0;
843 my $phrase_helper = shift || 0;
845 warn ' 'x$recursing." ** QP: decompose package is $pkg" if $self->debug;
848 $r = $$_compiled_decomposer{$pkg} = {};
849 warn ' 'x$recursing." ** Compiling decomposer\n" if $self->debug;
851 # Build the search class+field uber-regexp
852 $$r{search_class_re} = '^\s*(';
854 warn ' 'x$recursing." ** Decomposer already compiled\n" if $self->debug;
860 for my $class ( keys %{$pkg->search_field_aliases} ) {
861 warn ' 'x$recursing." *** ... Looking for search fields in $class\n" if $self->debug;
863 for my $field ( keys %{$pkg->search_field_aliases->{$class}} ) {
864 warn ' 'x$recursing." *** ... Looking for aliases of $field\n" if $self->debug;
866 for my $alias ( @{$pkg->search_field_aliases->{$class}{$field}} ) {
867 my $aliasr = qr/$alias/;
868 s/(^|\s+)$aliasr\|/$1$class\|$field#$alias\|/g;
869 s/(^|\s+)$aliasr[:=]/$1$class\|$field#$alias:/g;
870 warn ' 'x$recursing." *** Rewriting: $alias ($aliasr) as $class\|$field\n" if $self->debug;
875 $$r{search_class_re} .= '|' unless ($first_class);
877 $$r{search_class_re} .= $class . '(?:[|#][^:|]+)*';
878 $seen_classes{$class} = 1;
882 for my $class ( keys %{$pkg->search_class_aliases} ) {
884 for my $alias ( @{$pkg->search_class_aliases->{$class}} ) {
885 my $aliasr = qr/$alias/;
886 s/(^|[^|])\b$aliasr\|/$1$class#$alias\|/g;
887 s/(^|[^|])\b$aliasr[:=]/$1$class#$alias:/g;
888 warn ' 'x$recursing." *** Rewriting: $alias ($aliasr) as $class\n" if $self->debug;
891 if (!$compiled and !$seen_classes{$class}) {
892 $$r{search_class_re} .= '|' unless ($first_class);
895 $$r{search_class_re} .= $class . '(?:[|#][^:|]+)*';
896 $seen_classes{$class} = 1;
899 $$r{search_class_re} .= '):' if (!$compiled);
901 warn ' 'x$recursing." ** Rewritten query: $_\n" if $self->debug;
903 my $group_start = $pkg->operator('group_start');
904 my $group_end = $pkg->operator('group_end');
906 warn ' 'x$recursing." ** Search class RE: $$r{search_class_re}\n" if $self->debug;
908 my $required_op = $pkg->operator('required');
909 $$r{required_re} = qr/\Q$required_op\E/;
911 my $disallowed_op = $pkg->operator('disallowed');
912 $$r{disallowed_re} = qr/\Q$disallowed_op\E/;
914 my $negated_op = $pkg->operator('negated');
915 $$r{negated_re} = qr/\Q$negated_op\E/;
917 my $and_op = $pkg->operator('and');
918 $$r{and_re} = qr/^\s*\Q$and_op\E/;
920 my $or_op = $pkg->operator('or');
921 $$r{or_re} = qr/^\s*\Q$or_op\E/;
923 $$r{group_start_re} = qr/^\s*($$r{negated_re}|$$r{disallowed_re})?\Q$group_start\E/;
925 $$r{group_end_re} = qr/^\s*\Q$group_end\E/;
927 my $float_start = $pkg->operator('float_start');
928 $$r{float_start_re} = qr/^\s*\Q$float_start\E/;
930 my $float_end = $pkg->operator('float_end');
931 $$r{float_end_re} = qr/^\s*\Q$float_end\E/;
933 $$r{atom_re} = qr/.+?(?=\Q$float_start\E|\Q$group_start\E|\Q$float_end\E|\Q$group_end\E|\s|"|$)/;
935 my $modifier_tag = $pkg->operator('modifier');
936 $$r{modifier_tag_re} = qr/^\s*\Q$modifier_tag\E/;
938 # Group start/end normally are ( and ), but can be overridden.
939 # We thus include ( and ) specifically due to filters, as well as : for classes.
940 $$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|:|\(|\))/;
942 # Build the filter and modifier uber-regexps
943 $$r{facet_re} = '^\s*(-?)((?:' . join( '|', @{$pkg->facet_classes}) . ')(?:\|\w+)*)\[(.+?)\](?!\[)';
945 $$r{filter_re} = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . ')\(([^()]+)\)';
946 $$r{filter_as_class_re} = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . '):\s*(\S+)';
948 $$r{modifier_re} = '^\s*'.$$r{modifier_tag_re}.'(' . join( '|', @{$pkg->modifiers}) . ')\b';
949 $$r{modifier_as_class_re} = '^\s*(' . join( '|', @{$pkg->modifiers}) . '):\s*(\S+)';
953 my $struct = shift || $self->new_plan( level => $recursing );
954 $self->parse_tree( $struct ) if (!$self->parse_tree);
959 while (!$remainder) {
961 warn ' 'x$recursing."Start of the loop. loop: $loops last_type: $last_type, joiner: ".$struct->joiner.", struct: $struct\n" if $self->debug;
962 if ($loops > 1000) { # the most magical of numbers...
963 warn ' 'x$recursing." got to $loops loops; aborting\n" if $self->debug;
966 if ($last_type eq 'FEND' and $fstart and $fstart != $struct) { # fall back further
969 } elsif ($last_type eq 'FEND') {
974 if (/^\s*$/) { # end of an explicit group
977 } elsif (/$$r{float_end_re}/) { # end of an explicit group
978 warn ' 'x$recursing."Encountered explicit float end, remainder: $'\n" if $self->debug;
986 } elsif (/$$r{group_end_re}/) { # end of an explicit group
987 warn ' 'x$recursing."Encountered explicit group end, remainder: $'\n" if $self->debug;
993 } elsif ($self->filter_count && /$$r{filter_re}/) { # found a filter
994 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
996 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1000 my $params = [ split '[,]+', $3 ];
1002 if ($pkg->filter_callbacks->{$filter}) {
1003 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
1004 $_ = "$replacement $_" if ($replacement);
1006 $struct->new_filter( $filter => $params, $negate );
1011 } elsif ($self->filter_count && /$$r{filter_as_class_re}/) { # found a filter
1012 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
1014 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1018 my $params = [ split '[,]+', $3 ];
1020 if ($pkg->filter_callbacks->{$filter}) {
1021 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
1022 $_ = "$replacement $_" if ($replacement);
1024 $struct->new_filter( $filter => $params, $negate );
1028 } elsif ($self->modifier_count && /$$r{modifier_re}/) { # found a modifier
1029 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1032 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1033 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1035 $struct->new_modifier($1);
1039 } elsif ($self->modifier_count && /$$r{modifier_as_class_re}/) { # found a modifier
1040 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1045 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1046 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1047 } elsif ($2 =~ /^[ty1]/i) {
1048 $struct->new_modifier($mod);
1052 } elsif (/$$r{float_start_re}/) { # start of an explicit float
1053 warn ' 'x$recursing."Encountered explicit float start\n" if $self->debug;
1057 $last_class = $current_class;
1058 $current_class = undef;
1060 $self->floating_plan( $self->new_plan( floating => 1 ) ) if (!$self->floating_plan);
1062 # pass the floating_plan struct to be modified by the float'ed chunk
1063 my ($floating_plan, $subremainder) = $self->new( debug => $self->debug )->decompose( $', undef, undef, undef, $self->floating_plan);
1065 warn ' 'x$recursing."Remainder after explicit float: $_\n" if $self->debug;
1067 $current_class = $last_class;
1070 } elsif (/$$r{group_start_re}/) { # start of an explicit group
1071 warn ' 'x$recursing."Encountered explicit group start\n" if $self->debug;
1073 if ($last_type eq 'CLASS') {
1074 warn ' 'x$recursing."Previous class change generated an empty node. Removing...\n" if $self->debug;
1075 $struct->remove_last_node;
1079 my ($substruct, $subremainder) = $self->decompose( $', $current_class, $recursing + 1 );
1081 $substruct->negate(1) if ($negate);
1082 $substruct->explicit(1);
1083 $struct->add_node( $substruct );
1086 warn ' 'x$recursing."Query remainder after bool group: $_\n" if $self->debug;
1090 } elsif (/$$r{and_re}/) { # ANDed expression
1092 warn ' 'x$recursing."Encountered AND\n" if $self->debug;
1093 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1094 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1097 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1099 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1100 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 1 );
1103 warn ' 'x$recursing."RHS built\n" if $self->debug;
1104 warn ' 'x$recursing."Post-AND remainder: $subremainder\n" if $self->debug;
1106 my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '&' );
1108 if ($LHS->floating) {
1109 $wrapper->{query} = $LHS->{query};
1110 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '&' );
1111 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1112 $LHS->{query} = [$outer_wrapper];
1115 $wrapper->add_node($_) for ($LHS, $RHS);
1116 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1117 $struct = $self->new_plan( level => $recursing );
1118 $struct->add_node($wrapper);
1121 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1124 } elsif (/$$r{or_re}/) { # ORed expression
1126 warn ' 'x$recursing."Encountered OR\n" if $self->debug;
1127 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1128 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1131 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1133 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1134 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 2 );
1135 $remainder = $subremainder;
1137 warn ' 'x$recursing."RHS built\n" if $self->debug;
1138 warn ' 'x$recursing."Post-OR remainder: $subremainder\n" if $self->debug;
1140 my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1142 if ($LHS->floating) {
1143 $wrapper->{query} = $LHS->{query};
1144 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1145 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1146 $LHS->{query} = [$outer_wrapper];
1149 $wrapper->add_node($_) for ($LHS, $RHS);
1150 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1151 $struct = $self->new_plan( level => $recursing );
1152 $struct->add_node($wrapper);
1155 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1158 } elsif ($self->facet_class_count && /$$r{facet_re}/) { # changing current class
1159 warn ' 'x$recursing."Encountered facet: $1$2 => $3\n" if $self->debug;
1161 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1163 my $facet_value = [ split '\s*\]\[\s*', $3 ];
1164 $struct->new_facet( $facet => $facet_value, $negate );
1168 } elsif ($self->search_class_count && /$$r{search_class_re}/) { # changing current class
1170 if ($last_type eq 'CLASS') {
1171 $struct->remove_last_node( $current_class );
1172 warn ' 'x$recursing."Encountered class change with no searches!\n" if $self->debug;
1175 warn ' 'x$recursing."Encountered class change: $1\n" if $self->debug;
1177 $current_class = $struct->classed_node( $1 )->requested_class();
1180 $last_type = 'CLASS';
1181 } elsif (/^\s*($$r{required_re}|$$r{disallowed_re}|$$r{negated_re})?"([^"]+)(?:"|$)/) { # phrase, always anded
1182 warn ' 'x$recursing.'Encountered' . ($1 ? " ['$1' modified]" : '') . " phrase: $2\n" if $self->debug;
1184 my $req_ness = $1 || '';
1185 $req_ness = $pkg->operator('disallowed') if ($req_ness eq $pkg->operator('negated'));
1188 if (!$phrase_helper) {
1189 warn ' 'x$recursing."Recursing into decompose with the phrase as a subquery\n" if $self->debug;
1191 my ($substruct, $subremainder) = $self->decompose( qq/$req_ness"$phrase"/, $current_class, $recursing + 1, 1 );
1192 $struct->add_node( $substruct ) if ($substruct);
1195 warn ' 'x$recursing."Directly parsing the phrase [ $phrase ] subquery\n" if $self->debug;
1196 $struct->joiner( '&' );
1198 my $class_node = $struct->classed_node($current_class);
1200 if (grep { $req_ness eq $_ } ($pkg->operator('disallowed'), $pkg->operator('negated'))) {
1201 $class_node->negate(1);
1202 $req_ness = $pkg->operator('negated');
1206 $phrase =~ s/$$r{phrase_cleanup_re}/ /g;
1207 $class_node->add_phrase( $phrase );
1208 $class_node->add_dummy_atom;
1215 } elsif (/^\s*((?:$$r{required_re}|$$r{disallowed_re}|$$r{negated_re})?)($$r{atom_re})/) { # atoms
1216 warn ' 'x$recursing."Encountered atom: $1\n" if $self->debug;
1217 warn ' 'x$recursing."Remainder: $'\n" if $self->debug;
1226 my $class_node = $struct->classed_node($current_class);
1230 $prefix = ($req_ness =~ /^$$r{required_re}/) ? '' : '!';
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 $add_space = $q ? 1 : 0;
1467 if ($abstract_query->{explicit_start}) {
1468 $q .= ' ' if $add_space;
1472 my $prefix = $abstract_query->{prefix} || '';
1473 $prefix = $qpconfig->{operators}{negated} if $prefix eq '!';
1474 $q .= ($add_space ? ' ' : '') . $prefix .
1475 ($abstract_query->{content} // '') .
1476 ($abstract_query->{suffix} || '');
1477 $q .= $ge if ($abstract_query->{explicit_end});
1478 } elsif ($abstract_query->{type} eq 'facet') {
1479 my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
1480 $q .= ($q ? ' ' : '') . $prefix . $abstract_query->{name} . "[" .
1481 join("][", @{$abstract_query->{values}}) . "]";
1485 my $next_depth = int($size > 1);
1487 if (exists $abstract_query->{children}) {
1489 my $op = (keys(%{$abstract_query->{children}}))[0];
1491 if ($abstract_query->{floating}) { # always the top node!
1492 my $sub_node = pop @{$abstract_query->{children}{$op}};
1494 $abstract_query->{floating} = 0;
1495 $q = $fs . " " . abstract_query2str_impl($abstract_query,0,$qp_class, 1) . $fe. " ";
1497 $abstract_query = $sub_node;
1500 if ($abstract_query && exists $abstract_query->{children}) {
1501 $op = (keys(%{$abstract_query->{children}}))[0];
1502 $q .= ($q ? ' ' : '') . join(
1503 ($op eq '&' ? ' ' : " $or "),
1505 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1506 } @{$abstract_query->{children}{$op}}
1509 } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1510 my $op = (keys(%{$abstract_query}))[0];
1511 $q .= ($q ? ' ' : '') . join(
1512 ($op eq '&' ? ' ' : " $or "),
1514 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1515 } @{$abstract_query->{$op}}
1519 $q = "$gs$q$ge" if ($isnode);
1520 $q = $negate . $q if ($q);
1525 #-------------------------------
1526 package QueryParser::query_plan;
1528 $Data::Dumper::Indent = 0;
1530 sub get_abstract_data {
1533 return $self->{abstract_data}{$key};
1536 sub set_abstract_data {
1540 $self->{abstract_data}{$key} = $value;
1545 return @{$self->filters} == 0 &&
1546 @{$self->modifiers} == 0 &&
1547 @{[map { @{$_->phrases} } grep { ref($_) && $_->isa('QueryParser::query_plan::node')} @{$self->query_nodes}]} == 0
1552 my( $left, $right ) = @_;
1553 return 0 if scalar @$left != scalar @$right;
1555 @hash{ @$left, @$right } = ();
1556 return scalar keys %hash == scalar @$left;
1562 # burrow down until we our kids have no subqueries
1563 my $downlink_joiner;
1564 for my $qnode (@{ $self->query_nodes }) {
1565 $qnode->pullup() if (ref($qnode) && $qnode->can('pullup'));
1567 warn "Entering pullup depth ". $self->plan_level . "\n" if $self->QueryParser->debug;
1569 my $old_qnodes = $self->query_nodes;
1570 warn @$old_qnodes . " query nodes (plans, nodes) at pullup depth ". $self->plan_level . "\n"
1571 if $self->QueryParser->debug;
1573 # Step 1: pull up subplan filter/facet/modifier nodes. These
1574 # will bubble up to the top of the plan tree. Evergreen doesn't
1575 # support nested filter/facet/modifier constructs currently.
1576 for my $kid (@$old_qnodes) {
1577 if (ref($kid) and $kid->isa('QueryParser::query_plan')) {
1578 $self->add_filter($_) foreach @{$kid->filters};
1579 $self->add_facet($_) foreach @{$kid->facets};
1580 $self->add_modifier($_) foreach @{$kid->modifiers};
1581 $kid->{filters} = [];
1582 $kid->{facets} = [];
1583 $kid->{modifiers} = [];
1587 # Step 2: Pull up ::nodes from subplans that only have nodes (no
1588 # nested subplans). This is in preparation for adjacent node merge,
1589 # and because this is a depth-first recursion, we've already decided
1590 # if nested plans can be elided.
1592 while (my $kid = shift @$old_qnodes) {
1593 if (ref($kid) and $kid->isa('QueryParser::query_plan')) {
1594 my $kid_query_nodes = $kid->query_nodes;
1595 my @kid_notnodes = grep { ref($_) and !$_->isa('QueryParser::query_plan::node') } @$kid_query_nodes;
1596 my @kid_nodes = grep { ref($_) and $_->isa('QueryParser::query_plan::node') } @$kid_query_nodes;
1597 if (@kid_nodes and !@kid_notnodes) {
1598 warn "pulling up nodes from nested plan at pullup depth ". $self->plan_level . "\n" if $self->QueryParser->debug;
1599 push @new_nodes, map { $_->plan($self) if ref; $_ } @$kid_query_nodes;
1603 push @new_nodes, $kid;
1606 # Step 3: Merge our adjacent ::nodes if they have the same requested_class.
1607 # This could miss merging aliased classes that are equiv, but that check
1608 # is more fiddly, and usually searches just use the class name.
1609 $old_qnodes = [@new_nodes];
1611 while ( my $current_node = shift(@$old_qnodes) ) {
1613 unless (@$old_qnodes) { # last node, no compression possible
1614 push @new_nodes, $current_node;
1618 my $current_joiner = shift(@$old_qnodes);
1619 my $next_node = shift(@$old_qnodes);
1621 # if they're both nodes, see if we can merge them
1622 if ($current_node->isa('QueryParser::query_plan::node')
1623 and $next_node->isa('QueryParser::query_plan::node')
1624 and $current_node->requested_class eq $next_node->requested_class
1625 and $current_node->negate eq $next_node->negate
1627 warn "merging RHS atoms into atom list for LHS with joiner $current_joiner\n" if $self->QueryParser->debug;
1628 push @{$current_node->query_atoms}, $current_joiner if @{$current_node->query_atoms};
1629 push @{$current_node->query_atoms}, map { if (ref($_)) { $_->{node} = $current_node }; $_ } @{$next_node->query_atoms};
1630 push @{$current_node->phrases}, @{$next_node->phrases};
1631 unshift @$old_qnodes, $current_node;
1633 push @new_nodes, $current_node, $current_joiner;
1634 unshift @$old_qnodes, $next_node;
1638 $self->{query} = \@new_nodes;
1640 # Step 4: As soon as we can, apply the explicit markers directly
1641 # to ::atoms so that we retain that for canonicalization while
1642 # also clearing away useless explicit groupings.
1643 if ($self->explicit) {
1644 if (!grep { # we have no non-::node, non-joiner query nodes, we've become a same-class singlton
1645 ref($_) and !$_->isa('QueryParser::query_plan::node')
1646 } @{$self->query_nodes}
1647 and 1 == grep { # and we have exactly one (possibly merged, above) ::node with at least one ::atom
1648 ref($_) and $_->isa('QueryParser::query_plan::node')
1649 } @{$self->query_nodes}
1651 warn "setting explicit flags on atoms that may later be pulled up, at depth". $self->plan_level . "\n"
1652 if $self->QueryParser->debug;
1653 $self->query_nodes->[0]->query_atoms->[0]->explicit_start(1) if @{$self->query_nodes->[0]->query_atoms};
1654 $self->query_nodes->[0]->query_atoms->[-1]->explicit_end(1) if @{$self->query_nodes->[0]->query_atoms};
1655 } else { # otherwise, the explicit grouping is meaningless, toss it
1660 warn @new_nodes . " nodes at pullup depth ". $self->plan_level . " after compression\n" if $self->QueryParser->debug;
1667 my %list = map { ($_=>1) } grep {!ref($_)} @{$self->{query}};
1674 ($_->classname . '|' . join('|',sort($_->fields)) => 1)
1676 ref($_) and ref($_) =~ /::node$/
1677 } @{$self->{query}};
1683 return undef unless ref($self);
1684 return $self->{QueryParser};
1689 $pkg = ref($pkg) || $pkg;
1690 my %args = (abstract_data => {}, query => [], joiner => '&', @_);
1692 return bless \%args => $pkg;
1697 my $pkg = ref($self) || $self;
1698 my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1699 $self->add_node( $node );
1705 my $pkg = ref($self) || $self;
1710 my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1711 $self->add_node( $node );
1718 my $pkg = ref($self) || $self;
1723 my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1724 $self->add_filter( $node );
1730 sub _merge_filters {
1731 my $left_filter = shift;
1732 my $right_filter = shift;
1735 return undef unless $left_filter or $right_filter;
1736 return $right_filter unless $left_filter;
1737 return $left_filter unless $right_filter;
1739 my $args = $left_filter->{args} || [];
1742 push(@$args, @{$right_filter->{args}});
1745 # find the intersect values
1747 map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1748 $args = [ grep { $new_vals{$_} } @$args ];
1751 $left_filter->{args} = $args;
1752 return $left_filter;
1755 sub collapse_filters {
1759 # start by merging any filters at this level.
1760 # like-level filters are always ORed together
1763 my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1765 $cur_filter = shift @cur_filters;
1766 my $args = $cur_filter->{args} || [];
1767 $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1770 # next gather the collapsed filters from sub-plans and
1771 # merge them with our own
1773 my @subquery = @{$self->{query}};
1776 my $blob = shift @subquery;
1777 shift @subquery; # joiner
1778 next unless $blob->isa('QueryParser::query_plan');
1779 my $sub_filter = $blob->collapse_filters($name);
1780 $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1783 if ($self->QueryParser->debug) {
1784 my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1785 warn "collapse_filters($name) => [@args]\n";
1793 my $needle = shift;;
1794 return undef unless ($needle);
1796 my $filter = $self->collapse_filters($needle);
1798 warn "find_filter($needle) => " .
1799 (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n"
1800 if $self->QueryParser->debug;
1802 return $filter ? ($filter) : ();
1807 my $needle = shift;;
1808 return undef unless ($needle);
1809 return grep { $_->name eq $needle } @{ $self->modifiers };
1814 my $pkg = ref($self) || $self;
1817 my $node = do{$pkg.'::modifier'}->new( $name );
1818 $self->add_modifier( $node );
1825 my $requested_class = shift;
1828 for my $n (@{$self->{query}}) {
1829 next unless (ref($n) && $n->isa( 'QueryParser::query_plan::node' ));
1830 if ($n->requested_class eq $requested_class) {
1837 $node = $self->new_node;
1838 $node->requested_class( $requested_class );
1844 sub remove_last_node {
1846 my $requested_class = shift;
1848 my $old = pop(@{$self->query_nodes});
1849 pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1856 return $self->{query};
1862 $self->{floating} = $f if (defined $f);
1863 return $self->{floating};
1869 $self->{explicit} = $f if (defined $f);
1870 return $self->{explicit};
1877 $self->{query} ||= [];
1879 push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1880 push(@{$self->{query}}, $node);
1889 return $self->{level} ? 0 : 1;
1896 if (defined $level) {
1897 $self->{level} = $level;
1898 for (@{$self->query_nodes}) {
1899 $_->plan_level($level + 1) if (ref and $_->isa('QueryParser::query_plan'));
1903 return $self->{level};
1910 $self->{joiner} = $joiner if ($joiner);
1911 return $self->{joiner};
1916 $self->{modifiers} ||= [];
1917 return $self->{modifiers};
1922 my $modifier = shift;
1924 $self->{modifiers} ||= [];
1925 $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1927 push(@{$self->{modifiers}}, $modifier);
1934 $self->{facets} ||= [];
1935 return $self->{facets};
1942 $self->{facets} ||= [];
1943 $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
1945 push(@{$self->{facets}}, $facet);
1952 $self->{filters} ||= [];
1953 return $self->{filters};
1960 $self->{filters} ||= [];
1962 push(@{$self->{filters}}, $filter);
1971 $self->{negate} = $negate if (defined $negate);
1973 return $self->{negate};
1976 # %opts supports two options at this time:
1978 # If true, do not do anything to the phrases
1979 # fields on any discovered nodes.
1981 # If true, also return the query parser config as part of the blob.
1982 # This will get set back to 0 before recursion to avoid repetition.
1983 sub to_abstract_query {
1987 my $pkg = ref $self->QueryParser || $self->QueryParser;
1989 my $abstract_query = {
1990 type => "query_plan",
1991 floating => $self->floating,
1992 level => $self->plan_level,
1993 filters => [map { $_->to_abstract_query } @{$self->filters}],
1994 modifiers => [map { $_->to_abstract_query } @{$self->modifiers}],
1995 negate => $self->negate
1998 if ($opts{with_config}) {
1999 $opts{with_config} = 0;
2000 $abstract_query->{config} = $QueryParser::parser_config{$pkg};
2005 my $prev_was_joiner = 0;
2006 for my $qnode (@{$self->query_nodes}) {
2007 # Remember: qnode can be a joiner string, a node, or another query_plan
2009 if (QueryParser::_util::is_joiner($qnode)) {
2010 unless ($prev_was_joiner) {
2011 if ($abstract_query->{children}) {
2012 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2013 next if $open_joiner eq $qnode;
2015 my $oldroot = $abstract_query->{children};
2017 $abstract_query->{children} = {$qnode => $kids};
2019 $abstract_query->{children} = {$qnode => $kids};
2022 $prev_was_joiner = 1;
2024 if (my $next_kid = $qnode->to_abstract_query(%opts)) {
2025 push @$kids, $qnode->to_abstract_query(%opts);
2026 $prev_was_joiner = 0;
2031 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2032 $$abstract_query{additional_data} = $self->{abstract_data}
2033 if (keys(%{$self->{abstract_data}}));
2035 return $abstract_query;
2039 #-------------------------------
2040 package QueryParser::query_plan::node;
2042 $Data::Dumper::Indent = 0;
2044 sub effective_joiner {
2047 my @nodelist = @{$node->query_atoms};
2048 return $node->plan->joiner if (@nodelist == 1);
2050 # gather the joiners
2051 my %joiners = ( '&' => 0, '|' => 0 );
2052 while (my $n = shift(@nodelist)) {
2053 next if ref($n); # only look at joiners
2057 if (!($joiners{'&'} > 0 and $joiners{'|'} > 0)) { # no mix of joiners
2058 return '|' if ($joiners{'|'});
2067 $pkg = ref($pkg) || $pkg;
2070 return bless \%args => $pkg;
2075 my $pkg = ref($self) || $self;
2076 return do{$pkg.'::atom'}->new( @_ );
2079 sub requested_class { # also split into classname, fields and alias
2085 my (undef, $alias) = split '#', $class;
2087 $class =~ s/#[^|]+//;
2088 ($alias, @afields) = split '\|', $alias;
2091 my @fields = @afields;
2092 my ($class_part, @field_parts) = split '\|', $class;
2093 for my $f (@field_parts) {
2094 push(@fields, $f) unless (grep { $f eq $_ } @fields);
2097 $class_part ||= $class;
2099 $self->{requested_class} = $class;
2100 $self->{alias} = $alias if $alias;
2101 $self->{alias_fields} = \@afields if $alias;
2102 $self->{classname} = $class_part;
2103 $self->{fields} = \@fields;
2106 return $self->{requested_class};
2113 $self->{plan} = $plan if ($plan);
2114 return $self->{plan};
2121 $self->{alias} = $alias if ($alias);
2122 return $self->{alias};
2129 $self->{alias_fields} = $alias if ($alias);
2130 return $self->{alias_fields};
2137 $self->{classname} = $class if ($class);
2138 return $self->{classname};
2145 $self->{fields} ||= [];
2146 $self->{fields} = \@fields if (@fields);
2147 return $self->{fields};
2154 $self->{phrases} ||= [];
2155 $self->{phrases} = \@phrases if (@phrases);
2156 return $self->{phrases};
2163 push(@{$self->phrases}, $phrase);
2172 $self->{negate} = $negate if (defined $negate);
2174 return $self->{negate};
2179 my @query_atoms = @_;
2181 $self->{query_atoms} ||= [];
2182 $self->{query_atoms} = \@query_atoms if (@query_atoms);
2183 return $self->{query_atoms};
2191 my $content = $atom;
2194 $atom = $self->new_atom( content => $content, node => $self, @parts );
2197 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
2198 push(@{$self->query_atoms}, $atom);
2203 sub add_dummy_atom {
2207 my $atom = $self->new_atom( node => $self, @parts, dummy => 1 );
2209 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
2210 push(@{$self->query_atoms}, $atom);
2215 # This will find up to one occurence of @$short_list within @$long_list, and
2216 # replace it with the single atom $replacement.
2217 sub replace_phrase_in_abstract_query {
2218 my ($self, $short_list, $long_list, $replacement) = @_;
2222 my $goal = scalar @$short_list;
2224 for (my $i = 0; $i < scalar (@$long_list); $i++) {
2225 my $right = $long_list->[$i];
2227 if (QueryParser::_util::compare_abstract_atoms(
2228 $short_list->[scalar @already], $right
2231 } elsif (scalar @already) {
2236 if (scalar @already == $goal) {
2237 splice @$long_list, $already[0], scalar(@already), $replacement;
2246 sub to_abstract_query {
2250 my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
2252 my $abstract_query = {
2254 "alias" => $self->alias,
2255 "alias_fields" => $self->alias_fields,
2256 "class" => $self->classname,
2257 "fields" => $self->fields
2260 $self->abstract_node_additions($abstract_query)
2261 if ($self->can('abstract_node_additions'));
2265 my $prev_was_joiner = 0;
2266 for my $qatom (grep {!ref($_) or !$_->dummy} @{$self->query_atoms}) {
2267 if (QueryParser::_util::is_joiner($qatom)) {
2268 unless ($prev_was_joiner) {
2269 if ($abstract_query->{children}) {
2270 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2271 next if $open_joiner eq $qatom;
2273 my $oldroot = $abstract_query->{children};
2275 $abstract_query->{children} = {$qatom => $kids};
2277 $abstract_query->{children} = {$qatom => $kids};
2280 $prev_was_joiner = 1;
2282 push @$kids, $qatom->to_abstract_query;
2283 $prev_was_joiner = 0;
2287 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2289 if ($self->phrases and @{$self->phrases} and not $opts{no_phrases}) {
2290 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2291 if ($open_joiner ne '&') {
2292 my $oldroot = $abstract_query->{children};
2294 $abstract_query->{children} = {'&' => $kids};
2297 for my $phrase (@{$self->phrases}) {
2298 # Phrases appear duplication in a real QP tree, and we don't want
2299 # that duplication in our abstract query. So for all our phrases,
2300 # break them into atoms as QP would, and remove any matching
2301 # sequences of atoms from our abstract query.
2303 my $tmp_prefix = '';
2304 $tmp_prefix = $QueryParser::parser_config{$pkg}{operators}{disallowed} if ($self->{negate});
2306 my $tmptree = $self->{plan}->{QueryParser}->new(query => $phrase)->parse->parse_tree;
2308 # For a well-behaved phrase, we should now have only one node
2309 # in the $tmptree query plan, and that node should have an
2310 # orderly list of atoms and joiners.
2312 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2316 $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2318 )->{children}->{'&'};
2320 next if $@ or !ref($tmplist);
2322 $$tmplist[0]{prefix} = $tmp_prefix.'"';
2323 $$tmplist[-1]{suffix} = '"';
2324 push @{$abstract_query->{children}->{'&'}}, @$tmplist;
2330 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2332 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2333 return undef unless @{$abstract_query->{children}->{$open_joiner}};
2335 return $abstract_query;
2338 #-------------------------------
2339 package QueryParser::query_plan::node::atom;
2343 $pkg = ref($pkg) || $pkg;
2346 return bless \%args => $pkg;
2351 return undef unless (ref $self);
2352 return $self->{node};
2357 return undef unless (ref $self);
2358 return $self->{content};
2363 return undef unless (ref $self);
2364 return $self->{prefix};
2369 return undef unless (ref $self);
2370 return $self->{suffix};
2373 sub explicit_start {
2375 my $explicit_start = shift;
2377 $self->{explicit_start} = $explicit_start if (defined $explicit_start);
2379 return $self->{explicit_start};
2386 $self->{dummy} = $dummy if (defined $dummy);
2388 return $self->{dummy};
2393 my $explicit_end = shift;
2395 $self->{explicit_end} = $explicit_end if (defined $explicit_end);
2397 return $self->{explicit_end};
2400 sub to_abstract_query {
2404 (map { $_ => $self->$_ } qw/dummy prefix suffix content explicit_start explicit_end/),
2408 #-------------------------------
2409 package QueryParser::query_plan::filter;
2413 $pkg = ref($pkg) || $pkg;
2416 return bless \%args => $pkg;
2421 return $self->{plan};
2426 return $self->{name};
2431 return $self->{negate};
2436 return $self->{args};
2439 sub to_abstract_query {
2443 map { $_ => $self->$_ } qw/name negate args/
2447 #-------------------------------
2448 package QueryParser::query_plan::facet;
2452 $pkg = ref($pkg) || $pkg;
2455 return bless \%args => $pkg;
2460 return $self->{plan};
2465 return $self->{name};
2470 return $self->{negate};
2475 return $self->{'values'};
2478 sub to_abstract_query {
2482 (map { $_ => $self->$_ } qw/name negate values/),
2487 #-------------------------------
2488 package QueryParser::query_plan::modifier;
2492 $pkg = ref($pkg) || $pkg;
2493 my $modifier = shift;
2496 return bless { name => $modifier, negate => $negate } => $pkg;
2501 return $self->{name};
2506 return $self->{negate};
2509 sub to_abstract_query {