bib-search indexing; search api wip
authorBill Erickson <berickxx@gmail.com>
Wed, 24 Oct 2018 15:37:37 +0000 (11:37 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 8 Nov 2018 19:01:13 +0000 (14:01 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/share/catalog/catalog.service.ts
Open-ILS/src/eg2/src/app/share/catalog/search-context.ts
Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm
Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm
Open-ILS/src/perlmods/lib/OpenILS/Elastic/BibSearch.pm

index 95967cb..2bc681f 100644 (file)
@@ -84,6 +84,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));
     }
 
@@ -99,6 +102,7 @@ export class CatalogService {
             ctx.currentResultIds(), ctx.searchOrg.id(), depth)
         .pipe(map(summary => {
             // Responses are not necessarily returned in request-ID order.
+            summary.record.id(Number(summary.record.id())); // ensure int
             const idx = ctx.currentResultIds().indexOf(summary.record.id());
             if (ctx.result.records) {
                 // May be reset when quickly navigating results.
index e4e64b2..8b1a727 100644 (file)
@@ -73,7 +73,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;
     }
index 159ecd7..5f3846a 100644 (file)
@@ -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;
index 64312fc..9892a6d 100644 (file)
@@ -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;
 }
 
index 47cecb9..6405a0f 100644 (file)
@@ -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';