-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of search api integration.
- Loading branch information
Showing
7 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: Data Search API | ||
description: "Integation between DKAN's data module and the Search API module." | ||
type: module | ||
core: 8.x | ||
dependencies: | ||
- dkan_data | ||
- dkan_metastore | ||
- dkan_schema | ||
- search_api | ||
package: DKAN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
dkan_data_search_api.search: | ||
path: '/api/1/search' | ||
methods: [GET] | ||
defaults: | ||
{ _controller: '\Drupal\dkan_search_api\Controller::search'} | ||
requirements: | ||
_permission: 'access content' |
97 changes: 97 additions & 0 deletions
97
modules/custom/dkan_search_api/src/ComplexData/Dataset.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
namespace Drupal\dkan_search_api\ComplexData; | ||
|
||
use Drupal\Core\TypedData\DataDefinition; | ||
use Drupal\Core\TypedData\ListDataDefinition; | ||
use Drupal\Core\TypedData\Plugin\DataType\ItemList; | ||
use Drupal\Core\TypedData\TypedData; | ||
use Drupal\dkan_search_api\Facade\ComplexDataFacade; | ||
use Drupal\dkan_schema\SchemaRetriever; | ||
|
||
class Dataset extends ComplexDataFacade | ||
{ | ||
private $data; | ||
|
||
public static function definition() { | ||
$definitions = []; | ||
|
||
/** @var $schemaRetriever SchemaRetriever */ | ||
$schemaRetriever = \Drupal::service("dkan_schema.schema_retriever"); | ||
$json = $schemaRetriever->retrieve("dataset"); | ||
$object = json_decode($json); | ||
$properties = array_keys((array) $object->properties); | ||
|
||
foreach ($properties as $property) { | ||
$type = $object->properties->{$property}->type; | ||
|
||
if ($type == "object" || $type == "any") { | ||
$type = "string"; | ||
} | ||
|
||
if ($type == "array") { | ||
$definitions[$property] = ListDataDefinition::create("string"); | ||
} | ||
else { | ||
$definitions[$property] = DataDefinition::createFromDataType($type); | ||
} | ||
} | ||
|
||
return $definitions; | ||
} | ||
|
||
public function __construct(string $json) | ||
{ | ||
$this->data = json_decode($json); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get($property_name) | ||
{ | ||
$definitions = self::definition(); | ||
|
||
if (!isset($definitions[$property_name])) { | ||
return NULL; | ||
} | ||
|
||
$definition = $definitions[$property_name]; | ||
|
||
if ($definition instanceof ListDataDefinition) { | ||
$property = new ItemList($definition, $property_name); | ||
$values = $this->data->{$property_name}; | ||
if (is_string($values)) { | ||
$values = json_decode($values); | ||
} | ||
$property->setValue($values); | ||
} | ||
else { | ||
$property = new class($definition, $property_name) extends TypedData {}; | ||
$property->setValue($this->data->{$property_name}); | ||
} | ||
|
||
return $property; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getProperties($include_computed = false) | ||
{ | ||
$definitions = self::definition(); | ||
$properties = []; | ||
foreach (array_keys($definitions) as $propertyName) { | ||
$properties[$propertyName] = $this->get($propertyName); | ||
} | ||
return $properties; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->data; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Drupal\dkan_search_api; | ||
|
||
use Drupal\dkan_common\JsonResponseTrait; | ||
use Drupal\dkan_metastore\Service; | ||
use Drupal\search_api\Query\ResultSet; | ||
|
||
class Controller | ||
{ | ||
use JsonResponseTrait; | ||
|
||
public function search() { | ||
$defaults = [ | ||
"pageSize" => 10, | ||
"page" => 1 | ||
]; | ||
|
||
$params = \Drupal::request()->query->all(); | ||
|
||
foreach ($defaults as $param => $default) { | ||
$params[$param] = isset($params[$param]) ? $params[$param] : $default; | ||
} | ||
|
||
/* @var $qh \Drupal\search_api\Utility\QueryHelper */ | ||
$qh = \Drupal::service("search_api.query_helper"); | ||
|
||
$storage = \Drupal::service("entity.manager")->getStorage('search_api_index'); | ||
|
||
/** @var $metastore Service */ | ||
$metastore = \Drupal::service("dkan_metastore.service"); | ||
|
||
/** @var \Drupal\search_api\IndexInterface $index */ | ||
$index = $storage->load('index_1'); | ||
|
||
$query = $qh->createQuery($index); | ||
|
||
if ($params['fulltext']) { | ||
$fulltextFields = $index->getFulltextFields(); | ||
$cg = $query->createConditionGroup('OR'); | ||
foreach ($fulltextFields as $field) { | ||
$cg->addCondition($field, $params['fulltext']); | ||
} | ||
$query->addConditionGroup($cg); | ||
} | ||
|
||
$query->sort('search_api_relevance', $query::SORT_DESC); | ||
|
||
$end = ($params['page'] * $params['pageSize']); | ||
$start = $end - $params['pageSize']; | ||
$query->range($start, $params['pageSize']); | ||
|
||
/** @var $result ResultSet*/ | ||
$result = $query->execute(); | ||
|
||
$data = []; | ||
$scores = []; | ||
foreach ($result->getResultItems() as $item) { | ||
$id = $item->getId(); | ||
$id = str_replace("dkan_data/", "", $id); | ||
$scores[$id] = $item->getBoost(); | ||
$data[] = json_decode($metastore->get("dataset", $id)); | ||
} | ||
|
||
return $this->getResponse($data); | ||
} | ||
} |
Oops, something went wrong.