From 0742457840edf7784d5bccafaa6b6383125a53ed Mon Sep 17 00:00:00 2001 From: Jared Camins-Esakov Date: Thu, 2 May 2013 11:53:36 -0400 Subject: [PATCH] Bug 9961: Add truncation support to QP driver The original implementation of QueryParser did not handle truncation based on the QueryAutoTruncate system preference. This patch adds support. To test: 1) Apply patch. 2) Turn on UseQueryParser. 3) Set QueryAutoTruncate to "automatically." 4) Search for "har". Note that it returns results with words like "Harry" (i.e. with right truncation). 5) Search for "har*". Note that it still returns results with right truncation. 6) Set QueryAutoTruncate to "only when * is added." 7) Search for "har". Note that it returns only records that have the exact word "har" in them (most likely there will be none unless you have Hebrew items). 8) Search for "har*". Note that once again it returns results for "Harry" (i.e. right truncated results). 9) Sign off. This patch also reindents a hash in Koha/QueryParser/Driver/PQF.pm because it was hard to read before. Signed-off-by: Mirko Tietgen Signed-off-by: Katrin Fischer All tests and QA script pass. Thx for fixing this Jared! Signed-off-by: Jared Camins-Esakov (cherry picked from commit 8c6fe639b812665339a2cd84083f8c75d3957fdd) Signed-off-by: Jared Camins-Esakov --- C4/Search.pm | 5 ++++ Koha/QueryParser/Driver/PQF.pm | 26 +++++++++---------- .../QueryParser/Driver/PQF/query_plan/node.pm | 4 +++ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index da729ad957..aec024b935 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -112,6 +112,7 @@ sub FindDuplicate { $titleindex = 'title|exact'; $authorindex = 'author|exact'; $op = '&&'; + $QParser->custom_data->{'QueryAutoTruncate'} = C4::Context->preference('QueryAutoTruncate'); } else { $titleindex = 'ti,ext'; $authorindex = 'au,ext'; @@ -233,6 +234,9 @@ sub SimpleSearch { my $QParser; $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser') && ! ($query =~ m/\w,\w|\w=\w/)); + if ($QParser) { + $QParser->custom_data->{'QueryAutoTruncate'} = C4::Context->preference('QueryAutoTruncate'); + } # Initialize & Search Zebra for ( my $i = 0 ; $i < @servers ; $i++ ) { @@ -1177,6 +1181,7 @@ sub parseQuery { if ($QParser) { + $QParser->custom_data->{'QueryAutoTruncate'} = C4::Context->preference('QueryAutoTruncate'); $query = ''; for ( my $ii = 0 ; $ii <= @operands ; $ii++ ) { next unless $operands[$ii]; diff --git a/Koha/QueryParser/Driver/PQF.pm b/Koha/QueryParser/Driver/PQF.pm index 3007333692..d4551f0f58 100644 --- a/Koha/QueryParser/Driver/PQF.pm +++ b/Koha/QueryParser/Driver/PQF.pm @@ -349,23 +349,23 @@ not want to use it. sub clear_all_configuration { my ($self) = @_; %OpenILS::QueryParser::parser_config = ( - 'OpenILS::QueryParser' => { + 'OpenILS::QueryParser' => { filters => [], modifiers => [], operators => { - 'and' => '&&', - 'or' => '||', - float_start => '{{', - float_end => '}}', - group_start => '(', - group_end => ')', - required => '+', - disallowed => '-', - modifier => '#', - negated => '!' + 'and' => '&&', + 'or' => '||', + float_start => '{{', + float_end => '}}', + group_start => '(', + group_end => ')', + required => '+', + disallowed => '-', + modifier => '#', + negated => '!' } - } - ); + } + ); return $self; } diff --git a/Koha/QueryParser/Driver/PQF/query_plan/node.pm b/Koha/QueryParser/Driver/PQF/query_plan/node.pm index 7d05937387..e88ef84e02 100644 --- a/Koha/QueryParser/Driver/PQF/query_plan/node.pm +++ b/Koha/QueryParser/Driver/PQF/query_plan/node.pm @@ -57,6 +57,7 @@ sub target_syntax { $phrase =~ s/"/\\"/g; $pqf .= ' @or ' x (scalar(@fields) - 1); foreach my $attributes (@fields) { + $attributes->{'attr_string'} ||= ''; $pqf .= $attributes->{'attr_string'} . ($attributes->{'4'} ? '' : ' @attr 4=1') . ' "' . $phrase . '" '; } $atom_count++; @@ -70,6 +71,9 @@ sub target_syntax { $pqf .= ' @or ' x (scalar(@fields) - 1); foreach my $attributes (@fields) { $attributes->{'attr_string'} ||= ''; + if ($self->plan->QueryParser->custom_data->{'QueryAutoTruncate'} || $atom->suffix eq '*') { + $attributes->{'attr_string'} .= ($attributes->{'5'} ? '' : ' @attr 5=1 '); + } $pqf .= $attributes->{'attr_string'} . ($attributes->{'4'} ? '' : ' @attr 4=6 ') . $atom_content . ' '; } $atom_count++; -- 2.39.2