alfresco-ng2-components/lib/content-services/search/search-query-builder.service.ts
Denys Vuika ed48994e67 [ADF-2128] facet container component (#3094)
* (wip) facet container

* shaping out the API

* code lint fixes

* radiobox facet example

* fields selector facet

* search limits support

* scope locations facet example

* move custom search to 'search.query' config

* use facet fields and queries from the config file

* use facet filters

* use facet buckets in query

* preserve expanded/checked states

* code cleanup and binding fixes

* fix apis after rebase

* extract query builder into separate class

* code improvements

* full chip list (merge facet fields with queries)

* placeholder for range requests

* move search infrastructure to ADF core

* cleanup code

* auto-search on init

* move search components to the content services

* selected facets chip list

* split into separate components at ADF level

* move the rest of the implementation to ADF

* facet builder fixes and tests

* translation support for category names

* docs placeholders

* update language level

* unit tests and packaging updates

* fix after rebase

* remove fdescribe

* some docs on search settings

* rename components as per review

* simplify chip list as per review

* turn query builder into service

* improve search service, integrate old search results

* fix node selector integration

* move service to the top module

* update tests

* remove fdescribe

* update tests

* test fixes

* test fixes

* test updates

* fix tests

* code and test fixes

* remove fit

* fix tests

* fix tests

* remove obsolete test

* increase bundle threshold

* update docs to reflect PR changes

* fix docs
2018-03-29 11:34:08 +01:00

144 lines
4.4 KiB
TypeScript

/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { AlfrescoApiService, AppConfigService } from '@alfresco/adf-core';
import { QueryBody } from 'alfresco-js-api';
import { SearchCategory } from './search-category.interface';
import { FilterQuery } from './filter-query.interface';
import { SearchRange } from './search-range.interface';
import { SearchConfiguration } from './search-configuration.interface';
import { FacetQuery } from './facet-query.interface';
@Injectable()
export class SearchQueryBuilderService {
updated: Subject<QueryBody> = new Subject();
executed: Subject<any> = new Subject();
categories: Array<SearchCategory> = [];
queryFragments: { [id: string]: string } = {};
fields: { [id: string]: string[] } = {};
scope: { locations?: string };
filterQueries: FilterQuery[] = [];
ranges: { [id: string]: SearchRange } = {};
config: SearchConfiguration;
constructor(appConfig: AppConfigService, private api: AlfrescoApiService) {
this.config = appConfig.get<SearchConfiguration>('search');
if (!this.config) {
throw new Error('Search configuration not found.');
}
if (this.config.query && this.config.query.categories) {
this.categories = this.config.query.categories.filter(f => f.enabled);
}
this.filterQueries = this.config.filterQueries || [];
this.scope = {
locations: null
};
}
addFilterQuery(query: string): void {
if (query) {
const existing = this.filterQueries.find(q => q.query === query);
if (!existing) {
this.filterQueries.push({ query: query });
}
}
}
removeFilterQuery(query: string): void {
if (query) {
this.filterQueries = this.filterQueries.filter(f => f.query !== query);
}
}
getFacetQuery(label: string): FacetQuery {
if (label) {
const queries = this.config.facetQueries || [];
return queries.find(q => q.label === label);
}
return null;
}
update(): void {
const query = this.buildQuery();
this.updated.next(query);
}
async execute() {
const query = this.buildQuery();
const data = await this.api.searchApi.search(query);
this.executed.next(data);
}
buildQuery(): QueryBody {
let query = '';
const fields: string[] = [];
this.categories.forEach(facet => {
const customQuery = this.queryFragments[facet.id];
if (customQuery) {
if (query.length > 0) {
query += ' AND ';
}
query += `(${customQuery})`;
}
const customFields = this.fields[facet.id];
if (customFields && customFields.length > 0) {
for (const field of customFields) {
if (!fields.includes(field)) {
fields.push(field);
}
}
}
});
if (query) {
const result: QueryBody = {
query: {
query: query,
language: 'afts'
},
include: ['path', 'allowableOperations'],
fields: fields,
/*
paging: {
maxItems: maxResults,
skipCount: skipCount
},
*/
filterQueries: this.filterQueries,
facetQueries: this.config.facetQueries,
facetFields: this.config.facetFields,
limits: this.config.limits,
scope: this.scope
};
return result;
}
return null;
}
}