import {Injectable} from '@angular/core'; import {Http, Response} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {AutoCompleteValue} from '../searchPages/searchUtils/searchHelperClasses.class'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/share'; import {RefineResultsUtils} from './servicesUtils/refineResults.class'; import{EnvProperties} from '../utils/properties/env-properties'; @Injectable() export class RefineFieldResultsService { constructor(private http: Http ) {} getRefineFieldsResultsByEntityName(fields:string[], entityName:string, properties:EnvProperties):any{ let url = properties.searchAPIURLLAst + this.getSearchAPIURLForEntity(entityName)+"?format=json&refine=true&page=1&size=0"; for(var i=0; i < fields.length; i++){ url += "&fields="+fields[i]; } return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .map(res => res.json()) .map(res => [res['meta'].total, RefineResultsUtils.parse(res['refineResults'],fields, entityName)]); } getRefineFieldResultsByFieldName(fieldName:string, entityName:string, properties:EnvProperties):any{ let link = properties.searchAPIURLLAst +this.getSearchAPIURLForEntity(entityName)+"?fields="+fieldName + "&format=json"; return this.getField(link,fieldName, properties); } getField (link:string,fieldName:string, properties:EnvProperties):any{ let url = link+"&refine=true&page=1&size=0"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .map(res => res.json()) .map(res => res['refineResults']) .map(res => this.parse(res,fieldName)); } parse(data: any,fieldName:string):any { var values:AutoCompleteValue[] = []; if(data){ let field = data[fieldName]; for(let i=0; i