-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds lunr to composer * Update Search.php * Update Search.php * Update config.yml * Combines search index with lunr * Adds search updates * First pass at making interra_api into dkan_lunr. * Fixing lunr.php compser entry. * Finalizing the change to dkan_lunr and deactivating frontend tests. * Fixing complexity issue.
- Loading branch information
Showing
17 changed files
with
258 additions
and
416 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
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
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
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,5 @@ | ||
name: Lunr | ||
description: 'PHP Lunr index generator and endpoint.' | ||
type: module | ||
core: 8.x | ||
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_lunr.search: | ||
path: '/api/v1/search-index.json' | ||
defaults: | ||
{ _controller: '\Drupal\dkan_lunr\Controller\ApiController::search'} | ||
requirements: | ||
_permission: 'access content' | ||
|
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,6 @@ | ||
services: | ||
dkan_lunr.search: | ||
class: Drupal\dkan_lunr\Search | ||
shared: false | ||
dkan_lunr.dataset_modifier: | ||
class: Drupal\dkan_lunr\Service\DatasetModifier |
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,42 @@ | ||
<?php | ||
|
||
namespace Drupal\dkan_lunr\Controller; | ||
|
||
use Dkan\Datastore\Manager; | ||
use Drupal\Core\Controller\ControllerBase; | ||
use JsonSchemaProvider\Provider; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Drupal\dkan_schema\Schema; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
/** | ||
* An ample controller. | ||
* @codeCoverageIgnore | ||
*/ | ||
class ApiController extends ControllerBase { | ||
|
||
/** | ||
* | ||
*/ | ||
public function search(Request $request) { | ||
/** @var \Drupal\dkan_lunr\Search $search */ | ||
$search = \Drupal::service('dkan_lunr.search'); | ||
return $this->response($search->index()); | ||
} | ||
|
||
/** | ||
* | ||
* @param mixed $resp | ||
* @return \Symfony\Component\HttpFoundation\JsonResponse | ||
*/ | ||
protected function response($resp) { | ||
/** @var \Symfony\Component\HttpFoundation\JsonResponse $response */ | ||
$response = \Drupal::service('dkan.factory') | ||
->newJsonResponse($resp); | ||
$response->headers->set('Access-Control-Allow-Origin', '*'); | ||
$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PATCH, DELETE'); | ||
$response->headers->set('Access-Control-Allow-Headers', 'Authorization'); | ||
return $response; | ||
} | ||
} |
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,151 @@ | ||
<?php | ||
/** | ||
* @file | ||
* Creates search index using Lunr.php. | ||
*/ | ||
|
||
namespace Drupal\dkan_lunr; | ||
|
||
use LunrPHP\Pipeline; | ||
use LunrPHP\LunrDefaultPipelines; | ||
use LunrPHP\BuildLunrIndex; | ||
|
||
/** | ||
* Indexes datasets using Lunr.php. | ||
* @codeCoverageIgnore | ||
*/ | ||
class Search { | ||
|
||
|
||
/** | ||
* Fields to be searched for in the Lunr index. The more fields added the | ||
* bigger the index. | ||
* | ||
* TODO: Make configurable. | ||
*/ | ||
public $searchIndexFields = [ | ||
"title", | ||
"keyword", | ||
"theme", | ||
"description" | ||
]; | ||
|
||
/** | ||
* Fields to be available in search results. The more fields added the | ||
* bigger the index. | ||
* | ||
* TODO: Make configurable. | ||
*/ | ||
public $searchDocFields = [ | ||
"title", | ||
"identifier", | ||
"description", | ||
"modified", | ||
"distribution", | ||
"keyword", | ||
"theme" | ||
]; | ||
|
||
public $ref = "identifier"; | ||
|
||
public function formatDocs($docs) { | ||
$index = []; | ||
foreach ($docs as $id => $doc) { | ||
$index[] = $this->formatSearchDoc($doc); | ||
} | ||
return $index; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function formatSearchDoc($value) { | ||
$formatted = new \stdClass(); | ||
$doc = new \stdClass(); | ||
foreach ($this->searchDocFields as $field) { | ||
$doc->{$field} = isset($value->{$field}) ? $value->{$field} : null; | ||
} | ||
$formatted->doc = $doc; | ||
$formatted->ref = $doc->{$this->ref}; | ||
return $formatted; | ||
} | ||
|
||
|
||
public function lunrIndex() { | ||
// TODO: Make this configurable. | ||
$build = new BuildLunrIndex(); | ||
$build->ref($this->ref); | ||
foreach($this->searchIndexFields as $field) { | ||
$build->field($field); | ||
} | ||
|
||
$build->addPipeline('LunrPHP\LunrDefaultPipelines::trimmer'); | ||
$build->addPipeline('LunrPHP\LunrDefaultPipelines::stop_word_filter'); | ||
// Stemmer doesn't work with wildcard search. | ||
//$build->addPipeline('LunrPHP\LunrDefaultPipelines::stemmer'); | ||
|
||
$datasets = $this->getDatasets(); | ||
foreach ($datasets as $dataset) { | ||
$doc = []; | ||
array_push($this->searchIndexFields, $this->ref); | ||
foreach($this->searchIndexFields as $field) { | ||
if (isset($dataset->{$field})) { | ||
if (is_array($dataset->{$field})) { | ||
$doc[$field] = $dataset->{$field}; | ||
} | ||
else { | ||
$doc[$field] = strtolower(strip_tags($dataset->{$field})); | ||
} | ||
} | ||
} | ||
$build->add($doc); | ||
} | ||
|
||
|
||
return $build->output(); | ||
} | ||
|
||
public function docs() { | ||
$datasets = []; | ||
/** @var Service\DatasetModifier $dataset_modifier */ | ||
$dataset_modifier = \Drupal::service('dkan_lunr.dataset_modifier'); | ||
foreach ($this->getDatasets() as $dataset) { | ||
$datasets[] = $dataset_modifier->modifyDataset($dataset); | ||
} | ||
return $this->formatDocs($datasets); | ||
} | ||
|
||
|
||
/** | ||
* Indexes the available datasets. | ||
*/ | ||
public function index() { | ||
return [ | ||
'index' => $this->lunrIndex(), | ||
'docs' => $this->docs() | ||
]; | ||
} | ||
|
||
/** | ||
* Get datasets. | ||
* | ||
* @TODO Shouldn't use controller inner workings like this. Should refactor to service. | ||
* | ||
* @return array Array of dataset objects | ||
*/ | ||
protected function getDatasets() { | ||
/** @var \Drupal\dkan_api\Controller\Dataset $dataset_controller */ | ||
$dataset_controller = \Drupal::service('dkan_api.controller.dataset'); | ||
|
||
// Engine returns array of json strings. | ||
return array_map( | ||
function ($item) { | ||
return json_decode($item); | ||
}, | ||
$dataset_controller->getEngine() | ||
->get() | ||
); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.