You are here

protected function SearchBuilder::setMoreLikeThisQuery in Elasticsearch Connector 8.5

Same name and namespace in other branches
  1. 8.7 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::setMoreLikeThisQuery()
  2. 8.6 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::setMoreLikeThisQuery()

Setup the More like this clause of the Elasticsearch query.

Adjusts $this->body to have a more like this query.

Parameters

array $query_options: Array of query options. We're most interested here in the key of 'mlt', which should contain the following keys:

  • id: To be used as the like_text in the more_like_this query.
  • fields: Array of fields.
1 call to SearchBuilder::setMoreLikeThisQuery()
SearchBuilder::build in src/ElasticSearch/Parameters/Builder/SearchBuilder.php
Build up the body of the request to the Elasticsearch _search endpoint.

File

src/ElasticSearch/Parameters/Builder/SearchBuilder.php, line 505

Class

SearchBuilder
Class SearchBuilder.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder

Code

protected function setMoreLikeThisQuery(array $query_options) {
  if (!empty($query_options['mlt'])) {
    $mlt_query['more_like_this'] = [];

    // Transform input parameter "id" to "ids" if available.
    if (isset($query_options['mlt']['id'])) {
      $query_options['mlt']['ids'] = is_array($query_options['mlt']['id']) ? $query_options['mlt']['id'] : [
        $query_options['mlt']['id'],
      ];
      unset($query_options['mlt']['id']);
    }

    // Input parameter: ids
    if (isset($query_options['mlt']['ids'])) {
      $mlt_query['more_like_this']['ids'] = $query_options['mlt']['ids'];
    }

    // Input parameter: like
    if (isset($query_options['mlt']['like'])) {
      $mlt_query['more_like_this']['like'] = $query_options['mlt']['like'];
    }

    // Input parameter: unlike
    if (isset($query_options['mlt']['unlike'])) {
      $mlt_query['more_like_this']['unlike'] = $query_options['mlt']['unlike'];
    }

    // Input parameter: fields
    $mlt_query['more_like_this']['fields'] = array_values($query_options['mlt']['fields']);

    // TODO: Make this settings configurable in the view.
    $mlt_query['more_like_this']['max_query_terms'] = 1;
    $mlt_query['more_like_this']['min_doc_freq'] = 1;
    $mlt_query['more_like_this']['min_term_freq'] = 1;
    $this->body['query']['bool']['must'][] = $mlt_query;
  }
}