Commit 8cc26ed9 authored by ext.sopra.ncastejon's avatar ext.sopra.ncastejon
Browse files

Add documentation to the ES service.

parent 810e3f10
Pipeline #4415 passed with stage
in 6 seconds
......@@ -8,6 +8,11 @@ import { APP_CONFIG } from '../../core/services/app-config.service';
import { scopesResearch } from '../../shared/variables';
import { Aggregation, ElasticsearchOptions, Filter, IElasticsearchResponse, IPostsESOptions } from '../models';
/*
This service is in charge to get the content from Elasticsearch. This includes
all the metadata and data from datasets
*/
@Injectable()
export class ElasticsearchService {
......@@ -23,10 +28,9 @@ export class ElasticsearchService {
}
/**
* This is the main elasticsearch (ES) request to get information from ES indexes
* We have one ES options as parameter used for 2 purposes:
* - to construct the request options: research, sorting, paginations, highlight, etc...
* - to know in which indexes we will search: datasets, articles or both
* This is the main elasticsearch (ES) request to get information from ES
* We have one ES options as parameter used for multiple purposes:
* - research, sorting, paginations, highlight, etc...
*/
getAllEntities(options: ElasticsearchOptions): Observable<IElasticsearchResponse> {
const requestOptions = this.constructElasticsearchRequest(options);
......@@ -43,6 +47,7 @@ export class ElasticsearchService {
);
}
// Get the metadata. For this reason we add the 'metadata-fr' source where all the metadata content is.
getDatasetMetadata(slugOrUuid: string): Observable<IElasticsearchResponse> {
return this._http.request<IElasticsearchResponse>('POST', this.elasticSearchUrl, {
withCredentials: true,
......@@ -76,6 +81,8 @@ export class ElasticsearchService {
);
}
// Each dataset have one unique slug and uuid. This method get the slugs
// from a list of uuids. (this is useful for routing purpose for example)
getSlugsFromUuid(uuids: string[]): Observable<IElasticsearchResponse> {
return this._http.request<IElasticsearchResponse>('POST', this.elasticSearchUrl, {
body: {
......@@ -100,10 +107,15 @@ export class ElasticsearchService {
);
}
// This retrieves the data of a dataset. The query is done by uuid.
getDatasetData(id: number | string, options?: ElasticsearchOptions): Observable<IElasticsearchResponse> {
// Set the pagination
const from = options ? options.from : 0;
const size = options ? options.pageSize : 10;
// Retrieve only the data (not the editorial-metadata, metadata, etc..)
// This is done by setting 'data-fr' as _source.
const body = {
from,
size,
......@@ -126,13 +138,14 @@ export class ElasticsearchService {
body['sort'] = [sort];
}
// Here the default_field is 'data', meaning that the query
if (options.searchString !== '') {
const searchString = this.escapeSpecialCharacters(options.searchString, options.fromAutocompletion, 'AND');
body.query.bool['must'] = [
{
query_string: {
query: searchString,
default_field: '*.mapbox',
default_field: 'data',
analyzer: 'my_search_analyzer',
fuzziness: 'AUTO',
minimum_should_match: '90%',
......@@ -141,6 +154,9 @@ export class ElasticsearchService {
];
}
// This is an option to use an nginx cache or not.
// We usually use it, except for real time data when you want all the time the
// latest and more fresh data.
const headers = options.useCache ? {} : { nocache: 'True' };
return this._http.request<IElasticsearchResponse>('POST', this.elasticSearchUrl, {
......@@ -165,7 +181,7 @@ export class ElasticsearchService {
.replace(/\&\&/g, '\\&&'); // replace &&
const request = {
_source: false,
_source: false, // We need only the highlight, not the hits
query: {
bool: {
must: [
......@@ -185,6 +201,7 @@ export class ElasticsearchService {
minimum_should_match: '90%',
},
},
// Rules to construct the highlight fragments
fields: {
'*data-fr.*': {
fragment_size: 50,
......@@ -235,7 +252,7 @@ export class ElasticsearchService {
suggestion: {
phrase: {
field: 'data_and_metadata.suggest',
// as only the very first suggestion will be used, let's limit the size of the results to 1:
// as only the very first suggestion will be used, let's limit the size of the results to 1
size: 1,
max_errors: query.split(' ').length,
highlight: {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment