cluedin-widget
Version:
This project contains all the pages needed for browsing entities and searching them. The aim is to replace the CluedIn.Webapp project with this one when all the pages ( including the Admin page ) will be ported to REACT.
91 lines (67 loc) • 1.84 kB
JavaScript
/* eslint no-underscore-dangle: ["error", { "allow": ["__token"] }]*/
import { apiRequest } from '../helpers/request';
import entityAPI from '../../iso/entity';
export function search(q, options) {
const { order, filters, page, aggregation, type, } = options;
let url = 'api/search?q=';
let filterUrl = '';
if (aggregation) {
url = `api/v1/entitysearch?id=${aggregation}`;
if (type) {
url += `&type=${type}`;
}
url += `&q=`;
}
if (filters && filters.length > 0) {
filters.forEach((f) => {
if (filterUrl) {
filterUrl += ' AND ';
}
filterUrl += `${f.filterType}:${(f.facet.term || f.facet.time)}`;
});
filterUrl = filterUrl.replace('entitytype', 'entityType');
}
url += q;
if (filterUrl) {
url += `&type=${filterUrl}`;
}
if (page) {
url += `&page=${page}`;
}
if (order && order !== 'relevance') {
let direction = '';
if (order === 'new') {
direction = 'desc';
}
if (order === 'old') {
direction = 'asc';
}
url += `&dateSort=true&direction=${direction}`;
}
return apiRequest('GET', url).then((resp) => {
const result = resp.body;
if (result.Hits) {
result.Hits = result.Hits.map((entity) => (
entityAPI.toVM(entity, false, resp.__token)
));
}
return result;
});
}
export function suggestedSearch(q) {
let url = 'api/search/suggest?q=';
url += q;
return apiRequest('GET', url).then((resp) => (resp.body));
}
export function aggregationSearch(id) {
let url = `api/v1/entitysearch?id=${id}`;
return apiRequest('GET', url).then((resp)=> {
const result = resp.body;
if (result.Hits) {
result.Hits = result.Hits.map((entity) => (
entityAPI.toVM(entity, false, resp.__token)
));
}
return result;
});
}