triggerDefaultQuery method

Future triggerDefaultQuery (
  1. {Option options}
)

can be used to execute the default query for a particular widget. For examples,

  • to display the suggestions or results for a QueryType.search type of widget,
  • to display the filter options(aggregations) for a QueryType.term type of widget

Implementation

Future triggerDefaultQuery({Option options}) async {
  // To prevent duplicate queries
  if (isEqual(this._query, this.componentQuery)) {
    return Future<bool>.value(true);
  }
  try {
    this._updateQuery();
    this._setRequestStatus(RequestStatus.PENDING);
    final results = await this._fetchRequest({
      'query': this.query is List ? this.query : [this.query],
      'settings': this.appbaseSettings?.toJSON()
    }, false);
    final prev = this.results;
    final Map rawResults =
        results != null && results[this.id] is Map ? results[this.id] : {};
    void afterResponse() {
      if (rawResults['aggregations'] != null) {
        this._handleAggregationResponse(rawResults['aggregations'],
            options: new Options(stateChanges: options?.stateChanges));
      }
      this._setRequestStatus(RequestStatus.INACTIVE);
      this._applyOptions(new Options(stateChanges: options?.stateChanges),
          'results', prev, this.results);
    }

    if ((this.type == null || this.type == QueryType.search) &&
        this.enablePopularSuggestions == true) {
      final rawPopularSuggestions =
          await this._fetchRequest(this._getSuggestionsQuery(), true);
      if (rawPopularSuggestions != null) {
        final popularSuggestionsData =
            rawPopularSuggestions[suggestionQueryID];
        // Merge popular suggestions as the top suggestions
        if (popularSuggestionsData != null &&
            popularSuggestionsData['hits'] != null &&
            popularSuggestionsData['hits']['hits'] != null &&
            rawResults != null &&
            rawResults['hits'] != null &&
            rawResults['hits']['hits'] != null) {
          rawResults['hits']['hits'] = [
            ...rawResults['hits']['hits'],
            ...(popularSuggestionsData['hits']['hits'] as List)
                .map((hit) => ({
                      ...hit,
                      // Set the popular suggestion tag for suggestion hits
                      '_popular_suggestion': true
                    }))
                .toList(),
          ];
        }
        this._appendResults(rawResults);
        afterResponse();
      }
    } else {
      this._appendResults(rawResults);
      afterResponse();
    }
    return Future.value(rawResults);
  } catch (err) {
    return _handleError(err);
  }
}