From: Bill Erickson Date: Wed, 24 Oct 2018 15:37:37 +0000 (-0400) Subject: bib-search indexing; search api wip X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=b27c510be8ff5e2e6d25a8102f31ce41dc5fbd65;p=working%2FEvergreen.git bib-search indexing; search api wip Signed-off-by: Bill Erickson --- 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 2aaaf1f3ae..bf6036ced6 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 @@ -176,6 +176,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)); } @@ -207,7 +210,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 041d710a4b..7dbe6f8d86 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 @@ -429,7 +429,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 159ecd78b9..5f3846a0bd 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm @@ -10,6 +10,7 @@ use OpenSRF::Utils::SettingsClient; use OpenILS::Utils::CStoreEditor q/:funcs/; use OpenSRF::Utils::Cache; use Encode; +use OpenILS::Elastic::BibSearch; use OpenSRF::Utils::Logger qw/:logger/; @@ -1156,6 +1157,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 @@ -1306,6 +1310,51 @@ sub staged_search { return cache_facets($facet_key, $new_ids, $IAmMetabib, $ignore_facet_classes) if $docache; } + +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';