From feb4519e31697edaa92cd9859730912be8b99c77 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Wed, 27 Oct 2021 13:20:26 -0400 Subject: [PATCH] LP1844418 ES rebasing Signed-off-by: Bill Erickson --- .../eg2/src/app/share/catalog/catalog.service.ts | 118 ++++++++++----------- 1 file changed, 56 insertions(+), 62 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 670e10c4fd..1878e5d4e4 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 @@ -1,6 +1,6 @@ import {Injectable, EventEmitter} from '@angular/core'; import {Observable} from 'rxjs'; -import {map, tap, finalize, merge} from 'rxjs/operators'; +import {map, tap, finalize} from 'rxjs/operators'; import {OrgService} from '@eg/core/org.service'; import {UnapiService} from '@eg/share/catalog/unapi.service'; import {IdlService, IdlObject} from '@eg/core/idl.service'; @@ -12,11 +12,6 @@ import {BasketService} from './basket.service'; import {CATALOG_CCVM_FILTERS} from './search-context'; import {ElasticService} from './elastic.service'; -// Start with this number of records in the first batch so results -// can start displaying sooner. A batch of 5 gets a decent chunk of -// data on the page while waiting for the other results to appear. -const INITIAL_REC_BATCH_SIZE = 5; - @Injectable() export class CatalogService { @@ -40,10 +35,11 @@ export class CatalogService { private unapi: UnapiService, private pcrud: PcrudService, private bibService: BibRecordService, - private basket: BasketService, - private elastic: ElasticService + private elastic: ElasticService, + private basket: BasketService ) { this.onSearchComplete = new EventEmitter(); + } search(ctx: CatalogSearchContext): Promise { @@ -131,7 +127,6 @@ export class CatalogService { } marcSearch(ctx: CatalogSearchContext): Promise { - let method = 'open-ils.search.biblio.marc'; if (ctx.isStaff) { method += '.staff'; } @@ -214,9 +209,6 @@ 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)); } @@ -229,18 +221,7 @@ export class CatalogService { const isMeta = ctx.termSearch.isMetarecordSearch(); - // When fetching batches of search results, fetch the first - // few records first so results lists can start rendering - // before the full data set has arrived and been processed. - let ids1 = ctx.currentResultIds(); - let ids2 = []; - if (ids1.length > INITIAL_REC_BATCH_SIZE) { - ids1 = ctx.currentResultIds().slice(0, INITIAL_REC_BATCH_SIZE); - ids2 = ctx.currentResultIds().slice(INITIAL_REC_BATCH_SIZE); - } - let observable: Observable; - const bibFunc = isMeta ? 'getMetabibSummary' : 'getBibSummary'; const options: any = {pref_ou: ctx.prefOu}; @@ -267,6 +248,7 @@ 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; @@ -350,10 +332,17 @@ export class CatalogService { return Promise.resolve(); } - if (ctx.result.facets) { - // No need to fetch pre-compiled facets - console.debug('Showing pre-compiled facets'); - ctx.result.facetData = this.formatFacets(ctx.result.facets); + if (this.elastic.enabled && ctx.result.facets) { + // MARC searches also return facets, but we don't yet support + // using facets as filters for MARC searches. It's certainly + // possible with ES, but it would require quite a few changes + // to stock files, which I'm hoping to avoid as much as possible. + // Only collect facets on 'term' searches for now so they + // otherwise remain hidden. + if (ctx.termSearch.isSearchable()) { + ctx.result.facetData = + this.elastic.formatFacets(ctx.result.facets); + } return Promise.resolve(); } @@ -370,41 +359,6 @@ export class CatalogService { }); } - formatFacets(facets: any) { - const facetData = {}; - Object.keys(facets).forEach(cmfId => { - const facetHash = facets[cmfId]; - const cmf = this.cmfMap[cmfId]; - - const cmfData = []; - Object.keys(facetHash).forEach(value => { - const count = facetHash[value]; - cmfData.push({value : value, count : count}); - }); - - if (!facetData[cmf.field_class()]) { - facetData[cmf.field_class()] = {}; - } - - facetData[cmf.field_class()][cmf.name()] = { - cmfLabel : cmf.label(), - valueList : cmfData.sort((a, b) => { - if (a.count > b.count) { return -1; } - if (a.count < b.count) { return 1; } - // secondary alpha sort on display value - return a.value < b.value ? -1 : 1; - }) - }; - }); - - return facetData; - } - - checkSearchEngine(): Promise { - return this.pcrud.retrieve('cgf', 'elastic.bib_search.enabled') - .toPromise().then(flag => this.elastic.enabled = flag.enabled() === 't'); - } - fetchCcvms(): Promise { if (Object.keys(this.ccvmMap).length) { @@ -521,4 +475,44 @@ export class CatalogService { cbs.value, ctx.searchOrg.shortname(), cbs.limit, cbs.offset ).pipe(tap(result => ctx.searchState = CatalogSearchState.COMPLETE)); } + + formatFacets(facets: any) { + const facetData = {}; + Object.keys(facets).forEach(cmfId => { + const facetHash = facets[cmfId]; + const cmf = this.cmfMap[cmfId]; + + const cmfData = []; + Object.keys(facetHash).forEach(value => { + const count = facetHash[value]; + cmfData.push({value : value, count : count}); + }); + + if (!facetData[cmf.field_class()]) { + facetData[cmf.field_class()] = {}; + } + + facetData[cmf.field_class()][cmf.name()] = { + cmfLabel : cmf.label(), + valueList : cmfData.sort((a, b) => { + if (a.count > b.count) { return -1; } + if (a.count < b.count) { return 1; } + // secondary alpha sort on display value + return a.value < b.value ? -1 : 1; + }) + }; + }); + + return facetData; + } + + checkSearchEngine(): Promise { + return this.pcrud.retrieve('cgf', 'elastic.bib_search.enabled') + .toPromise().then(flag => { + if (flag && flag.enabled() === 't') { + this.elastic.enabled = true; + return this.elastic.init(); + } + }); + } } -- 2.11.0