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;
997 } elsif ($self->filter_count && /$$r{filter_re}/) { # found a filter
998 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
1000 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1004 my $params = [ split '[,]+', $3 ];
1006 if ($pkg->filter_callbacks->{$filter}) {
1007 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
1008 $_ = "$replacement $_" if ($replacement);
1010 $struct->new_filter( $filter => $params, $negate );
1015 } elsif ($self->filter_count && /$$r{filter_as_class_re}/) { # found a filter
1016 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
1018 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1022 my $params = [ split '[,]+', $3 ];
1024 if ($pkg->filter_callbacks->{$filter}) {
1025 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
1026 $_ = "$replacement $_" if ($replacement);
1028 $struct->new_filter( $filter => $params, $negate );
1032 } elsif ($self->modifier_count && /$$r{modifier_re}/) { # found a modifier
1033 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1036 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1037 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1039 $struct->new_modifier($1);
1043 } elsif ($self->modifier_count && /$$r{modifier_as_class_re}/) { # found a modifier
1044 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1049 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1050 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1051 } elsif ($2 =~ /^[ty1]/i) {
1052 $struct->new_modifier($mod);
1056 } elsif (/$$r{float_start_re}/) { # start of an explicit float
1057 warn ' 'x$recursing."Encountered explicit float start\n" if $self->debug;
1061 $last_class = $current_class;
1062 $current_class = undef;
1064 $self->floating_plan( $self->new_plan( floating => 1 ) ) if (!$self->floating_plan);
1066 # pass the floating_plan struct to be modified by the float'ed chunk
1067 my ($floating_plan, $subremainder) = $self->new( debug => $self->debug )->decompose( $', undef, undef, undef, $self->floating_plan);
1069 warn ' 'x$recursing."Remainder after explicit float: $_\n" if $self->debug;
1071 $current_class = $last_class;
1074 } elsif (/$$r{group_start_re}/) { # start of an explicit group
1075 warn ' 'x$recursing."Encountered explicit group start\n" if $self->debug;
1077 if ($last_type eq 'CLASS') {
1078 warn ' 'x$recursing."Previous class change generated an empty node. Removing...\n" if $self->debug;
1079 $struct->remove_last_node;
1083 my ($substruct, $subremainder) = $self->decompose( $', $current_class, $recursing + 1 );
1085 $substruct->negate(1) if ($negate);
1086 $substruct->explicit(1);
1087 $struct->add_node( $substruct );
1090 warn ' 'x$recursing."Query remainder after bool group: $_\n" if $self->debug;
1094 } elsif (/$$r{and_re}/) { # ANDed expression
1096 warn ' 'x$recursing."Encountered AND\n" if $self->debug;
1097 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1098 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1101 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1103 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1104 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 1 );
1107 warn ' 'x$recursing."RHS built\n" if $self->debug;
1108 warn ' 'x$recursing."Post-AND remainder: $subremainder\n" if $self->debug;
1110 my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '&' );
1112 if ($LHS->floating) {
1113 $wrapper->{query} = $LHS->{query};
1114 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '&' );
1115 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1116 $LHS->{query} = [$outer_wrapper];
1119 $wrapper->add_node($_) for ($LHS, $RHS);
1120 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1121 $struct = $self->new_plan( level => $recursing );
1122 $struct->add_node($wrapper);
1125 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1128 } elsif (/$$r{or_re}/) { # ORed expression
1130 warn ' 'x$recursing."Encountered OR\n" if $self->debug;
1131 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1132 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1135 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1137 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1138 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 2 );
1139 $remainder = $subremainder;
1141 warn ' 'x$recursing."RHS built\n" if $self->debug;
1142 warn ' 'x$recursing."Post-OR remainder: $subremainder\n" if $self->debug;
1144 my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1146 if ($LHS->floating) {
1147 $wrapper->{query} = $LHS->{query};
1148 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1149 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1150 $LHS->{query} = [$outer_wrapper];
1153 $wrapper->add_node($_) for ($LHS, $RHS);
1154 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1155 $struct = $self->new_plan( level => $recursing );
1156 $struct->add_node($wrapper);
1159 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1162 } elsif ($self->facet_class_count && /$$r{facet_re}/) { # changing current class
1163 warn ' 'x$recursing."Encountered facet: $1$2 => $3\n" if $self->debug;
1165 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1167 my $facet_value = [ split '\s*\]\[\s*', $3 ];
1168 $struct->new_facet( $facet => $facet_value, $negate );
1172 } elsif ($self->search_class_count && /$$r{search_class_re}/) { # changing current class
1174 if ($last_type eq 'CLASS') {
1175 $struct->remove_last_node( $current_class );
1176 warn ' 'x$recursing."Encountered class change with no searches!\n" if $self->debug;
1179 warn ' 'x$recursing."Encountered class change: $1\n" if $self->debug;
1181 $current_class = $struct->classed_node( $1 )->requested_class();
1184 $last_type = 'CLASS';
1185 } elsif (/^\s*($$r{required_re}|$$r{disallowed_re}|$$r{negated_re})?"([^"]+)(?:"|$)/) { # phrase, always anded
1186 warn ' 'x$recursing.'Encountered' . ($1 ? " ['$1' modified]" : '') . " phrase: $2\n" if $self->debug;
1188 my $req_ness = $1 || '';
1189 $req_ness = $pkg->operator('disallowed') if ($req_ness eq $pkg->operator('negated'));
1192 if (!$phrase_helper) {
1193 warn ' 'x$recursing."Recursing into decompose with the phrase as a subquery\n" if $self->debug;
1195 my ($substruct, $subremainder) = $self->decompose( qq/$req_ness"$phrase"/, $current_class, $recursing + 1, 1 );
1196 $struct->add_node( $substruct ) if ($substruct);
1199 warn ' 'x$recursing."Directly parsing the phrase [ $phrase ] subquery\n" if $self->debug;
1200 $struct->joiner( '&' );
1202 my $class_node = $struct->classed_node($current_class);
1204 if (grep { $req_ness eq $_ } ($pkg->operator('disallowed'), $pkg->operator('negated'))) {
1205 $class_node->negate(1);
1206 $req_ness = $pkg->operator('negated');
1210 $phrase =~ s/$$r{phrase_cleanup_re}/ /g;
1211 $class_node->add_phrase( $phrase );
1212 $class_node->add_dummy_atom;
1219 } elsif (/^\s*((?:$$r{required_re}|$$r{disallowed_re}|$$r{negated_re})?)($$r{atom_re})/) { # atoms
1220 warn ' 'x$recursing."Encountered atom: $1\n" if $self->debug;
1221 warn ' 'x$recursing."Remainder: $'\n" if $self->debug;
1230 my $class_node = $struct->classed_node($current_class);
1234 $prefix = ($req_ness =~ /^$$r{required_re}/) ? '' : '!';
1237 my $truncate = ($atom =~ s/\*$//o) ? '*' : '';
1239 if ($atom ne '' and !grep { $atom =~ /^\Q$_\E+$/ } ('&','|')) { # throw away & and |, not allowed in tsquery, and not really useful anyway
1240 # $class_node->add_phrase( $atom ) if ($atom =~ s/^$$r{required_re}//o);
1242 $class_node->add_fts_atom( $atom, suffix => $truncate, prefix => $prefix, node => $class_node );
1243 $struct->joiner( '&' );
1248 warn ' 'x$recursing."Cannot parse: $_\n" if $self->debug;
1257 scalar(@{$struct->query_nodes}) == 0 &&
1258 scalar(@{$struct->filters}) == 0 &&
1261 return $struct if !wantarray;
1262 return ($struct, $remainder);
1265 =head2 find_class_index
1267 $index = $QParser->find_class_index($class, $query);
1270 sub find_class_index {
1274 my ($class_part, @field_parts) = split '\|', $class;
1275 $class_part ||= $class;
1277 for my $idx ( 0 .. scalar(@$query) - 1 ) {
1278 next unless ref($$query[$idx]);
1279 return $idx if ( $$query[$idx]{requested_class} && $class eq $$query[$idx]{requested_class} );
1282 push(@$query, { classname => $class_part, (@field_parts ? (fields => \@field_parts) : ()), requested_class => $class, ftsquery => [], phrases => [] });
1288 $limit = $QParser->core_limit([$limit]);
1290 Return and/or set the core_limit.
1296 $self->{core_limit} = $l if ($l);
1297 return $self->{core_limit};
1302 $superpage = $QParser->superpage([$superpage]);
1304 Return and/or set the superpage.
1310 $self->{superpage} = $l if ($l);
1311 return $self->{superpage};
1314 =head2 superpage_size
1316 $size = $QParser->superpage_size([$size]);
1318 Return and/or set the superpage size.
1321 sub superpage_size {
1324 $self->{superpage_size} = $l if ($l);
1325 return $self->{superpage_size};
1329 #-------------------------------
1330 package QueryParser::_util;
1332 # At this level, joiners are always & or |. This is not
1333 # the external, configurable representation of joiners that
1334 # defaults to # && and ||.
1338 return (not ref $str and ($str eq '&' or $str eq '|'));
1341 sub default_joiner { '&' }
1343 # 0 for different, 1 for the same.
1344 sub compare_abstract_atoms {
1345 my ($left, $right) = @_;
1347 foreach (qw/prefix suffix content/) {
1348 no warnings; # undef can stand in for '' here
1349 return 0 unless $left->{$_} eq $right->{$_};
1355 sub fake_abstract_atom_from_phrase {
1358 my $qp_class = shift || 'QueryParser';
1363 $QueryParser::parser_config{$qp_class}{operators}{disallowed} .
1368 "type" => "atom", "prefix" => $prefix, "suffix" => '"',
1369 "content" => $phrase
1373 sub find_arrays_in_abstract {
1377 foreach my $key (keys %$hash) {
1378 if (ref $hash->{$key} eq "ARRAY") {
1379 push @arrays, $hash->{$key};
1380 foreach (@{$hash->{$key}}) {
1381 push @arrays, find_arrays_in_abstract($_);
1389 #-------------------------------
1390 package QueryParser::Canonicalize; # not OO
1393 sub _abstract_query2str_filter {
1395 my $qp_class = shift || 'QueryParser';
1396 my $qpconfig = $QueryParser::parser_config{$qp_class};
1400 $f->{negate} ? $qpconfig->{operators}{disallowed} : "",
1402 join(",", @{$f->{args}})
1406 sub _abstract_query2str_modifier {
1408 my $qp_class = shift || 'QueryParser';
1409 my $qpconfig = $QueryParser::parser_config{$qp_class};
1411 return $qpconfig->{operators}{modifier} . $f;
1415 my $children = shift;
1416 my $op = (keys %$children)[0];
1417 return @{$$children{$op}};
1421 # This should produce an equivalent query to the original, given an
1423 sub abstract_query2str_impl {
1424 my $abstract_query = shift;
1425 my $depth = shift || 0;
1427 my $qp_class ||= shift || 'QueryParser';
1428 my $force_qp_node = shift || 0;
1429 my $qpconfig = $QueryParser::parser_config{$qp_class};
1431 my $fs = $qpconfig->{operators}{float_start};
1432 my $fe = $qpconfig->{operators}{float_end};
1433 my $gs = $qpconfig->{operators}{group_start};
1434 my $ge = $qpconfig->{operators}{group_end};
1435 my $and = $qpconfig->{operators}{and};
1436 my $or = $qpconfig->{operators}{or};
1437 my $ng = $qpconfig->{operators}{negated};
1444 if (exists $abstract_query->{type}) {
1445 if ($abstract_query->{type} eq 'query_plan') {
1446 $q .= join(" ", map { _abstract_query2str_filter($_, $qp_class) } @{$abstract_query->{filters}}) if
1447 exists $abstract_query->{filters};
1449 $q .= ($q ? ' ' : '') . join(" ", map { _abstract_query2str_modifier($_, $qp_class) } @{$abstract_query->{modifiers}}) if
1450 exists $abstract_query->{modifiers};
1452 $size = _kid_list($abstract_query->{children});
1453 if ($abstract_query->{negate}) {
1457 $isnode = 1 if ($size > 1 and ($force_qp_node or $depth));
1458 #warn "size: $size, depth: $depth, isnode: $isnode, AQ: ".Dumper($abstract_query);
1459 } elsif ($abstract_query->{type} eq 'node') {
1460 if ($abstract_query->{alias}) {
1461 $q .= ($q ? ' ' : '') . $abstract_query->{alias};
1462 $q .= "|$_" foreach @{$abstract_query->{alias_fields}};
1464 $q .= ($q ? ' ' : '') . $abstract_query->{class};
1465 $q .= "|$_" foreach @{$abstract_query->{fields}};
1469 } elsif ($abstract_query->{type} eq 'atom') {
1470 my $add_space = $q ? 1 : 0;
1471 if ($abstract_query->{explicit_start}) {
1472 $q .= ' ' if $add_space;
1473 $q .= $gs x $abstract_query->{explicit_start};
1476 my $prefix = $abstract_query->{prefix} || '';
1477 $prefix = $qpconfig->{operators}{negated} if $prefix eq '!';
1478 $q .= ($add_space ? ' ' : '') . $prefix .
1479 ($abstract_query->{content} // '') .
1480 ($abstract_query->{suffix} || '');
1481 $q .= $ge x $abstract_query->{explicit_end} if ($abstract_query->{explicit_end});
1482 } elsif ($abstract_query->{type} eq 'facet') {
1483 my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
1484 $q .= ($q ? ' ' : '') . $prefix . $abstract_query->{name} . "[" .
1485 join("][", @{$abstract_query->{values}}) . "]";
1489 my $next_depth = int($size > 1);
1491 if (exists $abstract_query->{children}) {
1493 my $op = (keys(%{$abstract_query->{children}}))[0];
1495 if ($abstract_query->{floating}) { # always the top node!
1496 my $sub_node = pop @{$abstract_query->{children}{$op}};
1498 $abstract_query->{floating} = 0;
1499 $q = $fs . " " . abstract_query2str_impl($abstract_query,0,$qp_class, 1) . $fe. " ";
1501 $abstract_query = $sub_node;
1504 if ($abstract_query && exists $abstract_query->{children}) {
1505 $op = (keys(%{$abstract_query->{children}}))[0];
1506 $q .= ($q ? ' ' : '') . join(
1507 ($op eq '&' ? ' ' : " $or "),
1509 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1510 } @{$abstract_query->{children}{$op}}
1513 } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1514 my $op = (keys(%{$abstract_query}))[0];
1515 $q .= ($q ? ' ' : '') . join(
1516 ($op eq '&' ? ' ' : " $or "),
1518 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1519 } @{$abstract_query->{$op}}
1523 $q = "$gs$q$ge" if ($isnode);
1524 $q = $negate . $q if ($q);
1529 #-------------------------------
1530 package QueryParser::query_plan;
1532 $Data::Dumper::Indent = 0;
1534 sub get_abstract_data {
1537 return $self->{abstract_data}{$key};
1540 sub set_abstract_data {
1544 $self->{abstract_data}{$key} = $value;
1549 return @{$self->filters} == 0 &&
1550 @{$self->modifiers} == 0 &&
1551 @{[map { @{$_->phrases} } grep { ref($_) && $_->isa('QueryParser::query_plan::node')} @{$self->query_nodes}]} == 0
1556 my( $left, $right ) = @_;
1557 return 0 if scalar @$left != scalar @$right;
1559 @hash{ @$left, @$right } = ();
1560 return scalar keys %hash == scalar @$left;
1566 # burrow down until we our kids have no subqueries
1567 my $downlink_joiner;
1568 for my $qnode (@{ $self->query_nodes }) {
1569 $qnode->pullup() if (ref($qnode) && $qnode->can('pullup'));
1571 warn "Entering pullup depth ". $self->plan_level . "\n" if $self->QueryParser->debug;
1573 my $old_qnodes = $self->query_nodes;
1574 warn @$old_qnodes . " query nodes (plans, nodes) at pullup depth ". $self->plan_level . "\n"
1575 if $self->QueryParser->debug;
1577 # Step 1: pull up subplan filter/facet/modifier nodes. These
1578 # will bubble up to the top of the plan tree. Evergreen doesn't
1579 # support nested filter/facet/modifier constructs currently.
1580 for my $kid (@$old_qnodes) {
1581 if (ref($kid) and $kid->isa('QueryParser::query_plan')) {
1582 $self->add_filter($_) foreach @{$kid->filters};
1583 $self->add_facet($_) foreach @{$kid->facets};
1584 $self->add_modifier($_) foreach @{$kid->modifiers};
1585 $kid->{filters} = [];
1586 $kid->{facets} = [];
1587 $kid->{modifiers} = [];
1591 # Step 2: Pull up ::nodes from subplans that only have nodes (no
1592 # nested subplans). This is in preparation for adjacent node merge,
1593 # and because this is a depth-first recursion, we've already decided
1594 # if nested plans can be elided.
1596 while (my $kid = shift @$old_qnodes) {
1597 if (ref($kid) and $kid->isa('QueryParser::query_plan')) {
1598 my $kid_query_nodes = $kid->query_nodes;
1599 my @kid_notnodes = grep { ref($_) and !$_->isa('QueryParser::query_plan::node') } @$kid_query_nodes;
1600 my @kid_nodes = grep { ref($_) and $_->isa('QueryParser::query_plan::node') } @$kid_query_nodes;
1601 if (@kid_nodes and !@kid_notnodes) {
1602 warn "pulling up nodes from nested plan at pullup depth ". $self->plan_level . "\n" if $self->QueryParser->debug;
1603 push @new_nodes, map { $_->plan($self) if ref; $_ } @$kid_query_nodes;
1607 push @new_nodes, $kid;
1610 # Step 3: Merge our adjacent ::nodes if they have the same requested_class.
1611 # This could miss merging aliased classes that are equiv, but that check
1612 # is more fiddly, and usually searches just use the class name.
1613 $old_qnodes = [@new_nodes];
1615 while ( my $current_node = shift(@$old_qnodes) ) {
1617 unless (@$old_qnodes) { # last node, no compression possible
1618 push @new_nodes, $current_node;
1622 my $current_joiner = shift(@$old_qnodes);
1623 my $next_node = shift(@$old_qnodes);
1625 # if they're both nodes, see if we can merge them
1626 if ($current_node->isa('QueryParser::query_plan::node')
1627 and $next_node->isa('QueryParser::query_plan::node')
1628 and $current_node->requested_class eq $next_node->requested_class
1629 and (defined $current_node->negate ? $current_node->negate : "")
1630 eq (defined $next_node->negate ? $next_node->negate : "")
1632 warn "merging RHS atoms into atom list for LHS with joiner $current_joiner\n" if $self->QueryParser->debug;
1633 push @{$current_node->query_atoms}, $current_joiner if @{$current_node->query_atoms};
1634 push @{$current_node->query_atoms}, map { if (ref($_)) { $_->{node} = $current_node }; $_ } @{$next_node->query_atoms};
1635 push @{$current_node->phrases}, @{$next_node->phrases};
1636 unshift @$old_qnodes, $current_node;
1638 push @new_nodes, $current_node, $current_joiner;
1639 unshift @$old_qnodes, $next_node;
1643 $self->{query} = \@new_nodes;
1645 # Step 4: As soon as we can, apply the explicit markers directly
1646 # to ::atoms so that we retain that for canonicalization while
1647 # also clearing away useless explicit groupings.
1648 if ($self->explicit) {
1649 if (!grep { # we have no non-::node, non-joiner query nodes, we've become a same-class singlton
1650 ref($_) and !$_->isa('QueryParser::query_plan::node')
1651 } @{$self->query_nodes}
1652 and 1 == grep { # and we have exactly one (possibly merged, above) ::node with at least one ::atom
1653 ref($_) and $_->isa('QueryParser::query_plan::node')
1654 } @{$self->query_nodes}
1655 and (my @atoms = @{$self->query_nodes->[0]->query_atoms}) > 0
1658 warn "setting explicit flags on atoms that may later be pulled up, at depth". $self->plan_level . "\n"
1659 if $self->QueryParser->debug;
1660 my $first_atom = $atoms[0];
1661 my $last_atom = $atoms[-1];
1662 $first_atom->explicit_start(defined $first_atom->explicit_start ? $first_atom->explicit_start + 1 : 1);
1663 $last_atom->explicit_end(defined $last_atom->explicit_end ? $last_atom->explicit_end + 1 : 1);
1664 } else { # otherwise, the explicit grouping is meaningless, toss it
1669 warn @new_nodes . " nodes at pullup depth ". $self->plan_level . " after compression\n" if $self->QueryParser->debug;
1676 my %list = map { ($_=>1) } grep {!ref($_)} @{$self->{query}};
1683 ($_->classname . '|' . join('|',sort($_->fields)) => 1)
1685 ref($_) and ref($_) =~ /::node$/
1686 } @{$self->{query}};
1692 return undef unless ref($self);
1693 return $self->{QueryParser};
1698 $pkg = ref($pkg) || $pkg;
1699 my %args = (abstract_data => {}, query => [], joiner => '&', @_);
1701 return bless \%args => $pkg;
1706 my $pkg = ref($self) || $self;
1707 my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1708 $self->add_node( $node );
1714 my $pkg = ref($self) || $self;
1719 my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1720 $self->add_node( $node );
1727 my $pkg = ref($self) || $self;
1732 my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1733 $self->add_filter( $node );
1739 sub _merge_filters {
1740 my $left_filter = shift;
1741 my $right_filter = shift;
1744 return undef unless $left_filter or $right_filter;
1745 return $right_filter unless $left_filter;
1746 return $left_filter unless $right_filter;
1748 my $args = $left_filter->{args} || [];
1751 push(@$args, @{$right_filter->{args}});
1754 # find the intersect values
1756 map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1757 $args = [ grep { $new_vals{$_} } @$args ];
1760 $left_filter->{args} = $args;
1761 return $left_filter;
1764 sub collapse_filters {
1768 # start by merging any filters at this level.
1769 # like-level filters are always ORed together
1772 my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1774 $cur_filter = shift @cur_filters;
1775 my $args = $cur_filter->{args} || [];
1776 $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1779 # next gather the collapsed filters from sub-plans and
1780 # merge them with our own
1782 my @subquery = @{$self->{query}};
1785 my $blob = shift @subquery;
1786 shift @subquery; # joiner
1787 next unless $blob->isa('QueryParser::query_plan');
1788 my $sub_filter = $blob->collapse_filters($name);
1789 $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1792 if ($self->QueryParser->debug) {
1793 my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1794 warn "collapse_filters($name) => [@args]\n";
1802 my $needle = shift;;
1803 return undef unless ($needle);
1805 my $filter = $self->collapse_filters($needle);
1807 warn "find_filter($needle) => " .
1808 (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n"
1809 if $self->QueryParser->debug;
1811 return $filter ? ($filter) : ();
1816 my $needle = shift;;
1817 return undef unless ($needle);
1818 return grep { $_->name eq $needle } @{ $self->modifiers };
1823 my $pkg = ref($self) || $self;
1826 my $node = do{$pkg.'::modifier'}->new( $name );
1827 $self->add_modifier( $node );
1834 my $requested_class = shift;
1837 for my $n (@{$self->{query}}) {
1838 next unless (ref($n) && $n->isa( 'QueryParser::query_plan::node' ));
1839 if ($n->requested_class eq $requested_class) {
1846 $node = $self->new_node;
1847 $node->requested_class( $requested_class );
1853 sub remove_last_node {
1855 my $requested_class = shift;
1857 my $old = pop(@{$self->query_nodes});
1858 pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1865 return $self->{query};
1871 $self->{floating} = $f if (defined $f);
1872 return $self->{floating};
1878 $self->{explicit} = $f if (defined $f);
1879 return $self->{explicit};
1886 $self->{query} ||= [];
1888 push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1889 push(@{$self->{query}}, $node);
1898 return $self->{level} ? 0 : 1;
1905 if (defined $level) {
1906 $self->{level} = $level;
1907 for (@{$self->query_nodes}) {
1908 $_->plan_level($level + 1) if (ref and $_->isa('QueryParser::query_plan'));
1912 return $self->{level};
1919 $self->{joiner} = $joiner if ($joiner);
1920 return $self->{joiner};
1925 $self->{modifiers} ||= [];
1926 return $self->{modifiers};
1931 my $modifier = shift;
1933 $self->{modifiers} ||= [];
1934 $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1936 push(@{$self->{modifiers}}, $modifier);
1943 $self->{facets} ||= [];
1944 return $self->{facets};
1951 $self->{facets} ||= [];
1952 $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
1954 push(@{$self->{facets}}, $facet);
1961 $self->{filters} ||= [];
1962 return $self->{filters};
1969 $self->{filters} ||= [];
1971 push(@{$self->{filters}}, $filter);
1980 $self->{negate} = $negate if (defined $negate);
1982 return $self->{negate};
1985 # %opts supports two options at this time:
1987 # If true, do not do anything to the phrases
1988 # fields on any discovered nodes.
1990 # If true, also return the query parser config as part of the blob.
1991 # This will get set back to 0 before recursion to avoid repetition.
1992 sub to_abstract_query {
1996 my $pkg = ref $self->QueryParser || $self->QueryParser;
1998 my $abstract_query = {
1999 type => "query_plan",
2000 floating => $self->floating,
2001 level => $self->plan_level,
2002 filters => [map { $_->to_abstract_query } @{$self->filters}],
2003 modifiers => [map { $_->to_abstract_query } @{$self->modifiers}],
2004 negate => $self->negate
2007 if ($opts{with_config}) {
2008 $opts{with_config} = 0;
2009 $abstract_query->{config} = $QueryParser::parser_config{$pkg};
2014 my $prev_was_joiner = 0;
2015 for my $qnode (@{$self->query_nodes}) {
2016 # Remember: qnode can be a joiner string, a node, or another query_plan
2018 if (QueryParser::_util::is_joiner($qnode)) {
2019 unless ($prev_was_joiner) {
2020 if ($abstract_query->{children}) {
2021 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2022 next if $open_joiner eq $qnode;
2024 my $oldroot = $abstract_query->{children};
2026 $abstract_query->{children} = {$qnode => $kids};
2028 $abstract_query->{children} = {$qnode => $kids};
2031 $prev_was_joiner = 1;
2033 if (my $next_kid = $qnode->to_abstract_query(%opts)) {
2034 push @$kids, $qnode->to_abstract_query(%opts);
2035 $prev_was_joiner = 0;
2040 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2041 $$abstract_query{additional_data} = $self->{abstract_data}
2042 if (keys(%{$self->{abstract_data}}));
2044 return $abstract_query;
2048 #-------------------------------
2049 package QueryParser::query_plan::node;
2051 $Data::Dumper::Indent = 0;
2053 sub effective_joiner {
2056 my @nodelist = @{$node->query_atoms};
2057 return $node->plan->joiner if (@nodelist == 1);
2059 # gather the joiners
2060 my %joiners = ( '&' => 0, '|' => 0 );
2061 while (my $n = shift(@nodelist)) {
2062 next if ref($n); # only look at joiners
2066 if (!($joiners{'&'} > 0 and $joiners{'|'} > 0)) { # no mix of joiners
2067 return '|' if ($joiners{'|'});
2076 $pkg = ref($pkg) || $pkg;
2079 return bless \%args => $pkg;
2084 my $pkg = ref($self) || $self;
2085 return do{$pkg.'::atom'}->new( @_ );
2088 sub requested_class { # also split into classname, fields and alias
2094 my (undef, $alias) = split '#', $class;
2096 $class =~ s/#[^|]+//;
2097 ($alias, @afields) = split '\|', $alias;
2100 my @fields = @afields;
2101 my ($class_part, @field_parts) = split '\|', $class;
2102 for my $f (@field_parts) {
2103 push(@fields, $f) unless (grep { $f eq $_ } @fields);
2106 $class_part ||= $class;
2108 $self->{requested_class} = $class;
2109 $self->{alias} = $alias if $alias;
2110 $self->{alias_fields} = \@afields if $alias;
2111 $self->{classname} = $class_part;
2112 $self->{fields} = \@fields;
2115 return $self->{requested_class};
2122 $self->{plan} = $plan if ($plan);
2123 return $self->{plan};
2130 $self->{alias} = $alias if ($alias);
2131 return $self->{alias};
2138 $self->{alias_fields} = $alias if ($alias);
2139 return $self->{alias_fields};
2146 $self->{classname} = $class if ($class);
2147 return $self->{classname};
2154 $self->{fields} ||= [];
2155 $self->{fields} = \@fields if (@fields);
2156 return $self->{fields};
2163 $self->{phrases} ||= [];
2164 $self->{phrases} = \@phrases if (@phrases);
2165 return $self->{phrases};
2172 push(@{$self->phrases}, $phrase);
2181 $self->{negate} = $negate if (defined $negate);
2183 return $self->{negate};
2188 my @query_atoms = @_;
2190 $self->{query_atoms} ||= [];
2191 $self->{query_atoms} = \@query_atoms if (@query_atoms);
2192 return $self->{query_atoms};
2200 my $content = $atom;
2203 $atom = $self->new_atom( content => $content, node => $self, @parts );
2206 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
2207 push(@{$self->query_atoms}, $atom);
2212 sub add_dummy_atom {
2216 my $atom = $self->new_atom( node => $self, @parts, dummy => 1 );
2218 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
2219 push(@{$self->query_atoms}, $atom);
2224 # This will find up to one occurence of @$short_list within @$long_list, and
2225 # replace it with the single atom $replacement.
2226 sub replace_phrase_in_abstract_query {
2227 my ($self, $short_list, $long_list, $replacement) = @_;
2231 my $goal = scalar @$short_list;
2233 for (my $i = 0; $i < scalar (@$long_list); $i++) {
2234 my $right = $long_list->[$i];
2236 if (QueryParser::_util::compare_abstract_atoms(
2237 $short_list->[scalar @already], $right
2240 } elsif (scalar @already) {
2245 if (scalar @already == $goal) {
2246 splice @$long_list, $already[0], scalar(@already), $replacement;
2255 sub to_abstract_query {
2259 my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
2261 my $abstract_query = {
2263 "alias" => $self->alias,
2264 "alias_fields" => $self->alias_fields,
2265 "class" => $self->classname,
2266 "fields" => $self->fields
2269 $self->abstract_node_additions($abstract_query)
2270 if ($self->can('abstract_node_additions'));
2274 my $prev_was_joiner = 0;
2275 for my $qatom (grep {!ref($_) or !$_->dummy} @{$self->query_atoms}) {
2276 if (QueryParser::_util::is_joiner($qatom)) {
2277 unless ($prev_was_joiner) {
2278 if ($abstract_query->{children}) {
2279 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2280 next if $open_joiner eq $qatom;
2282 my $oldroot = $abstract_query->{children};
2284 $abstract_query->{children} = {$qatom => $kids};
2286 $abstract_query->{children} = {$qatom => $kids};
2289 $prev_was_joiner = 1;
2291 push @$kids, $qatom->to_abstract_query;
2292 $prev_was_joiner = 0;
2296 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2298 if ($self->phrases and @{$self->phrases} and not $opts{no_phrases}) {
2299 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2300 if ($open_joiner ne '&') {
2301 my $oldroot = $abstract_query->{children};
2303 $abstract_query->{children} = {'&' => $kids};
2306 for my $phrase (@{$self->phrases}) {
2307 # Phrases appear duplication in a real QP tree, and we don't want
2308 # that duplication in our abstract query. So for all our phrases,
2309 # break them into atoms as QP would, and remove any matching
2310 # sequences of atoms from our abstract query.
2312 my $tmp_prefix = '';
2313 $tmp_prefix = $QueryParser::parser_config{$pkg}{operators}{disallowed} if ($self->{negate});
2315 my $tmptree = $self->{plan}->{QueryParser}->new(query => $phrase)->parse->parse_tree;
2317 # For a well-behaved phrase, we should now have only one node
2318 # in the $tmptree query plan, and that node should have an
2319 # orderly list of atoms and joiners.
2321 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2325 $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2327 )->{children}->{'&'};
2329 next if $@ or !ref($tmplist);
2331 $$tmplist[0]{prefix} = $tmp_prefix.'"';
2332 $$tmplist[-1]{suffix} = '"';
2333 push @{$abstract_query->{children}->{'&'}}, @$tmplist;
2339 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2341 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2342 return undef unless @{$abstract_query->{children}->{$open_joiner}};
2344 return $abstract_query;
2347 #-------------------------------
2348 package QueryParser::query_plan::node::atom;
2352 $pkg = ref($pkg) || $pkg;
2355 return bless \%args => $pkg;
2360 return undef unless (ref $self);
2361 return $self->{node};
2366 return undef unless (ref $self);
2367 return $self->{content};
2372 return undef unless (ref $self);
2373 return $self->{prefix};
2378 return undef unless (ref $self);
2379 return $self->{suffix};
2382 sub explicit_start {
2384 my $explicit_start = shift;
2386 $self->{explicit_start} = $explicit_start if (defined $explicit_start);
2388 return $self->{explicit_start};
2395 $self->{dummy} = $dummy if (defined $dummy);
2397 return $self->{dummy};
2402 my $explicit_end = shift;
2404 $self->{explicit_end} = $explicit_end if (defined $explicit_end);
2406 return $self->{explicit_end};
2409 sub to_abstract_query {
2413 (map { $_ => $self->$_ } qw/dummy prefix suffix content explicit_start explicit_end/),
2417 #-------------------------------
2418 package QueryParser::query_plan::filter;
2422 $pkg = ref($pkg) || $pkg;
2425 return bless \%args => $pkg;
2430 return $self->{plan};
2435 return $self->{name};
2440 return $self->{negate};
2445 return $self->{args};
2448 sub to_abstract_query {
2452 map { $_ => $self->$_ } qw/name negate args/
2456 #-------------------------------
2457 package QueryParser::query_plan::facet;
2461 $pkg = ref($pkg) || $pkg;
2464 return bless \%args => $pkg;
2469 return $self->{plan};
2474 return $self->{name};
2479 return $self->{negate};
2484 return $self->{'values'};
2487 sub to_abstract_query {
2491 (map { $_ => $self->$_ } qw/name negate values/),
2496 #-------------------------------
2497 package QueryParser::query_plan::modifier;
2501 $pkg = ref($pkg) || $pkg;
2502 my $modifier = shift;
2505 return bless { name => $modifier, negate => $negate } => $pkg;
2510 return $self->{name};
2515 return $self->{negate};
2518 sub to_abstract_query {