5 use OpenSRF::Utils::JSON;
9 QueryParser - basic QueryParser class
14 my $QParser = QueryParser->new(%args);
18 Main entrypoint into the QueryParser functionality.
24 # Note that the first key must match the name of the package.
25 our %parser_config = (
46 return QueryParser::Canonicalize::abstract_query2str_impl(
47 $self->parse_tree->to_abstract_query(@_)
52 =head2 facet_class_count
54 $count = $QParser->facet_class_count();
57 sub facet_class_count {
59 return @{$self->facet_classes};
62 =head2 search_class_count
64 $count = $QParser->search_class_count();
67 sub search_class_count {
69 return @{$self->search_classes};
74 $count = $QParser->filter_count();
79 return @{$self->filters};
84 $count = $QParser->modifier_count();
89 return @{$self->modifiers};
94 $data = $QParser->custom_data($class);
99 $class = ref($class) || $class;
101 $parser_config{$class}{custom_data} ||= {};
102 return $parser_config{$class}{custom_data};
107 $operators = $QParser->operators();
109 Returns hashref of the configured operators.
114 $class = ref($class) || $class;
116 $parser_config{$class}{operators} ||= {};
117 return $parser_config{$class}{operators};
120 sub allow_nested_modifiers {
123 $class = ref($class) || $class;
125 $parser_config{$class}{allow_nested_modifiers} = $v if (defined $v);
126 return $parser_config{$class}{allow_nested_modifiers};
131 $filters = $QParser->filters();
133 Returns arrayref of the configured filters.
138 $class = ref($class) || $class;
140 $parser_config{$class}{filters} ||= [];
141 return $parser_config{$class}{filters};
144 =head2 filter_callbacks
146 $filter_callbacks = $QParser->filter_callbacks();
148 Returns hashref of the configured filter callbacks.
151 sub filter_callbacks {
153 $class = ref($class) || $class;
155 $parser_config{$class}{filter_callbacks} ||= {};
156 return $parser_config{$class}{filter_callbacks};
161 $modifiers = $QParser->modifiers();
163 Returns arrayref of the configured modifiers.
168 $class = ref($class) || $class;
170 $parser_config{$class}{modifiers} ||= [];
171 return $parser_config{$class}{modifiers};
176 $QParser = QueryParser->new(%args);
178 Creates a new QueryParser object.
183 $class = ref($class) || $class;
187 my $self = bless {} => $class;
189 for my $o (keys %{QueryParser->operators}) {
190 $class->operator($o => QueryParser->operator($o)) unless ($class->operator($o));
193 for my $opt ( keys %opts) {
194 $self->$opt( $opts{$opt} ) if ($self->can($opt));
202 $query_plan = $QParser->new_plan();
204 Create a new query plan.
209 my $pkg = ref($self) || $self;
210 return do{$pkg.'::query_plan'}->new( QueryParser => $self, @_ );
213 =head2 add_search_filter
215 $QParser->add_search_filter($filter, [$callback]);
217 Adds a filter with the specified name and an optional callback to the
218 QueryParser configuration.
221 sub add_search_filter {
223 $pkg = ref($pkg) || $pkg;
225 my $callback = shift;
227 return $filter if (grep { $_ eq $filter } @{$pkg->filters});
228 push @{$pkg->filters}, $filter;
229 $pkg->filter_callbacks->{$filter} = $callback if ($callback);
233 =head2 add_search_modifier
235 $QParser->add_search_modifier($modifier);
237 Adds a modifier with the specified name to the QueryParser configuration.
240 sub add_search_modifier {
242 $pkg = ref($pkg) || $pkg;
243 my $modifier = shift;
245 return $modifier if (grep { $_ eq $modifier } @{$pkg->modifiers});
246 push @{$pkg->modifiers}, $modifier;
250 =head2 add_facet_class
252 $QParser->add_facet_class($facet_class);
254 Adds a facet class with the specified name to the QueryParser configuration.
257 sub add_facet_class {
259 $pkg = ref($pkg) || $pkg;
262 return $class if (grep { $_ eq $class } @{$pkg->facet_classes});
264 push @{$pkg->facet_classes}, $class;
265 $pkg->facet_fields->{$class} = [];
270 =head2 add_search_class
272 $QParser->add_search_class($class);
274 Adds a search class with the specified name to the QueryParser configuration.
277 sub add_search_class {
279 $pkg = ref($pkg) || $pkg;
282 return $class if (grep { $_ eq $class } @{$pkg->search_classes});
284 push @{$pkg->search_classes}, $class;
285 $pkg->search_fields->{$class} = [];
286 $pkg->default_search_class( $pkg->search_classes->[0] ) if (@{$pkg->search_classes} == 1);
291 =head2 add_search_modifier
293 $op = $QParser->operator($operator, [$newvalue]);
295 Retrieves or sets value for the specified operator. Valid operators and
296 their defaults are as follows:
304 =item * group_start => (
306 =item * group_end => )
308 =item * required => +
310 =item * disallowed => -
312 =item * modifier => #
320 $class = ref($class) || $class;
324 return undef unless ($opname);
326 $parser_config{$class}{operators} ||= {};
327 $parser_config{$class}{operators}{$opname} = $op if ($op);
329 return $parser_config{$class}{operators}{$opname};
334 $classes = $QParser->facet_classes([\@newclasses]);
336 Returns arrayref of all configured facet classes after optionally
337 replacing configuration.
342 $class = ref($class) || $class;
345 $parser_config{$class}{facet_classes} ||= [];
346 $parser_config{$class}{facet_classes} = $classes if (ref($classes) && @$classes);
347 return $parser_config{$class}{facet_classes};
350 =head2 search_classes
352 $classes = $QParser->search_classes([\@newclasses]);
354 Returns arrayref of all configured search classes after optionally
355 replacing the previous configuration.
360 $class = ref($class) || $class;
363 $parser_config{$class}{classes} ||= [];
364 $parser_config{$class}{classes} = $classes if (ref($classes) && @$classes);
365 return $parser_config{$class}{classes};
368 =head2 add_query_normalizer
370 $function = $QParser->add_query_normalizer($class, $field, $func, [\@params]);
374 sub add_query_normalizer {
376 $pkg = ref($pkg) || $pkg;
380 my $params = shift || [];
382 # do not add if function AND params are identical to existing member
383 return $func if (grep {
384 $_->{function} eq $func and
385 OpenSRF::Utils::JSON->perl2JSON($_->{params}) eq OpenSRF::Utils::JSON->perl2JSON($params)
386 } @{$pkg->query_normalizers->{$class}->{$field}});
388 push(@{$pkg->query_normalizers->{$class}->{$field}}, { function => $func, params => $params });
393 =head2 query_normalizers
395 $normalizers = $QParser->query_normalizers($class, $field);
397 Returns a list of normalizers associated with the specified search class
401 sub query_normalizers {
403 $pkg = ref($pkg) || $pkg;
408 $parser_config{$pkg}{normalizers} ||= {};
411 $parser_config{$pkg}{normalizers}{$class}{$field} ||= [];
412 return $parser_config{$pkg}{normalizers}{$class}{$field};
414 return $parser_config{$pkg}{normalizers}{$class};
418 return $parser_config{$pkg}{normalizers};
421 =head2 add_filter_normalizer
423 $normalizer = $QParser->add_filter_normalizer($filter, $func, [\@params]);
425 Adds a normalizer function to the specified filter.
428 sub add_filter_normalizer {
430 $pkg = ref($pkg) || $pkg;
433 my $params = shift || [];
435 return $func if (grep { $_ eq $func } @{$pkg->filter_normalizers->{$filter}});
437 push(@{$pkg->filter_normalizers->{$filter}}, { function => $func, params => $params });
442 =head2 filter_normalizers
444 $normalizers = $QParser->filter_normalizers($filter);
446 Return arrayref of normalizer functions associated with the specified filter.
449 sub filter_normalizers {
451 $pkg = ref($pkg) || $pkg;
455 $parser_config{$pkg}{filter_normalizers} ||= {};
457 $parser_config{$pkg}{filter_normalizers}{$filter} ||= [];
458 return $parser_config{$pkg}{filter_normalizers}{$filter};
461 return $parser_config{$pkg}{filter_normalizers};
464 =head2 default_search_class
466 $default_class = $QParser->default_search_class([$class]);
468 Set or return the default search class.
471 sub default_search_class {
473 $pkg = ref($pkg) || $pkg;
475 $QueryParser::parser_config{$pkg}{default_class} = $pkg->add_search_class( $class ) if $class;
477 return $QueryParser::parser_config{$pkg}{default_class};
480 =head2 remove_facet_class
482 $QParser->remove_facet_class($class);
484 Remove the specified facet class from the configuration.
487 sub remove_facet_class {
489 $pkg = ref($pkg) || $pkg;
492 return $class if (!grep { $_ eq $class } @{$pkg->facet_classes});
494 $pkg->facet_classes( [ grep { $_ ne $class } @{$pkg->facet_classes} ] );
495 delete $QueryParser::parser_config{$pkg}{facet_fields}{$class};
500 =head2 remove_search_class
502 $QParser->remove_search_class($class);
504 Remove the specified search class from the configuration.
507 sub remove_search_class {
509 $pkg = ref($pkg) || $pkg;
512 return $class if (!grep { $_ eq $class } @{$pkg->search_classes});
514 $pkg->search_classes( [ grep { $_ ne $class } @{$pkg->search_classes} ] );
515 delete $QueryParser::parser_config{$pkg}{fields}{$class};
520 =head2 add_facet_field
522 $QParser->add_facet_field($class, $field);
524 Adds the specified field (and facet class if it doesn't already exist)
525 to the configuration.
528 sub add_facet_field {
530 $pkg = ref($pkg) || $pkg;
534 $pkg->add_facet_class( $class );
536 return { $class => $field } if (grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
538 push @{$pkg->facet_fields->{$class}}, $field;
540 return { $class => $field };
545 $fields = $QParser->facet_fields($class);
547 Returns arrayref with list of fields for specified facet class.
552 $class = ref($class) || $class;
554 $parser_config{$class}{facet_fields} ||= {};
555 return $parser_config{$class}{facet_fields};
558 =head2 add_search_field
560 $QParser->add_search_field($class, $field);
562 Adds the specified field (and facet class if it doesn't already exist)
563 to the configuration.
566 sub add_search_field {
568 $pkg = ref($pkg) || $pkg;
572 $pkg->add_search_class( $class );
574 return { $class => $field } if (grep { $_ eq $field } @{$pkg->search_fields->{$class}});
576 push @{$pkg->search_fields->{$class}}, $field;
578 return { $class => $field };
583 $fields = $QParser->search_fields();
585 Returns arrayref with list of configured search fields.
590 $class = ref($class) || $class;
592 $parser_config{$class}{fields} ||= {};
593 return $parser_config{$class}{fields};
596 =head2 add_search_class_alias
598 $QParser->add_search_class_alias($class, $alias);
601 sub add_search_class_alias {
603 $pkg = ref($pkg) || $pkg;
607 $pkg->add_search_class( $class );
609 return { $class => $alias } if (grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
611 push @{$pkg->search_class_aliases->{$class}}, $alias;
613 return { $class => $alias };
616 =head2 search_class_aliases
618 $aliases = $QParser->search_class_aliases($class);
621 sub search_class_aliases {
623 $class = ref($class) || $class;
625 $parser_config{$class}{class_map} ||= {};
626 return $parser_config{$class}{class_map};
629 =head2 add_search_field_alias
631 $QParser->add_search_field_alias($class, $field, $alias);
634 sub add_search_field_alias {
636 $pkg = ref($pkg) || $pkg;
641 return { $class => { $field => $alias } } if (grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
643 push @{$pkg->search_field_aliases->{$class}{$field}}, $alias;
645 return { $class => { $field => $alias } };
648 =head2 search_field_aliases
650 $aliases = $QParser->search_field_aliases();
653 sub search_field_aliases {
655 $class = ref($class) || $class;
657 $parser_config{$class}{field_alias_map} ||= {};
658 return $parser_config{$class}{field_alias_map};
661 =head2 remove_facet_field
663 $QParser->remove_facet_field($class, $field);
666 sub remove_facet_field {
668 $pkg = ref($pkg) || $pkg;
672 return { $class => $field } if (!$pkg->facet_fields->{$class} || !grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
674 $pkg->facet_fields->{$class} = [ grep { $_ ne $field } @{$pkg->facet_fields->{$class}} ];
676 return { $class => $field };
679 =head2 remove_search_field
681 $QParser->remove_search_field($class, $field);
684 sub remove_search_field {
686 $pkg = ref($pkg) || $pkg;
690 return { $class => $field } if (!$pkg->search_fields->{$class} || !grep { $_ eq $field } @{$pkg->search_fields->{$class}});
692 $pkg->search_fields->{$class} = [ grep { $_ ne $field } @{$pkg->search_fields->{$class}} ];
694 return { $class => $field };
697 =head2 remove_search_field_alias
699 $QParser->remove_search_field_alias($class, $field, $alias);
702 sub remove_search_field_alias {
704 $pkg = ref($pkg) || $pkg;
709 return { $class => { $field => $alias } } if (!$pkg->search_field_aliases->{$class}{$field} || !grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
711 $pkg->search_field_aliases->{$class}{$field} = [ grep { $_ ne $alias } @{$pkg->search_field_aliases->{$class}{$field}} ];
713 return { $class => { $field => $alias } };
716 =head2 remove_search_class_alias
718 $QParser->remove_search_class_alias($class, $alias);
721 sub remove_search_class_alias {
723 $pkg = ref($pkg) || $pkg;
727 return { $class => $alias } if (!$pkg->search_class_aliases->{$class} || !grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
729 $pkg->search_class_aliases->{$class} = [ grep { $_ ne $alias } @{$pkg->search_class_aliases->{$class}} ];
731 return { $class => $alias };
736 $debug = $QParser->debug([$debug]);
738 Return or set whether debugging output is enabled.
744 $self->{_debug} = $q if (defined $q);
745 return $self->{_debug};
750 $query = $QParser->query([$query]);
752 Return or set the query.
758 $self->{_query} = " $q " if (defined $q);
759 return $self->{_query};
764 $parse_tree = $QParser->parse_tree([$parse_tree]);
766 Return or set the parse tree associated with the QueryParser.
772 $self->{_parse_tree} = $q if (defined $q);
773 return $self->{_parse_tree};
779 $self->{_top} = $q if (defined $q);
780 return $self->{_top};
785 $QParser->parse([$query]);
787 Parse the specified query, or the query already associated with the QueryParser
792 our $last_class = '';
797 my $pkg = ref($self) || $self;
798 warn " ** parse package is $pkg\n" if $self->debug;
800 # Reset at each top-level parsing request
806 $self->decompose( $self->query( shift() ) );
808 if ($self->floating_plan) {
809 $self->floating_plan->add_node( $self->parse_tree );
810 $self->parse_tree( $self->floating_plan );
813 $self->parse_tree->plan_level(0);
820 ($struct, $remainder) = $QParser->decompose($querystring, [$current_class], [$recursing], [$phrase_helper]);
822 This routine does the heavy work of parsing the query string recursively.
823 Returns the top level query plan, or the query plan from a lower level plus
824 the portion of the query string that needs to be processed at a higher level.
827 our $_compiled_decomposer = {};
830 my $pkg = ref($self) || $self;
832 my $r = $$_compiled_decomposer{$pkg};
833 my $compiled = defined($r);
836 my $current_class = shift || $self->default_search_class;
838 my $recursing = shift || 0;
839 my $phrase_helper = shift || 0;
841 warn ' 'x$recursing." ** QP: decompose package is $pkg" if $self->debug;
844 $r = $$_compiled_decomposer{$pkg} = {};
845 warn ' 'x$recursing." ** Compiling decomposer\n" if $self->debug;
847 # Build the search class+field uber-regexp
848 $$r{search_class_re} = '^\s*(';
850 warn ' 'x$recursing." ** Decomposer already compiled\n" if $self->debug;
856 for my $class ( keys %{$pkg->search_field_aliases} ) {
857 warn ' 'x$recursing." *** ... Looking for search fields in $class\n" if $self->debug;
859 for my $field ( keys %{$pkg->search_field_aliases->{$class}} ) {
860 warn ' 'x$recursing." *** ... Looking for aliases of $field\n" if $self->debug;
862 for my $alias ( @{$pkg->search_field_aliases->{$class}{$field}} ) {
863 my $aliasr = qr/$alias/;
864 s/(^|\s+)$aliasr\|/$1$class\|$field#$alias\|/g;
865 s/(^|\s+)$aliasr[:=]/$1$class\|$field#$alias:/g;
866 warn ' 'x$recursing." *** Rewriting: $alias ($aliasr) as $class\|$field\n" if $self->debug;
871 $$r{search_class_re} .= '|' unless ($first_class);
873 $$r{search_class_re} .= $class . '(?:[|#][^:|]+)*';
874 $seen_classes{$class} = 1;
878 for my $class ( keys %{$pkg->search_class_aliases} ) {
880 for my $alias ( @{$pkg->search_class_aliases->{$class}} ) {
881 my $aliasr = qr/$alias/;
882 s/(^|[^|])\b$aliasr\|/$1$class#$alias\|/g;
883 s/(^|[^|])\b$aliasr[:=]/$1$class#$alias:/g;
884 warn ' 'x$recursing." *** Rewriting: $alias ($aliasr) as $class\n" if $self->debug;
887 if (!$compiled and !$seen_classes{$class}) {
888 $$r{search_class_re} .= '|' unless ($first_class);
891 $$r{search_class_re} .= $class . '(?:[|#][^:|]+)*';
892 $seen_classes{$class} = 1;
895 $$r{search_class_re} .= '):' if (!$compiled);
897 warn ' 'x$recursing." ** Rewritten query: $_\n" if $self->debug;
899 my $group_start = $pkg->operator('group_start');
900 my $group_end = $pkg->operator('group_end');
902 warn ' 'x$recursing." ** Search class RE: $$r{search_class_re}\n" if $self->debug;
904 my $required_op = $pkg->operator('required');
905 $$r{required_re} = qr/\Q$required_op\E/;
907 my $disallowed_op = $pkg->operator('disallowed');
908 $$r{disallowed_re} = qr/\Q$disallowed_op\E/;
910 my $negated_op = $pkg->operator('negated');
911 $$r{negated_re} = qr/\Q$negated_op\E/;
913 my $and_op = $pkg->operator('and');
914 $$r{and_re} = qr/^\s*\Q$and_op\E/;
916 my $or_op = $pkg->operator('or');
917 $$r{or_re} = qr/^\s*\Q$or_op\E/;
919 $$r{group_start_re} = qr/^\s*($$r{negated_re}|$$r{disallowed_re})?\Q$group_start\E/;
921 $$r{group_end_re} = qr/^\s*\Q$group_end\E/;
923 my $float_start = $pkg->operator('float_start');
924 $$r{float_start_re} = qr/^\s*\Q$float_start\E/;
926 my $float_end = $pkg->operator('float_end');
927 $$r{float_end_re} = qr/^\s*\Q$float_end\E/;
929 $$r{atom_re} = qr/.+?(?=\Q$float_start\E|\Q$group_start\E|\Q$float_end\E|\Q$group_end\E|\s|"|$)/;
931 my $modifier_tag = $pkg->operator('modifier');
932 $$r{modifier_tag_re} = qr/^\s*\Q$modifier_tag\E/;
934 # Group start/end normally are ( and ), but can be overridden.
935 # We thus include ( and ) specifically due to filters, as well as : for classes.
936 $$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|:|\(|\))/;
938 # Build the filter and modifier uber-regexps
939 $$r{facet_re} = '^\s*(-?)((?:' . join( '|', @{$pkg->facet_classes}) . ')(?:\|\w+)*)\[(.+?)\](?!\[)';
941 $$r{filter_re} = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . ')\(([^()]+)\)';
942 $$r{filter_as_class_re} = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . '):\s*(\S+)';
944 $$r{modifier_re} = '^\s*'.$$r{modifier_tag_re}.'(' . join( '|', @{$pkg->modifiers}) . ')\b';
945 $$r{modifier_as_class_re} = '^\s*(' . join( '|', @{$pkg->modifiers}) . '):\s*(\S+)';
949 my $struct = shift || $self->new_plan( level => $recursing );
950 $self->parse_tree( $struct ) if (!$self->parse_tree);
954 while (!$remainder) {
955 warn ' 'x$recursing."Start of the loop. last_type: $last_type, joiner: ".$struct->joiner.", struct: $struct\n" if $self->debug;
956 if ($last_type eq 'FEND' and $fstart and $fstart != $struct) { # fall back further
959 } elsif ($last_type eq 'FEND') {
964 if (/^\s*$/) { # end of an explicit group
967 } elsif (/$$r{float_end_re}/) { # end of an explicit group
968 warn ' 'x$recursing."Encountered explicit float end, remainder: $'\n" if $self->debug;
976 } elsif (/$$r{group_end_re}/) { # end of an explicit group
977 warn ' 'x$recursing."Encountered explicit group end, remainder: $'\n" if $self->debug;
983 } elsif ($self->filter_count && /$$r{filter_re}/) { # found a filter
984 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
986 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
990 my $params = [ split '[,]+', $3 ];
992 if ($pkg->filter_callbacks->{$filter}) {
993 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
994 $_ = "$replacement $_" if ($replacement);
996 $struct->new_filter( $filter => $params, $negate );
1001 } elsif ($self->filter_count && /$$r{filter_as_class_re}/) { # found a filter
1002 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
1004 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1008 my $params = [ split '[,]+', $3 ];
1010 if ($pkg->filter_callbacks->{$filter}) {
1011 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
1012 $_ = "$replacement $_" if ($replacement);
1014 $struct->new_filter( $filter => $params, $negate );
1018 } elsif ($self->modifier_count && /$$r{modifier_re}/) { # found a modifier
1019 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1022 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1023 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1025 $struct->new_modifier($1);
1029 } elsif ($self->modifier_count && /$$r{modifier_as_class_re}/) { # found a modifier
1030 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1035 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1036 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1037 } elsif ($2 =~ /^[ty1]/i) {
1038 $struct->new_modifier($mod);
1042 } elsif (/$$r{float_start_re}/) { # start of an explicit float
1043 warn ' 'x$recursing."Encountered explicit float start\n" if $self->debug;
1047 $last_class = $current_class;
1048 $current_class = undef;
1050 $self->floating_plan( $self->new_plan( floating => 1 ) ) if (!$self->floating_plan);
1052 # pass the floating_plan struct to be modified by the float'ed chunk
1053 my ($floating_plan, $subremainder) = $self->new( debug => $self->debug )->decompose( $', undef, undef, undef, $self->floating_plan);
1055 warn ' 'x$recursing."Remainder after explicit float: $_\n" if $self->debug;
1057 $current_class = $last_class;
1060 } elsif (/$$r{group_start_re}/) { # start of an explicit group
1061 warn ' 'x$recursing."Encountered explicit group start\n" if $self->debug;
1063 my ($substruct, $subremainder) = $self->decompose( $', $current_class, $recursing + 1 );
1064 $substruct->negate(1) if ($substruct && $negate);
1065 $struct->add_node( $substruct ) if ($substruct);
1067 warn ' 'x$recursing."Query remainder after bool group: $_\n" if $self->debug;
1071 } elsif (/$$r{and_re}/) { # ANDed expression
1073 warn ' 'x$recursing."Encountered AND\n" if $self->debug;
1074 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1075 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1078 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1080 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1081 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 1 );
1084 warn ' 'x$recursing."RHS built\n" if $self->debug;
1085 warn ' 'x$recursing."Post-AND remainder: $subremainder\n" if $self->debug;
1087 my $wrapper = $self->new_plan( level => $recursing + 1 );
1089 if ($LHS->floating) {
1090 $wrapper->{query} = $LHS->{query};
1091 my $outer_wrapper = $self->new_plan( level => $recursing + 1 );
1092 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1093 $LHS->{query} = [$outer_wrapper];
1096 $wrapper->add_node($_) for ($LHS, $RHS);
1097 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1098 $struct = $self->new_plan( level => $recursing );
1099 $struct->add_node($wrapper);
1102 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1105 } elsif (/$$r{or_re}/) { # ORed expression
1107 warn ' 'x$recursing."Encountered OR\n" if $self->debug;
1108 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1109 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1112 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1114 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1115 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 2 );
1118 warn ' 'x$recursing."RHS built\n" if $self->debug;
1119 warn ' 'x$recursing."Post-OR remainder: $subremainder\n" if $self->debug;
1121 my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1123 if ($LHS->floating) {
1124 $wrapper->{query} = $LHS->{query};
1125 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1126 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1127 $LHS->{query} = [$outer_wrapper];
1130 $wrapper->add_node($_) for ($LHS, $RHS);
1131 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1132 $struct = $self->new_plan( level => $recursing );
1133 $struct->add_node($wrapper);
1136 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1139 } elsif ($self->facet_class_count && /$$r{facet_re}/) { # changing current class
1140 warn ' 'x$recursing."Encountered facet: $1$2 => $3\n" if $self->debug;
1142 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1144 my $facet_value = [ split '\s*\]\[\s*', $3 ];
1145 $struct->new_facet( $facet => $facet_value, $negate );
1149 } elsif ($self->search_class_count && /$$r{search_class_re}/) { # changing current class
1151 if ($last_type eq 'CLASS') {
1152 $struct->remove_last_node( $current_class );
1153 warn ' 'x$recursing."Encountered class change with no searches!\n" if $self->debug;
1156 warn ' 'x$recursing."Encountered class change: $1\n" if $self->debug;
1158 $current_class = $struct->classed_node( $1 )->requested_class();
1161 $last_type = 'CLASS';
1162 } elsif (/^\s*($$r{required_re}|$$r{disallowed_re}|$$r{negated_re})?"([^"]+)"/) { # phrase, always anded
1163 warn ' 'x$recursing.'Encountered' . ($1 ? " ['$1' modified]" : '') . " phrase: $2\n" if $self->debug;
1165 my $req_ness = $1 || '';
1166 $req_ness = $pkg->operator('disallowed') if ($req_ness eq $pkg->operator('negated'));
1169 if (!$phrase_helper) {
1170 warn ' 'x$recursing."Recursing into decompose with the phrase as a subquery\n" if $self->debug;
1172 my ($substruct, $subremainder) = $self->decompose( qq/$req_ness"$phrase"/, $current_class, $recursing + 1, 1 );
1173 $struct->add_node( $substruct ) if ($substruct);
1176 warn ' 'x$recursing."Directly parsing the phrase [ $phrase ] subquery\n" if $self->debug;
1177 $struct->joiner( '&' );
1179 my $class_node = $struct->classed_node($current_class);
1181 if ($req_ness eq $pkg->operator('disallowed')) {
1182 $class_node->negate(1);
1184 $class_node->add_phrase( $phrase );
1186 # Save $' before we clean up $phrase
1189 # Cleanup the phrase to make it so that we don't parse things in it as anything other than atoms
1190 $phrase =~ s/$$r{phrase_cleanup_re}/ /g;
1192 $_ = $phrase . $temp_val;
1198 } elsif (/^\s*($$r{required_re}|$$r{disallowed_re})($$r{atom_re})/) { # convert require/disallow word to {un}phrase
1199 warn ' 'x$recursing."Encountered required atom (mini phrase), transforming for phrase parse: $1\n" if $self->debug;
1201 $_ = $1 . '"' . $2 . '"' . $';
1204 } elsif (/^\s*($$r{atom_re})/) { # atom
1205 warn ' 'x$recursing."Encountered atom: $1\n" if $self->debug;
1206 warn ' 'x$recursing."Remainder: $'\n" if $self->debug;
1214 my $class_node = $struct->classed_node($current_class);
1216 my $prefix = ($atom =~ s/^$$r{negated_re}//o) ? '!' : '';
1217 my $truncate = ($atom =~ s/\*$//o) ? '*' : '';
1219 if ($atom ne '' and !grep { $atom =~ /^\Q$_\E+$/ } ('&','|')) { # throw away & and |, not allowed in tsquery, and not really useful anyway
1220 # $class_node->add_phrase( $atom ) if ($atom =~ s/^$$r{required_re}//o);
1222 $class_node->add_fts_atom( $atom, suffix => $truncate, prefix => $prefix, node => $class_node );
1223 $struct->joiner( '&' );
1228 warn ' 'x$recursing."Cannot parse: $_\n" if $self->debug;
1237 scalar(@{$struct->query_nodes}) == 0 &&
1238 scalar(@{$struct->filters}) == 0 &&
1241 return $struct if !wantarray;
1242 return ($struct, $remainder);
1245 =head2 find_class_index
1247 $index = $QParser->find_class_index($class, $query);
1250 sub find_class_index {
1254 my ($class_part, @field_parts) = split '\|', $class;
1255 $class_part ||= $class;
1257 for my $idx ( 0 .. scalar(@$query) - 1 ) {
1258 next unless ref($$query[$idx]);
1259 return $idx if ( $$query[$idx]{requested_class} && $class eq $$query[$idx]{requested_class} );
1262 push(@$query, { classname => $class_part, (@field_parts ? (fields => \@field_parts) : ()), requested_class => $class, ftsquery => [], phrases => [] });
1268 $limit = $QParser->core_limit([$limit]);
1270 Return and/or set the core_limit.
1276 $self->{core_limit} = $l if ($l);
1277 return $self->{core_limit};
1282 $superpage = $QParser->superpage([$superpage]);
1284 Return and/or set the superpage.
1290 $self->{superpage} = $l if ($l);
1291 return $self->{superpage};
1294 =head2 superpage_size
1296 $size = $QParser->superpage_size([$size]);
1298 Return and/or set the superpage size.
1301 sub superpage_size {
1304 $self->{superpage_size} = $l if ($l);
1305 return $self->{superpage_size};
1309 #-------------------------------
1310 package QueryParser::_util;
1312 # At this level, joiners are always & or |. This is not
1313 # the external, configurable representation of joiners that
1314 # defaults to # && and ||.
1318 return (not ref $str and ($str eq '&' or $str eq '|'));
1321 sub default_joiner { '&' }
1323 # 0 for different, 1 for the same.
1324 sub compare_abstract_atoms {
1325 my ($left, $right) = @_;
1327 foreach (qw/prefix suffix content/) {
1328 no warnings; # undef can stand in for '' here
1329 return 0 unless $left->{$_} eq $right->{$_};
1335 sub fake_abstract_atom_from_phrase {
1338 my $qp_class = shift || 'QueryParser';
1343 $QueryParser::parser_config{$qp_class}{operators}{disallowed} .
1348 "type" => "atom", "prefix" => $prefix, "suffix" => '"',
1349 "content" => $phrase
1353 sub find_arrays_in_abstract {
1357 foreach my $key (keys %$hash) {
1358 if (ref $hash->{$key} eq "ARRAY") {
1359 push @arrays, $hash->{$key};
1360 foreach (@{$hash->{$key}}) {
1361 push @arrays, find_arrays_in_abstract($_);
1369 #-------------------------------
1370 package QueryParser::Canonicalize; # not OO
1373 sub _abstract_query2str_filter {
1375 my $qp_class = shift || 'QueryParser';
1376 my $qpconfig = $QueryParser::parser_config{$qp_class};
1380 $f->{negate} ? $qpconfig->{operators}{disallowed} : "",
1382 join(",", @{$f->{args}})
1386 sub _abstract_query2str_modifier {
1388 my $qp_class = shift || 'QueryParser';
1389 my $qpconfig = $QueryParser::parser_config{$qp_class};
1391 return $qpconfig->{operators}{modifier} . $f;
1395 my $children = shift;
1396 my $op = (keys %$children)[0];
1397 return @{$$children{$op}};
1401 # This should produce an equivalent query to the original, given an
1403 sub abstract_query2str_impl {
1404 my $abstract_query = shift;
1405 my $depth = shift || 0;
1407 my $qp_class ||= shift || 'QueryParser';
1408 my $force_qp_node = shift || 0;
1409 my $qpconfig = $QueryParser::parser_config{$qp_class};
1411 my $fs = $qpconfig->{operators}{float_start};
1412 my $fe = $qpconfig->{operators}{float_end};
1413 my $gs = $qpconfig->{operators}{group_start};
1414 my $ge = $qpconfig->{operators}{group_end};
1415 my $and = $qpconfig->{operators}{and};
1416 my $or = $qpconfig->{operators}{or};
1417 my $ng = $qpconfig->{operators}{negated};
1424 if (exists $abstract_query->{type}) {
1425 if ($abstract_query->{type} eq 'query_plan') {
1426 $q .= join(" ", map { _abstract_query2str_filter($_, $qp_class) } @{$abstract_query->{filters}}) if
1427 exists $abstract_query->{filters};
1429 $q .= ($q ? ' ' : '') . join(" ", map { _abstract_query2str_modifier($_, $qp_class) } @{$abstract_query->{modifiers}}) if
1430 exists $abstract_query->{modifiers};
1432 $size = _kid_list($abstract_query->{children});
1433 if ($abstract_query->{negate}) {
1437 $isnode = 1 if ($size > 1 and ($force_qp_node or $depth));
1438 #warn "size: $size, depth: $depth, isnode: $isnode, AQ: ".Dumper($abstract_query);
1439 } elsif ($abstract_query->{type} eq 'node') {
1440 if ($abstract_query->{alias}) {
1441 $q .= ($q ? ' ' : '') . $abstract_query->{alias};
1442 $q .= "|$_" foreach @{$abstract_query->{alias_fields}};
1444 $q .= ($q ? ' ' : '') . $abstract_query->{class};
1445 $q .= "|$_" foreach @{$abstract_query->{fields}};
1449 } elsif ($abstract_query->{type} eq 'atom') {
1450 my $prefix = $abstract_query->{prefix} || '';
1451 $prefix = $qpconfig->{operators}{negated} if $prefix eq '!';
1452 $q .= ($q ? ' ' : '') . $prefix .
1453 ($abstract_query->{content} || '') .
1454 ($abstract_query->{suffix} || '');
1455 } elsif ($abstract_query->{type} eq 'facet') {
1456 # facet syntax [ # ] is hardcoded I guess?
1457 my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
1458 $q .= ($q ? ' ' : '') . $prefix . $abstract_query->{name} . "[" .
1459 join(" # ", @{$abstract_query->{values}}) . "]";
1463 my $next_depth = int($size > 1);
1465 if (exists $abstract_query->{children}) {
1467 my $op = (keys(%{$abstract_query->{children}}))[0];
1469 if ($abstract_query->{floating}) { # always the top node!
1470 my $sub_node = pop @{$abstract_query->{children}{$op}};
1472 $abstract_query->{floating} = 0;
1473 $q = $fs . " " . abstract_query2str_impl($abstract_query,0,$qp_class, 1) . $fe. " ";
1475 $abstract_query = $sub_node;
1478 if ($abstract_query && exists $abstract_query->{children}) {
1479 $op = (keys(%{$abstract_query->{children}}))[0];
1480 $q .= ($q ? ' ' : '') . join(
1481 ($op eq '&' ? ' ' : " $or "),
1483 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1484 } @{$abstract_query->{children}{$op}}
1487 } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1488 my $op = (keys(%{$abstract_query}))[0];
1489 $q .= ($q ? ' ' : '') . join(
1490 ($op eq '&' ? ' ' : " $or "),
1492 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1493 } @{$abstract_query->{$op}}
1497 $q = "$gs$q$ge" if ($isnode);
1498 $q = $negate . $q if ($q);;
1503 #-------------------------------
1504 package QueryParser::query_plan;
1508 return undef unless ref($self);
1509 return $self->{QueryParser};
1514 $pkg = ref($pkg) || $pkg;
1515 my %args = (query => [], joiner => '&', @_);
1517 return bless \%args => $pkg;
1522 my $pkg = ref($self) || $self;
1523 my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1524 $self->add_node( $node );
1530 my $pkg = ref($self) || $self;
1535 my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1536 $self->add_node( $node );
1543 my $pkg = ref($self) || $self;
1548 my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1549 $self->add_filter( $node );
1555 sub _merge_filters {
1556 my $left_filter = shift;
1557 my $right_filter = shift;
1560 return undef unless $left_filter or $right_filter;
1561 return $right_filter unless $left_filter;
1562 return $left_filter unless $right_filter;
1564 my $args = $left_filter->{args} || [];
1567 push(@$args, @{$right_filter->{args}});
1570 # find the intersect values
1572 map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1573 $args = [ grep { $new_vals{$_} } @$args ];
1576 $left_filter->{args} = $args;
1577 return $left_filter;
1580 sub collapse_filters {
1584 # start by merging any filters at this level.
1585 # like-level filters are always ORed together
1588 my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1590 $cur_filter = shift @cur_filters;
1591 my $args = $cur_filter->{args} || [];
1592 $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1595 # next gather the collapsed filters from sub-plans and
1596 # merge them with our own
1598 my @subquery = @{$self->{query}};
1601 my $blob = shift @subquery;
1602 shift @subquery; # joiner
1603 next unless $blob->isa('QueryParser::query_plan');
1604 my $sub_filter = $blob->collapse_filters($name);
1605 $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1608 if ($self->QueryParser->debug) {
1609 my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1610 warn "collapse_filters($name) => [@args]\n";
1618 my $needle = shift;;
1619 return undef unless ($needle);
1621 my $filter = $self->collapse_filters($needle);
1623 warn "find_filter($needle) => " .
1624 (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n"
1625 if $self->QueryParser->debug;
1627 return $filter ? ($filter) : ();
1632 my $needle = shift;;
1633 return undef unless ($needle);
1634 return grep { $_->name eq $needle } @{ $self->modifiers };
1639 my $pkg = ref($self) || $self;
1642 my $node = do{$pkg.'::modifier'}->new( $name );
1643 $self->add_modifier( $node );
1650 my $requested_class = shift;
1653 for my $n (@{$self->{query}}) {
1654 next unless (ref($n) && $n->isa( 'QueryParser::query_plan::node' ));
1655 if ($n->requested_class eq $requested_class) {
1662 $node = $self->new_node;
1663 $node->requested_class( $requested_class );
1669 sub remove_last_node {
1671 my $requested_class = shift;
1673 my $old = pop(@{$self->query_nodes});
1674 pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1681 return $self->{query};
1687 $self->{floating} = $f if (defined $f);
1688 return $self->{floating};
1695 $self->{query} ||= [];
1697 push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1698 push(@{$self->{query}}, $node);
1707 return $self->{level} ? 0 : 1;
1714 if (defined $level) {
1715 $self->{level} = $level;
1716 for (@{$self->query_nodes}) {
1717 $_->plan_level($level + 1) if (ref and $_->isa('QueryParser::query_plan'));
1721 return $self->{level};
1728 $self->{joiner} = $joiner if ($joiner);
1729 return $self->{joiner};
1734 $self->{modifiers} ||= [];
1735 return $self->{modifiers};
1740 my $modifier = shift;
1742 $self->{modifiers} ||= [];
1743 $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1745 push(@{$self->{modifiers}}, $modifier);
1752 $self->{facets} ||= [];
1753 return $self->{facets};
1760 $self->{facets} ||= [];
1761 $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
1763 push(@{$self->{facets}}, $facet);
1770 $self->{filters} ||= [];
1771 return $self->{filters};
1778 $self->{filters} ||= [];
1780 push(@{$self->{filters}}, $filter);
1789 $self->{negate} = $negate if (defined $negate);
1791 return $self->{negate};
1794 # %opts supports two options at this time:
1796 # If true, do not do anything to the phrases
1797 # fields on any discovered nodes.
1799 # If true, also return the query parser config as part of the blob.
1800 # This will get set back to 0 before recursion to avoid repetition.
1801 sub to_abstract_query {
1805 my $pkg = ref $self->QueryParser || $self->QueryParser;
1807 my $abstract_query = {
1808 type => "query_plan",
1809 floating => $self->floating,
1810 level => $self->plan_level,
1811 filters => [map { $_->to_abstract_query } @{$self->filters}],
1812 modifiers => [map { $_->to_abstract_query } @{$self->modifiers}],
1813 negate => $self->negate
1816 if ($opts{with_config}) {
1817 $opts{with_config} = 0;
1818 $abstract_query->{config} = $QueryParser::parser_config{$pkg};
1823 for my $qnode (@{$self->query_nodes}) {
1824 # Remember: qnode can be a joiner string, a node, or another query_plan
1826 if (QueryParser::_util::is_joiner($qnode)) {
1827 if ($abstract_query->{children}) {
1828 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
1829 next if $open_joiner eq $qnode;
1831 my $oldroot = $abstract_query->{children};
1833 $abstract_query->{children} = {$qnode => $kids};
1835 $abstract_query->{children} = {$qnode => $kids};
1838 push @$kids, $qnode->to_abstract_query(%opts);
1842 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
1843 return $abstract_query;
1847 #-------------------------------
1848 package QueryParser::query_plan::node;
1850 $Data::Dumper::Indent = 0;
1854 $pkg = ref($pkg) || $pkg;
1857 return bless \%args => $pkg;
1862 my $pkg = ref($self) || $self;
1863 return do{$pkg.'::atom'}->new( @_ );
1866 sub requested_class { # also split into classname, fields and alias
1872 my (undef, $alias) = split '#', $class;
1874 $class =~ s/#[^|]+//;
1875 ($alias, @afields) = split '\|', $alias;
1878 my @fields = @afields;
1879 my ($class_part, @field_parts) = split '\|', $class;
1880 for my $f (@field_parts) {
1881 push(@fields, $f) unless (grep { $f eq $_ } @fields);
1884 $class_part ||= $class;
1886 $self->{requested_class} = $class;
1887 $self->{alias} = $alias if $alias;
1888 $self->{alias_fields} = \@afields if $alias;
1889 $self->{classname} = $class_part;
1890 $self->{fields} = \@fields;
1893 return $self->{requested_class};
1900 $self->{plan} = $plan if ($plan);
1901 return $self->{plan};
1908 $self->{alias} = $alias if ($alias);
1909 return $self->{alias};
1916 $self->{alias_fields} = $alias if ($alias);
1917 return $self->{alias_fields};
1924 $self->{classname} = $class if ($class);
1925 return $self->{classname};
1932 $self->{fields} ||= [];
1933 $self->{fields} = \@fields if (@fields);
1934 return $self->{fields};
1941 $self->{phrases} ||= [];
1942 $self->{phrases} = \@phrases if (@phrases);
1943 return $self->{phrases};
1950 push(@{$self->phrases}, $phrase);
1959 $self->{negate} = $negate if (defined $negate);
1961 return $self->{negate};
1966 my @query_atoms = @_;
1968 $self->{query_atoms} ||= [];
1969 $self->{query_atoms} = \@query_atoms if (@query_atoms);
1970 return $self->{query_atoms};
1978 my $content = $atom;
1981 $atom = $self->new_atom( content => $content, @parts );
1984 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1985 push(@{$self->query_atoms}, $atom);
1990 sub add_dummy_atom {
1994 my $atom = $self->new_atom( @parts, dummy => 1 );
1996 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1997 push(@{$self->query_atoms}, $atom);
2002 # This will find up to one occurence of @$short_list within @$long_list, and
2003 # replace it with the single atom $replacement.
2004 sub replace_phrase_in_abstract_query {
2005 my ($self, $short_list, $long_list, $replacement) = @_;
2009 my $goal = scalar @$short_list;
2011 for (my $i = 0; $i < scalar (@$long_list); $i++) {
2012 my $right = $long_list->[$i];
2014 if (QueryParser::_util::compare_abstract_atoms(
2015 $short_list->[scalar @already], $right
2018 } elsif (scalar @already) {
2023 if (scalar @already == $goal) {
2024 splice @$long_list, $already[0], scalar(@already), $replacement;
2033 sub to_abstract_query {
2037 my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
2039 my $abstract_query = {
2041 "alias" => $self->alias,
2042 "alias_fields" => $self->alias_fields,
2043 "class" => $self->classname,
2044 "fields" => $self->fields
2049 for my $qatom (@{$self->query_atoms}) {
2050 if (QueryParser::_util::is_joiner($qatom)) {
2051 if ($abstract_query->{children}) {
2052 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2053 next if $open_joiner eq $qatom;
2055 my $oldroot = $abstract_query->{children};
2057 $abstract_query->{children} = {$qatom => $kids};
2059 $abstract_query->{children} = {$qatom => $kids};
2062 push @$kids, $qatom->to_abstract_query;
2066 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2068 if ($self->{phrases} and not $opts{no_phrases}) {
2069 for my $phrase (@{$self->{phrases}}) {
2070 # Phrases appear duplication in a real QP tree, and we don't want
2071 # that duplication in our abstract query. So for all our phrases,
2072 # break them into atoms as QP would, and remove any matching
2073 # sequences of atoms from our abstract query.
2075 my $tmp_prefix = '';
2076 $tmp_prefix = $QueryParser::parser_config{$pkg}{operators}{disallowed} if ($self->{negate});
2078 my $tmptree = $self->{plan}->{QueryParser}->new(query => $tmp_prefix.'"'.$phrase.'"')->parse->parse_tree;
2080 # For a well-behaved phrase, we should now have only one node
2081 # in the $tmptree query plan, and that node should have an
2082 # orderly list of atoms and joiners.
2084 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2088 $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2090 )->{children}->{'&'}->[0]->{children}->{'&'};
2095 QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
2097 last if $self->replace_phrase_in_abstract_query(
2100 QueryParser::_util::fake_abstract_atom_from_phrase($phrase, $self->{negate}, $pkg)
2108 $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2109 return $abstract_query;
2112 #-------------------------------
2113 package QueryParser::query_plan::node::atom;
2117 $pkg = ref($pkg) || $pkg;
2120 return bless \%args => $pkg;
2125 return undef unless (ref $self);
2126 return $self->{node};
2131 return undef unless (ref $self);
2132 return $self->{content};
2137 return undef unless (ref $self);
2138 return $self->{prefix};
2143 return undef unless (ref $self);
2144 return $self->{suffix};
2147 sub to_abstract_query {
2151 (map { $_ => $self->$_ } qw/prefix suffix content/),
2155 #-------------------------------
2156 package QueryParser::query_plan::filter;
2160 $pkg = ref($pkg) || $pkg;
2163 return bless \%args => $pkg;
2168 return $self->{plan};
2173 return $self->{name};
2178 return $self->{negate};
2183 return $self->{args};
2186 sub to_abstract_query {
2190 map { $_ => $self->$_ } qw/name negate args/
2194 #-------------------------------
2195 package QueryParser::query_plan::facet;
2199 $pkg = ref($pkg) || $pkg;
2202 return bless \%args => $pkg;
2207 return $self->{plan};
2212 return $self->{name};
2217 return $self->{negate};
2222 return $self->{'values'};
2225 sub to_abstract_query {
2229 (map { $_ => $self->$_ } qw/name negate values/),
2234 #-------------------------------
2235 package QueryParser::query_plan::modifier;
2239 $pkg = ref($pkg) || $pkg;
2240 my $modifier = shift;
2243 return bless { name => $modifier, negate => $negate } => $pkg;
2248 return $self->{name};
2253 return $self->{negate};
2256 sub to_abstract_query {