From a2132949adcadbfee004f6ccf1a2b1b7acbe9334 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Wed, 24 Oct 2018 11:37:37 -0400 Subject: [PATCH] bib-search indexing; search api wip Signed-off-by: Bill Erickson --- .../eg2/src/app/share/catalog/catalog.service.ts | 4 +- .../eg2/src/app/share/catalog/search-context.ts | 2 +- .../lib/OpenILS/Application/Search/Biblio.pm | 47 ++++++++++++++++++++++ Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm | 15 ++++++- .../src/perlmods/lib/OpenILS/Elastic/BibSearch.pm | 3 +- 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/share/catalog/catalog.service.ts b/Open-ILS/src/eg2/src/app/share/catalog/catalog.service.ts index 1c861fa40c..9af61ef10f 100644 --- a/Open-ILS/src/eg2/src/app/share/catalog/catalog.service.ts +++ b/Open-ILS/src/eg2/src/app/share/catalog/catalog.service.ts @@ -198,6 +198,9 @@ export class CatalogService { ctx.resultIds = []; } + console.debug(`Search found ${result.count} and ` + + `returned ${result.ids.length} record IDs`); + result.ids.forEach((blob, idx) => ctx.addResultId(blob[0], idx)); } @@ -237,7 +240,6 @@ export class CatalogService { } else { idx = ctx.currentResultIds().indexOf(summary.id); } - if (ctx.result.records) { // May be reset when quickly navigating results. ctx.result.records[idx] = summary; diff --git a/Open-ILS/src/eg2/src/app/share/catalog/search-context.ts b/Open-ILS/src/eg2/src/app/share/catalog/search-context.ts index 7ee7ab1271..2d3c9adfc5 100644 --- a/Open-ILS/src/eg2/src/app/share/catalog/search-context.ts +++ b/Open-ILS/src/eg2/src/app/share/catalog/search-context.ts @@ -448,7 +448,7 @@ export class CatalogSearchContext { this.pager.resultCount ); for (let idx = this.pager.offset; idx < max; idx++) { - ids.push(this.resultIds[idx]); + ids.push(Number(this.resultIds[idx])); } return ids; } diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm index 3af4a33d90..98cb416784 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm @@ -12,6 +12,7 @@ use OpenSRF::Utils::Cache; use Encode; use Email::Send; use Email::Simple; +use OpenILS::Elastic::BibSearch; use OpenSRF::Utils::Logger qw/:logger/; @@ -1157,6 +1158,9 @@ sub staged_search { $user_offset = ($user_offset >= 0) ? $user_offset : 0; $user_limit = ($user_limit >= 0) ? $user_limit : 10; + # TODO TODO check settings/db to see if elasticsearch is + # enabled for bib-search. + return elastic_search($search_hash->{query}, $user_offset, $user_limit); # we're grabbing results on a per-superpage basis, which means the # limit and offset should coincide with superpage boundaries @@ -1382,6 +1386,49 @@ sub one_class_one_term { return ($class, $term); } +sub elastic_search { + my ($query, $offset, $limit) = @_; + + # TODO visibility limits on holdings summaries + my ($site) = ($query =~ /site\(([^\)]+)\)/); + $query =~ s/site\(([^\)]+)\)//g; + + my $elastic_query = { + _source => ['id'] , # return only the ID field +# sort => [ +# {'title.raw' => 'asc'}, +# {'author.raw' => 'asc'}, +# '_score' +# ], + size => $limit, + from => $offset, + query => { + bool => { + must => { + query_string => { + default_field => 'keyword', + query => $query + } + } + } + } + }; + + my $es = OpenILS::Elastic::BibSearch->new('main'); + $es->connect; + my $results = $es->search($elastic_query); + + return {count => 0} unless $results; + + return { + count => $results->{hits}->{total}, + ids => [ + map { [$_->{_id}, undef, $_->{_score}] } + grep {defined $_} @{$results->{hits}->{hits}} + ] + }; +} + sub fetch_display_fields { my $self = shift; my $conn = shift; diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm b/Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm index 64312fc157..9892a6db8e 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm @@ -16,11 +16,12 @@ package OpenILS::Elastic; use strict; use warnings; use DBI; +use Time::HiRes qw/time/; use OpenSRF::Utils::Logger qw/:logger/; use OpenSRF::Utils::SettingsClient; use OpenILS::Utils::CStoreEditor qw/:funcs/; use Search::Elasticsearch; -use Data::Dumper; +use OpenSRF::Utils::JSON; sub new { my ($class, $cluster) = @_; @@ -182,20 +183,30 @@ sub search { my ($self, $query) = @_; my $result; + my $duration; + + $logger->info("ES searching " . OpenSRF::Utils::JSON->perl2JSON($query)); eval { + my $start_time = time; $result = $self->es->search( index => $self->index_name, body => $query ); + $duration = time - $start_time; }; if ($@) { $logger->error("ES search failed with $@"); - $logger->error("ES failed query: " . Dumper($query)); return undef; } + $logger->info( + sprintf("ES search found %d results in %f seconds.", + $result->{hits}->{total}, substr($duration, 0, 6) + ) + ); + return $result; } diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Elastic/BibSearch.pm b/Open-ILS/src/perlmods/lib/OpenILS/Elastic/BibSearch.pm index 47cecb96d8..6405a0fbe7 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Elastic/BibSearch.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Elastic/BibSearch.pm @@ -21,9 +21,8 @@ use XML::LibXML; use XML::LibXSLT; use OpenSRF::Utils::Logger qw/:logger/; use OpenILS::Elastic; +use OpenSRF::Utils::JSON; use base qw/OpenILS::Elastic/; -use Data::Dumper; -$Data::Dumper::Indent = 2; my $INDEX_NAME = 'bib-search'; -- 2.11.0