Skip Menu |

This queue is for tickets about the Search-Query CPAN distribution.

Report information
The Basics
Id: 77243
Status: resolved
Priority: 0/
Queue: Search-Query

People
Owner: Nobody in particular
Requestors: dapatrick [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.19
Fixed in: (no value)



Subject: Patch to allow term_expander to receive field name when available
Hi Peter, I'm really enjoying using this module. It's working out well for my purposes. However, it would be nice if term_expander also received the field name when it is available. I've created a patch, including tests and pod updates. What do you think of adding this feature? It's actually unstuck me in my current project and I think it might be valuable for others as well. Warm regards, Darian
Subject: term_expander_with_field.patch
diff -u -r Search-Query-0.19/lib/Search/Query/Parser.pm Search-Query-0.19-patched/lib/Search/Query/Parser.pm --- Search-Query-0.19/lib/Search/Query/Parser.pm 2011-06-09 23:18:52.000000000 -0400 +++ Search-Query-0.19-patched/lib/Search/Query/Parser.pm 2012-05-16 13:19:15.209218838 -0400 @@ -352,7 +352,7 @@ my $parser = Search::Query->parser( term_expander => sub { - my ($term) = @_; + my ($term, $field) = @_; return ($term) if ref $term; # skip ranges return ( qw( one two three ), $term ); } @@ -361,8 +361,8 @@ my $query = $parser->parse("foo=bar") print "$query\n"; # +foo=(one OR two OR three OR bar) -The term_expander reference should expect one argument (the term value) -and should return an array of values. +The term_expander reference should expect two arguments: the term value +and, if available, the term field name. It should return an array of values. The term_expander reference is called internally during the parse() method, B<before> any field alias expansion or validation is performed. @@ -668,7 +668,7 @@ return; } - my @newterms = $expander->( $clause->value ); + my @newterms = $expander->( $clause->value, $clause->field ); if ( ref $newterms[0] and ref $clause->value ) { $clause->value( $newterms[0] ); } diff -u -r Search-Query-0.19/t/07-term-expander.t Search-Query-0.19-patched/t/07-term-expander.t --- Search-Query-0.19/t/07-term-expander.t 2010-06-26 23:01:49.000000000 -0400 +++ Search-Query-0.19-patched/t/07-term-expander.t 2012-05-16 13:14:01.324503237 -0400 @@ -2,9 +2,9 @@ use strict; use warnings; use Search::Query; -use Test::More tests => 3; +use Test::More tests => 5; -ok( my $parser = Search::Query->parser( +ok( my $parser1 = Search::Query->parser( term_expander => sub { my ($term) = @_; return ( qw( one two three ), $term ); @@ -13,7 +13,24 @@ "new parser with term_expander" ); -ok( my $query = $parser->parse("foo=bar"), "parse foo=bar" ); -my $expect = qq/+(foo=one foo=two foo=three foo=bar)/; -is( "$query", $expect, "query expanded" ); +ok( my $query1 = $parser1->parse("foo=bar"), "parse foo=bar" ); +my $expect1 = qq/+(foo=one foo=two foo=three foo=bar)/; +is( "$query1", $expect1, "query expanded" ); +my $parser2 = Search::Query->parser( + term_expander => sub { + my ($term, $field) = @_; + if ($field) { + return "$term-$field"; + } + else { + return "$term"; + } + } +); + +my $query2 = $parser2->parse('foo=bar'); +is( "$query2", qq/+foo=bar-foo/, "query expanded, with field passed" ); + +my $query3 = $parser2->parse('bar'); +is( "$query3", qq/+bar/, "query expanded, with field passed" );
Glad to hear this is useful to you. Thanks for the patch. Applied and uploaded as 0.20.