From: erickson Date: Wed, 14 Nov 2007 23:31:58 +0000 (+0000) Subject: Added a new method to parse complex search query syntax for multiclass bib and metabi... X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=6a935e4a0ceb3bb5a71414f8fd9077e0d929bd4c;p=Evergreen.git Added a new method to parse complex search query syntax for multiclass bib and metabib searches E.g. "title:crime author:dostoyevsky lang:rus" This will allow us to encode multiclass searches as human-readable search strings TODO: more testing git-svn-id: svn://svn.open-ils.org/ILS/trunk@8063 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm b/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm index 3daa5f4545..a9e9bd986b 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm @@ -9,6 +9,7 @@ use OpenILS::Utils::ModsParser; use OpenSRF::Utils::SettingsClient; use OpenILS::Utils::CStoreEditor q/:funcs/; use OpenSRF::Utils::Cache; +use Encode; use OpenSRF::Utils::Logger qw/:logger/; @@ -405,6 +406,115 @@ sub biblio_copy_to_mods { } +__PACKAGE__->register_method( + api_name => 'open-ils.search.biblio.multiclass.query', + method => 'multiclass_query', + signature => q# + @param arghash @see open-ils.search.biblio.multiclass + @param query Raw human-readable query string. + Recognized search keys include: + keyword/kw - search keyword(s) + author/au/name - search author(s) + title/ti - search title + subject/su - search subject + series/se - search series + lang - limit by language (specifiy multiple langs with lang:l1 lang:l2 ...) + site - search at specified org unit, corresponds to actor.org_unit.shortname + sort - sort type (title, author, pubdate) + dir - sort direction (asc, desc) + available - if set to anything other than "false" or "0", limits to available items + + keyword, title, author, subject, and series support additional search + subclasses, specified with a "|". For example, "title|proper:gone with the wind" + For more, see config.metabib_field + + @param nocache @see open-ils.search.biblio.multiclass + # +); +__PACKAGE__->register_method( + api_name => 'open-ils.search.biblio.multiclass.query.staff', + method => 'multiclass_query', + signature => '@see open-ils.search.biblio.multiclass.query'); +__PACKAGE__->register_method( + api_name => 'open-ils.search.metabib.multiclass.query', + method => 'multiclass_query', + signature => '@see open-ils.search.biblio.multiclass.query'); +__PACKAGE__->register_method( + api_name => 'open-ils.search.metabib.multiclass.query.staff', + method => 'multiclass_query', + signature => '@see open-ils.search.biblio.multiclass.query'); + +sub multiclass_query { + my($self, $conn, $arghash, $query, $docache) = @_; + + $logger->debug("initial search query => $query"); + + $query = decode_utf8($query); + $query =~ s/\+/ /go; + $query =~ s/'//go; + $query =~ s/^\s+//go; + + # convert convenience classes (e.g. kw for keyword) to the full class name + $query =~ s/kw(:|\|)/keyword$1/go; + $query =~ s/ti(:|\|)/title$1/go; + $query =~ s/au(:|\|)/author$1/go; + $query =~ s/su(:|\|)/subject$1/go; + $query =~ s/se(:|\|)/series$1/go; + $query =~ s/name(:|\|)/author$1/og; + + $logger->debug("cleansed query string => $query"); + + my %search; + + while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):[^:]+)$//so) { + my($type, $value) = split(':', $1); + next unless $type and $value; + + $value =~ s/^\s*//og; + $value =~ s/\s*$//og; + $type = 'sort_dir' if $type eq 'dir'; + + if($type eq 'site') { + # 'site' is the org shortname. when using this, we also want + # to search at the requested org's depth + my $e = new_editor(); + if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) { + $arghash->{org_unit} = $org->id if $org; + $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth; + } else { + $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring"); + } + + } elsif($type eq 'available') { + # limit to available + $arghash->{available} = 1 unless $value eq 'false' or $value eq '0'; + + } elsif($type eq 'lang') { + # collect languages into an array of languages + $arghash->{language} = [] unless $arghash->{language}; + push(@{$arghash->{language}}, $value); + + } else { + # append the search term to the term under construction + $search{$type} = {} unless $search{$type}; + $search{$type}->{term} = + ($search{$type}->{term}) ? $search{$type}->{term} . " $value" : $value; + } + } + $arghash->{searches} = \%search; + + # capture the original limit because the search method alters the limit internally + my $ol = $arghash->{limit}; + + (my $method = $self->api_name) =~ s/\.query//o; + $method = $self->method_lookup($method); + my ($data) = $method->run($arghash, $docache); + + $arghash->{limit} = $ol if $ol; + $data->{compiled_search} = $arghash; + return $data; +} + # ---------------------------------------------------------------------------- # These are the main OPAC search methods # ----------------------------------------------------------------------------