public static function SearchFactory::parseResult in Elasticsearch Connector 8.2
Same name and namespace in other branches
- 8.7 src/ElasticSearch/Parameters/Factory/SearchFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\SearchFactory::parseResult()
- 8.5 src/ElasticSearch/Parameters/Factory/SearchFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\SearchFactory::parseResult()
- 8.6 src/ElasticSearch/Parameters/Factory/SearchFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\SearchFactory::parseResult()
Parse a elastic search response into a ResultSetInterface TODO: Add excerpt handling.
Parameters
\Drupal\search_api\Query\QueryInterface $query:
array $response:
Return value
\Drupal\search_api\Query\ResultSetInterface
1 call to SearchFactory::parseResult()
- SearchApiElasticsearchBackend::search in src/
Plugin/ search_api/ backend/ SearchApiElasticsearchBackend.php - Executes a search on this server.
File
- src/
ElasticSearch/ Parameters/ Factory/ SearchFactory.php, line 36
Class
- SearchFactory
- Class SearchFactory.
Namespace
Drupal\elasticsearch_connector\ElasticSearch\Parameters\FactoryCode
public static function parseResult(QueryInterface $query, array $response) {
$index = $query
->getIndex();
// Set up the results array.
$results = $query
->getResults();
$results
->setExtraData('elasticsearch_response', $response);
$results
->setResultCount($response['hits']['total']);
/** @var \Drupal\search_api\Utility\FieldsHelper $fields_helper */
$fields_helper = \Drupal::getContainer()
->get('search_api.fields_helper');
// Add each search result to the results array.
if (!empty($response['hits']['hits'])) {
foreach ($response['hits']['hits'] as $result) {
$result_item = $fields_helper
->createItem($index, $result['_id']);
$result_item
->setScore($result['_score']);
// Set each item in _source as a field in Search API.
foreach ($result['_source'] as $elasticsearch_property_id => $elasticsearch_property) {
// Make everything a multifield.
if (!is_array($elasticsearch_property)) {
$elasticsearch_property = [
$elasticsearch_property,
];
}
$field = $index
->getField($elasticsearch_property_id);
if (!$field instanceof FieldInterface) {
$field = $fields_helper
->createField($index, $elasticsearch_property_id, [
'property_path' => $elasticsearch_property_id,
]);
}
$field
->setValues($elasticsearch_property);
$result_item
->setField($elasticsearch_property_id, $field);
}
$results
->addResultItem($result_item);
}
}
return $results;
}