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';
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 {
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<CatalogSearchContext>();
+
}
search(ctx: CatalogSearchContext): Promise<void> {
}
marcSearch(ctx: CatalogSearchContext): Promise<void> {
-
let method = 'open-ils.search.biblio.marc';
if (ctx.isStaff) { method += '.staff'; }
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));
}
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<BibRecordSummary>;
- const bibFunc = isMeta ? 'getMetabibSummary' : 'getBibSummary';
const options: any = {pref_ou: ctx.prefOu};
} else {
idx = ctx.currentResultIds().indexOf(summary.id);
}
+
if (ctx.result.records) {
// May be reset when quickly navigating results.
ctx.result.records[idx] = summary;
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();
}
});
}
- 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<any> {
- return this.pcrud.retrieve('cgf', 'elastic.bib_search.enabled')
- .toPromise().then(flag => this.elastic.enabled = flag.enabled() === 't');
- }
-
fetchCcvms(): Promise<void> {
if (Object.keys(this.ccvmMap).length) {
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<any> {
+ 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();
+ }
+ });
+ }
}