Skip to content

Commit

Permalink
Support synonyms better
Browse files Browse the repository at this point in the history
  • Loading branch information
almasaeed2010 committed Feb 26, 2019
1 parent cd1fd97 commit 9c196ef
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 84 deletions.
21 changes: 12 additions & 9 deletions includes/Common/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,16 @@ public function setIndexParams($index_name, $shards = 5, $replicas = 0, $tokeniz
'number_of_replicas' => $replicas,
'analysis' => [
'analyzer' => [
$index_name => [
'type' => 'custom',
'tokenizer' => $tokenizer,
'filter' => array_keys($filters),
],
//$index_name => [
// 'type' => 'custom',
// 'tokenizer' => $tokenizer,
// 'filter' => array_keys($filters),
//],
'synonym' => [
'tokenizer' => 'whitespace',
'filter' => [
'synonym',
'lowercase'
],
'filter' => array_unique(
['synonym', 'lowercase'] + array_keys($filters)
),
],
],
'filter' => [
Expand All @@ -315,6 +314,10 @@ public function setIndexParams($index_name, $shards = 5, $replicas = 0, $tokeniz
'format' => 'wordnet',
'synonyms_path' => 'analysis/wn_s.pl',
],
'stemmer' => [
'type' => 'stemmer',
'language' => 'english',
],
],
],
'max_result_window' => 1000000,
Expand Down
58 changes: 21 additions & 37 deletions includes/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
*/
class Model{

/**
* @var array
*/
protected $attributes = [];

/**
* The ES instance.
*
Expand Down Expand Up @@ -362,46 +357,35 @@ public function search() {
}

/**
* Get query paramaters.
* Paginate the results.
*
* @param int $per_page
*
* @return array
* @throws \Exception
*/
public function getQuery() {
return $this->builder->build();
public function paginate($per_page = 10) {
$start = microtime(true);
$results = $this->search();
$total = $results['hits']['total'];
$current_page = pager_default_initialize($total, $per_page);
return [
'results' => $results,
'total' => $total,
'page' => $current_page + 1,
'pages' => ceil($total / $per_page),
'pager' => theme('pager', ['quantity', $total]),
'time' => microtime(true) - $start
];
}

/**
* Get an attribute.
*
* @param string $name
* The name of attribute.
*
* @return mixed
* The value of the attribute if it exists.
*/
public function __get($name) {
if (array_key_exists($name, $this->attributes)) {
return $this->attributes[$name];
}

if (method_exists(static::class, $name)) {
return;
}

return $this->{$name};
}

/**
* Check if an attribute isset.
*
* @param string $name
* The name of the attribute.
* Get query paramaters.
*
* @return bool
* Whether the attribute has been set.
* @return array
* @throws \Exception
*/
public function __isset($name) {
return isset($this->attributes[$name]);
public function getQuery() {
return $this->builder->build();
}
}
2 changes: 1 addition & 1 deletion includes/Query/SimpleQueryClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function multiMatch(array $fields, $value) {
'fields' => $fields,
'query' => $value,
'lenient' => TRUE,
//'type' => 'phrase',
'analyzer' => 'synonym',
//'type' => 'phrase',
'fuzziness' => 'AUTO',
],
];
Expand Down
77 changes: 56 additions & 21 deletions includes/tripal_elasticsearch.advanced_search.form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {

$form['#method'] = 'GET';

if (isset($_GET['term']) && !empty($_GET['term'])) {
$form['wrapper']['results'] = [
'#markup' => tripal_elasticsearch_advanced_search_results($_GET),
];
}
//if (isset($_GET['term']) && !empty($_GET['term'])) {
$form['wrapper']['results'] = [
'#markup' => tripal_elasticsearch_advanced_search_results($_GET),
];
//}

return $form;
}
Expand All @@ -79,26 +79,18 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {
* @return string
*/
function tripal_elasticsearch_advanced_search_results(array $values) {
$fields = ['content.*', 'title'];

if (isset($_GET['field']) && !empty($_GET['field'])) {
$fields = ["content.{$_GET['field']}"];
if (empty($values['category']) && empty($values['term'])) {
return '';
}

try {
$instance = new \ES\Common\Instance();
$model = new \ES\Models\Model();
$model->setIndexName('entities');
$model->where($fields, trim($values['term']));

$category = trim($values['category'] ?? '');
if (!empty($category)) {
$model->where('bundle_label', "\"$category\"");
$results = tripal_elasticsearch_perform_advanced_search($values, 15);
if($results['total'] == 0 && !empty($values['term'])) {
$values['term'] = strtolower($values['term']);
$results = tripal_elasticsearch_perform_advanced_search($values, 15);
}

$model->highlight(['content.*', 'title']);
$data = $model->search();
$hits = $instance->formatHits($data);
$hits = $instance->formatHits($results['results']);

if (count($hits) === 0) {
return theme(
Expand All @@ -109,7 +101,16 @@ function tripal_elasticsearch_advanced_search_results(array $values) {
);
}

return tripal_elasticsearch_get_website_search_result_table($hits);
$content = theme('elasticsearch_results_header', [
'page' => $results['page'],
'total' => $results['total'],
'pages' => $results['pages'],
'time' => $results['time']
]);
$content .= tripal_elasticsearch_get_website_search_result_table($hits, false);
$content .= $results['pager'];

return $content;
} catch (Exception $exception) {
watchdog(
'tripal_elasticsearch',
Expand All @@ -123,6 +124,40 @@ function tripal_elasticsearch_advanced_search_results(array $values) {
}
}

/**
* @param $values
*
* @return array
* @throws \Exception
*/
function tripal_elasticsearch_perform_advanced_search($values, $per_page = NULL) {
$fields = ['content.*', 'title'];

if (isset($values['field']) && !empty($values['field'])) {
$fields = ["content.{$_GET['field']}"];
}

$model = new \ES\Models\Model();
$model->setIndexName('entities');

if (!empty($values['term'])) {
$model->where($fields, trim($values['term']));
}

$category = trim($values['category'] ?? '');
if (!empty($category)) {
$model->where('bundle_label', "\"$category\"");
}

$model->highlight(['content.*', 'title']);

if (is_null($per_page)) {
return $model->search();
}

return $model->paginate($per_page);
}

/**
* AJAX Callback.
*
Expand Down
9 changes: 9 additions & 0 deletions theme/templates/elasticsearch_results_header.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>
<span>
<b><?php print number_format($total) ?> results found</b>
<?php if (isset($time)): ?>
(<?php print number_format($time, 2) ?> seconds)
<?php endif; ?>
</span>
<span style="float: right">Page <?php print $page ?> of <?php print $pages ?></span>
</p>
16 changes: 0 additions & 16 deletions tripal_elasticsearch.api.inc
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,6 @@ function tripal_elasticsearch_get_website_search_result_table($search_results, $
$row = new stdClass();
$row->title = $item->title;
if (isset($item->nid)) {
// $row = '<h3>' . l($item->title, 'node/' . $item->nid) . '</h3>';
// if ($base_url) {
// $row = '<h3><a href="' . $base_url . '/node/' . $item->nid . '">' . $item->title . '</a></h3>';
// }

if ($base_url) {
$row->url = "{$base_url}/node/{$item->nid}";
Expand All @@ -251,11 +247,6 @@ function tripal_elasticsearch_get_website_search_result_table($search_results, $
}
}
else {
// $row = '<h3>' . l($item->title, 'bio_data/' . $item->entity_id) . '</h3>';
// if ($base_url) {
// $row = '<h3><a href="' . $base_url . '/bio_data/' . $item->entity_id . '">' . $item->title . '</a></h3>';
// }

if ($base_url) {
$row->url = "{$base_url}/bio_data/{$item->entity_id}";
}
Expand Down Expand Up @@ -285,13 +276,6 @@ function tripal_elasticsearch_get_website_search_result_table($search_results, $
$rows[] = $row;
}

// $output = theme('table', [
// 'header' => [],
// 'rows' => $rows,
// 'attributes' => [
// 'id' => 'es-search-results-table',
// ],
// ]);
$output = theme('elasticsearch_results', [
'rows' => $rows,
'base_url' => $base_url
Expand Down
11 changes: 11 additions & 0 deletions tripal_elasticsearch.module
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,17 @@ function tripal_elasticsearch_theme($existing, $type, $theme, $path) {
'message' => ''
]
],
'elasticsearch_results_header' => [
'template' => 'elasticsearch_results_header',
'render element' => 'content',
'path' => "$path/theme/templates",
'variables' => [
// Current page
'page' => 1,
'total' => 0,
'pages' => 1
]
],
'elasticsearch_results' => [
'template' => 'elasticsearch_results',
// 'render element' => 'elements',
Expand Down

0 comments on commit 9c196ef

Please sign in to comment.