Skip to content

Commit

Permalink
First draft of search api integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell committed Jan 15, 2020
1 parent b1a549e commit a256467
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"drush/drush": "^9.7.1",
"drupal/config_update": "1.x-dev",
"drupal/entity": "1.0.0-rc1",
"drupal/search_api": "^1.15",
"drupal/facets": "^1.4",
"fmizzell/json_form": "dev-8.x-1.x",
"getdkan/contracts": "^1.0.0",
"getdkan/datastore" : "^3.0.0",
Expand Down
10 changes: 10 additions & 0 deletions modules/custom/dkan_search_api/dkan_search_api.info.yml
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
7 changes: 7 additions & 0 deletions modules/custom/dkan_search_api/dkan_search_api.routing.yml
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 modules/custom/dkan_search_api/src/ComplexData/Dataset.php
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;
}

}
67 changes: 67 additions & 0 deletions modules/custom/dkan_search_api/src/Controller.php
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);
}
}
Loading

0 comments on commit a256467

Please sign in to comment.