Skip to content

Commit

Permalink
Add rudimentary fuzzy/ngram search (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
cachan committed Nov 22, 2015
1 parent f76c510 commit 632f17f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

ES_HOST=localhost
ES_INDEX=dino
ES_INDEX=dinos
1 change: 1 addition & 0 deletions app/Console/Commands/CreateIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ public function handle()
{
$this->info("Creating elasticsearch index");
$this->search->createIndex();
$this->search->insertDocument(array());
}
}
35 changes: 29 additions & 6 deletions app/Http/Controllers/Api/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,43 @@ public function __construct(Search $search)
{
$this->search = $search;
}

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function getIndex(Request $request)
{
$terms = [];

foreach ($request->all() as $key => $value) {
if ('q' === $key) {
// Fuzzy search.
}
else {
// Faceted search
}
if ('q' === $key) {
$terms['keyword'] = $value;
} else {
// Faceted search
}
}

$results = $this->search->search($terms);
return ApiResponseFactory::MakeEnvelope($this->parseResultsToResponse($results));
}

protected function parseResultsToResponse(array $results)
{
$data = [];

if (empty($results['hits'])) {
return [];
}

foreach ($results['hits']['hits'] as $result) {
$data[] = [
'id' => $result['_id'],
'source' => $result['_source']
];
}

return $data;
}
}
65 changes: 56 additions & 9 deletions app/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Search
{
/** @var Client */
private $client;
private $type = "dinos";

public function __construct()
{
Expand All @@ -23,24 +24,51 @@ public function __construct()
*/
public function createIndex()
{
// $this->client->indices()->delete(['index' => env('ES_INDEX')]);
// Try to delete the index if it already exists.
try {
$this->client->indices()->delete(['index' => env('ES_INDEX')]);
}
catch (\Exception $e) {

}

// Create the mapping
$params = [
'index' => env('ES_INDEX'),
'body' => [
'settings' => [
'number_of_shards' => 1,
'number_of_replicas' => 0,
'analysis' => [
'filter' => [
'autocomplete_filter' => [
'type' => 'ngram',
'min_gram' => 3,
'max_gram' => 20,
],
],
'analyzer' => [
'autocomplete' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'lowercase',
'autocomplete_filter',
]
]
]
]
],
'mappings' => [
'dino' => [
$this->type => [
'properties' => [
'speciesCommonName' => [
'type' => 'string',
'analyzer' => 'standard',
'analyzer' => 'autocomplete',
],
'speciesScientificName' => [
'type' => 'string',
'analyzer' => 'standard',
'analyzer' => 'autocomplete',
],
'scanId' => [
'type' => 'string',
Expand Down Expand Up @@ -98,9 +126,8 @@ public function createIndex()
]
]
];

$this->client->indices()->create($params);

$this->client->indices()->create($params);
}

/**
Expand All @@ -115,13 +142,26 @@ public function getSchema()
}

/**
* Faceted search
* @param array $terms
*
* @param array $params
* @return array
*/
public function search(array $params)
public function search(array $terms)
{
$params = [
'index' => env('ES_INDEX'),
'type' => $this->type,
'body' => [
'query' => [
'multi_match' => [
'query' => $terms['keyword'],
'fields' => ["speciesCommonName", "speciesScientificName"]
]
]
]
];

return $this->client->search($params);
}

/**
Expand All @@ -141,7 +181,14 @@ public function searchAutocomplete($text)
*/
public function insertDocument(array $doc)
{
$params = [
'index' => env('ES_INDEX'),
'type' => $this->type,
'body' => [ 'speciesCommonName' => 'Oviraptorid', 'speciesScientificName' => 'Citipati osmolskae']
];

// Document will be indexed to my_index/my_type/<autogenerated ID>
$result = $this->client->index($params);
}

/**
Expand Down

0 comments on commit 632f17f

Please sign in to comment.