Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed partial fix for rtcpan 96977 and feature requests from last year. #26

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 28 additions & 4 deletions lib/WebService/Solr/Query.pm
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package WebService::Solr::Query;

use Any::Moose;
use Moo;

use overload q("") => 'stringify';

my $escape_chars = quotemeta( '+-&|!(){}[]^"~*?:\\' );

has 'query' => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
has 'query' => ( is => 'ro', default => sub { [] } );


use constant D => 0;

Expand All @@ -20,6 +21,17 @@ sub BUILDARGS {
return { query => \@_ };
}

sub select_all {
my $self = shift;
return $self->new( { '*' => \'*' } );
}

sub geofilter {
my $self = shift;
my( $coordinate, $sfield, $distancekm ) = @_;
return qq|\{!geofilt pt=$coordinate sfield=$sfield d=$distancekm\}|;
}

sub stringify {
my $self = shift;

Expand Down Expand Up @@ -276,8 +288,6 @@ sub __dumper {
return Data::Dumper::Dumper( @_ );
}

no Any::Moose;

__PACKAGE__->meta->make_immutable;

1;
Expand Down Expand Up @@ -421,6 +431,20 @@ B<NB:> Values sent to C<new()> are automatically escaped for you.

Unescapes values escaped in C<escape()>.

=head2 select_all

A shortcut to generate a query for selecting all records (*:*).

=head2 geofilter

Generate a geofilter for use in queries. Arguments are a Latitude-Longitude Coordinate in the form 40.76,-73.98,
the name of the field in the collection that holds geo-data and a number of kilometers.

my $fq = WebService::Solr::Query->geofilter( $coordinate, $geofield, $distance_in_km );


}

=head2 D

Debugging constant, default: off.
Expand Down
19 changes: 18 additions & 1 deletion t/query.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Test::More tests => 7;
use Test::More tests => 9;

use strict;
use warnings;
Expand All @@ -15,6 +15,23 @@ subtest 'Unescapes' => sub {
'(1+1):2', 'unescape' );
};

subtest 'Select All' => sub {
my $string_select_all_via_new = sprintf( "%s", WebService::Solr::Query->new( { '*' => \'*' } ) );
is( $string_select_all_via_new, '(*:*)', q|selecting all via ->new( { '*' => \'*' } ) stringifies like this (*:*)| );
my $string_select_all = sprintf( "%s", WebService::Solr::Query->select_all() ) ;
is( $string_select_all_via_new, $string_select_all_via_new, 'the select_all method stringifies the same' );
is_deeply( WebService::Solr::Query->new( { '*' => \'*' } ),
WebService::Solr::Query->select_all(),
'select_all: The objects returned by both methods are identical' );
};

subtest 'GeoFilter' => sub {
my $carnegiehall = '40.76,-73.98' ;
my $carnegie400 = WebService::Solr::Query->geofilter( $carnegiehall, 'store', 400 );
is( $carnegie400, '{!geofilt pt=40.76,-73.98 sfield=store d=400}',
'Returned string for filter query for store within 400 km of Carnegie Hall' ) ;
};

subtest 'Basic queries' => sub {
# default field
_check( query => { -default => 'space' }, expect => '("space")' );
Expand Down