Bug 12647: PQF QueryParser driver and unit tests fixes
authorTomas Cohen Arazi <tomascohen@gmail.com>
Fri, 8 Aug 2014 17:48:59 +0000 (14:48 -0300)
committerTomas Cohen Arazi <tomascohen@gmail.com>
Mon, 11 Aug 2014 13:09:38 +0000 (10:09 -0300)
Due to Perl 5.18, QueryParser the default search class is no longer
'keyword' (see bug 12738), and needs to be set manually. This patch
adds a line that does that. The problem that gets fixed is with test
'super simple keyword query'.

The rest of the non-deterministically failing tests are due to the same
problem, keys returning differently sorted keys from hashes.

So this patch sorts keys in the step that concatenates attributes when building
the PQF queries (and tests get adjusted to match the now deterministic result).

I did that (sorting there) under Jared's recommendation. Hopefuly he will step
in and comment/fix any mistake I made. My main concern was a possible loss
in performance. That we agreed it is almost void, because of the tiny size
of the hash.

Sponsored-by: Universidad Nacional de Cordoba
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
All tests are passing now again :)

Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Koha/QueryParser/Driver/PQF/Util.pm
t/QueryParser.t

index 5d9ed71..af3c81b 100644 (file)
@@ -41,11 +41,10 @@ Convert a hashref with a Bib-1 mapping into its PQF string representation.
 sub attributes_to_attr_string {
     my ($attributes) = @_;
     my $attr_string = '';
-    my $key;
     my $value;
-    while (($key, $value) = each(%$attributes)) {
+    foreach my $key ( sort keys %{$attributes} ) {
         next unless looks_like_number($key);
-        $attr_string .= ' @attr ' . $key . '=' . $value . ' ';
+        $attr_string .= ' @attr ' . $key . '=' . $attributes->{ $key } . ' ';
     }
     $attr_string =~ s/^\s*//;
     $attr_string =~ s/\s*$//;
index aa1e7fd..7b69430 100644 (file)
@@ -19,20 +19,62 @@ ok($QParser->load_config('./etc/searchengine/queryparser.yaml'), 'Loaded QP conf
 is($QParser->search_class_count, 4, 'Initialized 4 search classes');
 is (scalar(@{$QParser->search_fields()->{'keyword'}}), 111, "Correct number of search fields for 'keyword' class");
 
-is($QParser->target_syntax('biblioserver', 'smith'), '@or @or @attr 1=1016 @attr 4=6 "smith" @attr 9=20 @attr 2=102 @attr 4=6 "smith" @attr 9=34 @attr 2=102 @attr 4=6 "smith"', 'super simple keyword query');
-is($QParser->target_syntax('biblioserver', 'au:smith'), '@attr 1=1003 @attr 4=6 "smith"', 'simple author query');
-is($QParser->target_syntax('biblioserver', 'keyword|publisher:smith'), '@attr 1=1018 @attr 4=6 "smith"', 'fielded publisher query');
-is($QParser->target_syntax('biblioserver', 'ti:"little engine that could"'), '@attr 1=4 @attr 4=1 "little engine that could"', 'phrase query');
-is($QParser->target_syntax('biblioserver', 'keyword|titlekw:smith'), '@attr 1=4 @attr 9=20 @attr 2=102 @attr 4=6 "smith"', 'relevance-bumped query');
-is($QParser->target_syntax('biblioserver', 'au:smith && johnson'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=1003 @attr 4=6 "johnson"', 'query with boolean &&');
-is($QParser->target_syntax('biblioserver', 'au:smith && ti:johnson'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=4 @attr 4=6 "johnson"', 'query with boolean &&');
-is($QParser->target_syntax('biblioserver', 'au:smith pubdate(-2008)'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 4=4 @attr 1=31 @attr 2=2 "2008"', 'keyword search with pubdate limited to -2008');
-is($QParser->target_syntax('biblioserver', 'au:smith pubdate(2008-)'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 4=4 @attr 1=31 @attr 2=4 "2008"', 'keyword search with pubdate limited to 2008-');
-is($QParser->target_syntax('biblioserver', 'au:smith pubdate(2008)'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 4=4 @attr 1=31 "2008"', 'keyword search with pubdate limited to 2008');
-is($QParser->target_syntax('biblioserver', 'au:smith pubdate(1980,2008)'), '@and @attr 1=1003 @attr 4=6 "smith" @or @attr 4=4 @attr 1=31 "1980" @attr 4=4 @attr 1=31 "2008"', 'keyword search with pubdate limited to 1980, 2008');
-is($QParser->target_syntax('biblioserver', 'au:smith #acqdate_dsc'), '@or @attr 1=32 @attr 7=1 0 @attr 1=1003 @attr 4=6 "smith"', 'keyword search sorted by acqdate descending');
-is($QParser->bib1_mapping_by_attr('field', 'biblioserver', {'1' => '1004'})->{'field'}, 'personal', 'retrieve field by attr');
-is($QParser->bib1_mapping_by_attr_string('field', 'biblioserver', '@attr 1=1004')->{'field'}, 'personal', 'retrieve field by attrstring');
+# Set keyword search as the default
+$QParser->default_search_class('keyword');
+
+my $kwd_search = q/@attr 1=1016 @attr 4=6/;
+my $weight1    = q/@attr 2=102 @attr 9=20 @attr 4=6/;
+my $weight2    = q/@attr 2=102 @attr 9=34 @attr 4=6/;
+
+like( $QParser->target_syntax('biblioserver', 'smith'),
+    qr/\@or \@or $kwd_search "smith" ($weight1 "smith" $weight2 "smith"|$weight2 "smith" $weight1 "smith")/,
+    'super simple keyword query');
+
+is($QParser->target_syntax('biblioserver', 'au:smith'),
+    '@attr 1=1003 @attr 4=6 "smith"', 'simple author query');
+
+is($QParser->target_syntax('biblioserver', 'keyword|publisher:smith'),
+    '@attr 1=1018 @attr 4=6 "smith"', 'fielded publisher query');
+
+is($QParser->target_syntax('biblioserver', 'ti:"little engine that could"'),
+    '@attr 1=4 @attr 4=1 "little engine that could"', 'phrase query');
+
+is($QParser->target_syntax('biblioserver', 'keyword|titlekw:smith'),
+    '@attr 1=4 @attr 2=102 @attr 9=20 @attr 4=6 "smith"',
+    'relevance-bumped query');
+
+is($QParser->target_syntax('biblioserver', 'au:smith && johnson'),
+    '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=1003 @attr 4=6 "johnson"',
+    'query with boolean &&');
+
+is($QParser->target_syntax('biblioserver', 'au:smith && ti:johnson'),
+    '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=4 @attr 4=6 "johnson"', 'query with boolean &&');
+
+is($QParser->target_syntax('biblioserver', 'au:smith pubdate(-2008)'),
+    '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=31 @attr 4=4 @attr 2=2 "2008"',
+    'keyword search with pubdate limited to -2008');
+
+is($QParser->target_syntax('biblioserver', 'au:smith pubdate(2008-)'),
+    '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=31 @attr 4=4 @attr 2=4 "2008"',
+    'keyword search with pubdate limited to 2008-');
+
+is($QParser->target_syntax('biblioserver', 'au:smith pubdate(2008)'),
+    '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=31 @attr 4=4 "2008"',
+    'keyword search with pubdate limited to 2008');
+
+is($QParser->target_syntax('biblioserver', 'au:smith pubdate(1980,2008)'),
+    '@and @attr 1=1003 @attr 4=6 "smith" @or @attr 1=31 @attr 4=4 "1980" @attr 1=31 @attr 4=4 "2008"',
+    'keyword search with pubdate limited to 1980, 2008');
+
+is($QParser->target_syntax('biblioserver', 'au:smith #acqdate_dsc'),
+    '@or @attr 1=32 @attr 7=1 0 @attr 1=1003 @attr 4=6 "smith"',
+    'keyword search sorted by acqdate descending');
+
+is($QParser->bib1_mapping_by_attr('field', 'biblioserver', {'1' => '1004'})->{'field'},
+    'personal', 'retrieve field by attr');
+
+is($QParser->bib1_mapping_by_attr_string('field', 'biblioserver', '@attr 1=1004')->{'field'},
+    'personal', 'retrieve field by attrstring');
 
 is ($QParser->clear_all_mappings, $QParser, 'clear all mappings returns self');
 is ($QParser->clear_all_configuration, $QParser, 'clear all configuration returns self');