You are here

protected function SearchApiElasticsearchConnector::handleMLTSearch in Elasticsearch Connector 7

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::handleMLTSearch()
  2. 7.2 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::handleMLTSearch()

Handle the "More like this" functionality if it is required.

Parameters

SearchApiQueryInterface $query:

array $index_fields:

Return value

array

1 call to SearchApiElasticsearchConnector::handleMLTSearch()
SearchApiElasticsearchConnector::handleFulltextSearch in modules/elasticsearch_connector_search_api/service.inc
Helper function. Handle freetext search parameters.

File

modules/elasticsearch_connector_search_api/service.inc, line 1546
Provides a Elasticsearch-based service class for the Search API using Elasticsearch Connector module.

Class

SearchApiElasticsearchConnector
Search service class.

Code

protected function handleMLTSearch(SearchApiQueryInterface $query, $index_fields) {
  $query_options = $query
    ->getOptions();
  $mlt_queries = array();

  // More Like This
  if (!empty($query_options['search_api_mlt'])) {
    $mlt = $query_options['search_api_mlt'];
    $mlt_query['more_like_this'] = array();
    $version = $this
      ->getClusterVersion();
    if (version_compare($version, '1.3.0', '>=')) {
      $mlt_query['more_like_this']['ids'] = array(
        $mlt['id'],
      );
      $mlt_query['more_like_this']['fields'] = array_values($mlt['fields']);
      $mlt_query['more_like_this']['max_query_terms'] = $mlt['max_query_terms'];
      $mlt_query['more_like_this']['min_doc_freq'] = $mlt['min_doc_freq'];
      $mlt_query['more_like_this']['min_term_freq'] = $mlt['min_term_freq'];
      $mlt_queries[] = $mlt_query;
    }
    else {
      $document = $this
        ->getItemFromElasticsearch($query
        ->getIndex(), $mlt['id']);
      $fields = array_values($mlt['fields']);
      foreach ($fields as $field) {
        $mlt_query = array();
        $mlt_query['more_like_this']['fields'] = array(
          $field,
        );
        $mlt_query['more_like_this']['max_query_terms'] = $mlt['max_query_terms'];
        $mlt_query['more_like_this']['min_doc_freq'] = $mlt['min_doc_freq'];
        $mlt_query['more_like_this']['min_term_freq'] = $mlt['min_term_freq'];
        $mlt_query['more_like_this']['like_text'] = $document['_source'][$field];
        $mlt_queries[] = $mlt_query;
      }
    }
  }
  return $mlt_queries;
}