diff --git a/src/app/datasets/components/results/results.component.ts b/src/app/datasets/components/results/results.component.ts index 84917034f71fdea1eee8778c1ebd87fb635d5c15..983486d0e59c5731cb140b946d65352e2686bbba 100644 --- a/src/app/datasets/components/results/results.component.ts +++ b/src/app/datasets/components/results/results.component.ts @@ -94,7 +94,6 @@ export class ResultsComponent implements OnInit, OnDestroy { // We need to make an initial request to get the aggregations and subaggregations this._datasetResearchService.getResults().subscribe(() => { // Create the ESOptions - const options = this._researchUrlService.getOptionsFromParameters(params); this._datasetResearchService.elasticSearchOptions.setElasticsearchOptions(options); // Force to recalculate the aggregate counts diff --git a/src/app/datasets/components/sort/sort.component.ts b/src/app/datasets/components/sort/sort.component.ts index 5981188f472f922d7c9cedc9f3241cc440a1022a..4d6b33a8bd45c34daa7b34f7817dbefad5fffc68 100644 --- a/src/app/datasets/components/sort/sort.component.ts +++ b/src/app/datasets/components/sort/sort.component.ts @@ -1,6 +1,7 @@ -import { Component, OnInit } from '@angular/core'; +import {Component, OnDestroy, OnInit} from '@angular/core'; import { DatasetResearchService } from '../../services'; import { geosource } from '../../../../i18n/traductions'; +import { Subscription } from 'rxjs'; interface IDropdownOptions { 'label': string; @@ -14,13 +15,15 @@ interface IDropdownOptions { templateUrl: './sort.component.html', styleUrls: ['./sort.component.scss'], }) -export class SortComponent implements OnInit { +export class SortComponent implements OnInit, OnDestroy { // Options list for the sort dropdown options: IDropdownOptions[]; // Selected value of the dropdown selectedOption: IDropdownOptions; + searchChangeSubscription: Subscription; + dropDownFormatsToggle: boolean = false; constructor( @@ -61,14 +64,20 @@ export class SortComponent implements OnInit { } ngOnInit() { - this.selectedOption = this.options.find( - e => ( - e.value === this._datasetResearchService.sortOptions.value && - e.order === this._datasetResearchService.sortOptions.order - ), + this.selectedOption = this.getOptionFromValueAndOrder(this._datasetResearchService.sortOptions); + this.searchChangeSubscription = this._datasetResearchService.searchChange$.subscribe( + (sortOption: any) => { + if (sortOption && sortOption.value && sortOption.order) { + this.sortValueChanged(this.getOptionFromValueAndOrder(sortOption)); + } + }, ); } + ngOnDestroy(): void { + this.searchChangeSubscription.unsubscribe(); + } + sortValueChanged(option) { this.selectedOption = option; this._datasetResearchService.sortChanged({ @@ -81,4 +90,12 @@ export class SortComponent implements OnInit { return this._datasetResearchService.isLoading; } + private getOptionFromValueAndOrder(sortOption) { + return this.options.find( + e => ( + e.value === sortOption.value && + e.order === sortOption.order + ), + ); + } } diff --git a/src/app/datasets/services/dataset-research.service.ts b/src/app/datasets/services/dataset-research.service.ts index 948d86265b0448306665152668874b4a5622fa18..ac475fa650c242c2d12df79a16f9ccc468db1e00 100644 --- a/src/app/datasets/services/dataset-research.service.ts +++ b/src/app/datasets/services/dataset-research.service.ts @@ -463,6 +463,8 @@ export class DatasetResearchService { this._elasticsearchOptions.searchString = value; this._elasticsearchOptions.pageIndex = 0; this._elasticsearchOptions.shouldAggregateResultCount = true; + this._elasticsearchOptions.sortOptions.value = (value && true) ? 'relevance' : 'date'; + this._elasticsearchOptions.sortOptions.order = 'desc'; if (this._router.url.split('/').pop() !== AppRoutes.research.uri) { if (value === '') { this._router.navigate(['/', `${AppRoutes.research.uri}`]); @@ -472,7 +474,7 @@ export class DatasetResearchService { } this._researchUrlService.setParameter('q', value); - this._searchChangeSubject.next(); + this._searchChangeSubject.next(this._elasticsearchOptions.sortOptions); } scopeChanged(scope: IScope) { diff --git a/src/app/elasticsearch/models/elasticsearch-options.model.ts b/src/app/elasticsearch/models/elasticsearch-options.model.ts index feca498e889d99a2052c0fa04bb36e8f197e9b76..c3995f42eb2d278ad53136ec739d6d402042555e 100644 --- a/src/app/elasticsearch/models/elasticsearch-options.model.ts +++ b/src/app/elasticsearch/models/elasticsearch-options.model.ts @@ -66,7 +66,7 @@ export class ElasticsearchOptions { // Need to be done in order to avoid object reference copy this._sortOptions = { - value: 'date', + value: (data && data.searchString != null) ? 'relevance' : 'date', order: 'desc', }; if (data && data.sortOptions != null) { @@ -149,7 +149,7 @@ export class ElasticsearchOptions { // Need to be done in order to avoid object reference copy this._sortOptions = { - value: 'date', + value: (data && data.searchString != null) ? 'relevance' : 'date', order: 'desc', }; if (data && data.sortOptions != null) {