WebSer...::Query(3pm)

πŸ“¦
version 0.44 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– WebSer...::Query(3pm) πŸ’–

okay bestie, so this is WebService::Solr::Query from the User Contributed Perl Documentation, and it's about to make your Solr life so much easier, no cap ✨


🌸 NAME 🌸

WebService::Solr::Query β€” the abstract query syntax for Solr queries πŸ¦‹


πŸŽ€ SYNOPSIS πŸŽ€

my $query  = WebService::Solr::Query->new( { foo => 'bar' } );
my $result = $solr->search( $query );

like. that's it. that's the whole vibe πŸ’…


✨ DESCRIPTION ✨

so WebService::Solr::Query gives you a programmatic way to build queries that get sent off to Solr, which is lowkey the best way to do it because hand-writing query strings is giving chaos πŸ™ˆ

syntax-wise, it tries so hard to stay as close to SQL::Abstract WHERE clauses as it possibly can, with the obvious exceptions for idioms that just... don't exist in SQL πŸ’€

and here's the part you need to know: just like values in SQL::Abstract get SQL-escaped, this module does the appropriate Solr-escaping on all values you pass to the object. see escape(). it's doing the work so you don't have to 🫢


🌈 QUERY SYNTAX 🌈

πŸ“ Key-Value Pairs

the simplest way to search. iconic. the classic.

my $q = WebService::Solr::Query->new( { foo => 'bar' } );
# RESULT: (foo:"bar")

πŸ’œ Implicit AND and OR

by default, data you hand over as a HASHREF gets AND'ed together. it's automatic, bestie.

my $q = WebService::Solr::Query->new( { foo => 'bar', baz => 'quux' } );
# RESULT: (foo:"bar" AND baz:"quux")

and furthermore (yes we're going there), data you hand over as an ARRAYREF gets OR'ed together:

my $q = WebService::Solr::Query->new( { foo => [ 'bar', 'baz' ] } );
# RESULT: (foo:"bar" OR foo:"baz")

πŸ¦‹ Nested AND and OR

being able to nest AND and OR boolean operators is essential to express complex queries. essential! non-negotiable! and that's literally why the -and and -or prefixes exist πŸ’–

my $q = WebService::Solr::Query->new( { foo => [
    -and => { -prohibit => 'bar' }, { -require => 'baz' }
] } );
# RESULT: (((-foo:"bar") AND (+foo:"baz")))
my $q = WebService::Solr::Query->new( { foo => [
    -or => { -require => 'bar' }, { -prohibit => 'baz' }
] } );
# RESULT: (((+foo:"bar") OR (-foo:"baz")))

🌟 Default Field

wanna search the default field? use the -default prefix, easy peasy πŸŽ€

my $q = WebService::Solr::Query->new( { -default => 'bar' } );
# RESULT: ("bar")

πŸ’€ Require/Prohibit

my $q = WebService::Solr::Query->new( { foo => { -require => 'bar' } } );
# RESULT: (+foo:"bar")
my $q = WebService::Solr::Query->new( { foo => { -prohibit => 'bar' } } );
# RESULT: (-foo:"bar")

πŸ”₯ Range

there are two types of range queries bestie: inclusive (-range_inc) and exclusive (-range_exc). and plot twist, the -range prefix can be used in place of -range_inc because we're flexible like that πŸ’…

my $q = WebService::Solr::Query->new( { foo => { -range => ['a', 'z'] } } );
# RESULT: (+foo:[a TO z])
my $q = WebService::Solr::Query->new( { foo => { -range_exc => ['a', 'z'] } } );
# RESULT: (+foo:{a TO z})

⭐ Boost

my $q = WebService::Solr::Query->new( { foo => { -boost => [ 'bar', '2.0' ] } } );
# RESULT: (foo:"bar"^2.0)

🧚 Proximity

my $q = WebService::Solr::Query->new( { foo => { -proximity => [ 'bar baz', 10 ] } } );
# RESULT: (foo:"bar baz"~10)

🫢 Fuzzy

my $q = WebService::Solr::Query->new( { foo => { -fuzzy => [ 'bar', '0.8' ] } } );
# RESULT: (foo:bar~0.8)

πŸ‘‘ Literal Queries

okay so if you specify a scalar ref as a value in a key-value pair, that lets arbitrary queries get sent across the line. 🚨

NB: this will bypass any data massaging done on regular strings β€” so the onus of properly escaping the data is left to you. yes, you. the main character. no pressure πŸ™ˆ

my $q = WebService::Solr::Query->new( { '*' => \'*' } )
# RESULT (*:*)

πŸ’Ž ACCESSORS πŸ’Ž

  • query β€” stores the original query structure ✨

🌸 METHODS 🌸

πŸ¦‹ new( \%query )

creates a new query object with the given hashref. brand new baby query πŸŽ€

πŸ’– stringify( )

converts the supplied structure into a Solr/Lucene query. it's giving translation services.

πŸ”₯ escape( $value )

the following values must be escaped in a search value:

+ - & | ! ( ) { } [ ] ^ " ~ * ? : \

NB: values sent to new() are automatically escaped for you. we got you 🫢

πŸ’… unescape( $value )

unescapes values escaped in escape(). the reverse moment.

⭐ D

debugging constant, default: off.

🌈 BUILDARGS

Moo method to handle input to new().


πŸ‘€ SEE ALSO πŸ‘€


πŸ’œ AUTHORS πŸ’œ

  • Andy Lester andy@petdance.com
  • Brian Cassidy <bricas@cpan.org>
  • Jos Boumans <kane@cpan.org>

Copyright 2008-2014 National Adult Literacy Database Copyright 2015-2020 Andy Lester

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page bestie, go forth and build queries that eat πŸ’…βœ¨

signing off from the world of perl v5.36.0, dated 2022-10-16, WebSer...::Query(3pm) β€” ily, go slay πŸ¦‹πŸ’–

πŸ“¦ where this page comes from