ES gets child init
authorBill Erickson <berickxx@gmail.com>
Tue, 6 Nov 2018 15:20:02 +0000 (10:20 -0500)
committerBill Erickson <berickxx@gmail.com>
Tue, 15 Oct 2019 18:09:56 +0000 (14:09 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Search.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Elastic.pm

index 78d4a4e..bcd3fbf 100644 (file)
@@ -36,6 +36,7 @@ sub initialize {
 sub child_init {
     OpenILS::Application::Search::Z3950->child_init;
     OpenILS::Application::Search::Browse->child_init;
+    OpenILS::Application::Search::Elastic->child_init;
 }
     
 
index ce75bed..a0ac123 100644 (file)
@@ -1157,16 +1157,11 @@ sub staged_search {
     $user_offset = ($user_offset >= 0) ? $user_offset :  0;
     $user_limit  = ($user_limit  >= 0) ? $user_limit  : 10;
 
-    # TODO: cache this.  Real-time lookups are better for testing, though.
-    my $use_elastic = new_editor()->search_elastic_index({
-        active => 't', code => 'bib-search'})->[0];
-
-    if ($use_elastic) {
-        return OpenILS::Application::Search::Elastic->bib_search(
-            $search_hash->{query}, ($method =~ /staff/ ? 1 : 0),
-            $user_offset, $user_limit
-        );
-    }
+    return OpenILS::Application::Search::Elastic->bib_search(
+        $search_hash->{query}, # query string
+        ($method =~ /staff/ ? 1 : 0),
+        $user_offset, $user_limit
+    ) if OpenILS::Application::Search::Elastic->is_enabled;
 
     # we're grabbing results on a per-superpage basis, which means the 
     # limit and offset should coincide with superpage boundaries
index e311658..bb6caa1 100644 (file)
@@ -39,9 +39,31 @@ my $bib_fields;
 my $hidden_copy_statuses;
 my $hidden_copy_locations;
 my $avail_copy_statuses;
+our $enabled = undef;
 
-sub init_data {
-    return if $bib_fields;
+# Returns true if the Elasticsearch 'bib-search' index is active.
+sub is_enabled {
+
+    return $enabled if defined $enabled;
+
+    # Elastic bib search is enabled if a "bib-search" index is enabled.
+    my $index = new_editor()->search_elastic_index(
+        {active => 't', code => 'bib-search'})->[0];
+
+    if ($index) {
+
+        $logger->info("ES bib-search index is enabled");
+        $enabled = 1;
+    } else {
+        $enabled = 0;
+    }
+
+    return $enabled;
+}
+
+sub child_init {
+    my $class = shift;
+    return unless $class->is_enabled();
 
     my $e = new_editor();
 
@@ -70,6 +92,8 @@ sub init_data {
     });
 
     $hidden_copy_locations = [map {$_->{id}} @$locs];
+
+    return 1;
 }
 
 # Translate a bib search API call into something consumable by Elasticsearch
@@ -80,8 +104,6 @@ sub bib_search {
 
     $logger->info("ES parsing API query $query staff=$staff");
 
-    init_data();
-
     my ($elastic_query, $cache_key) = 
         compile_elastic_query($query, $staff, $offset, $limit);