Skip to content

Commit 9c196ef

Browse files
committed
Support synonyms better
1 parent cd1fd97 commit 9c196ef

File tree

7 files changed

+110
-84
lines changed

7 files changed

+110
-84
lines changed

includes/Common/Instance.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,16 @@ public function setIndexParams($index_name, $shards = 5, $replicas = 0, $tokeniz
296296
'number_of_replicas' => $replicas,
297297
'analysis' => [
298298
'analyzer' => [
299-
$index_name => [
300-
'type' => 'custom',
301-
'tokenizer' => $tokenizer,
302-
'filter' => array_keys($filters),
303-
],
299+
//$index_name => [
300+
// 'type' => 'custom',
301+
// 'tokenizer' => $tokenizer,
302+
// 'filter' => array_keys($filters),
303+
//],
304304
'synonym' => [
305305
'tokenizer' => 'whitespace',
306-
'filter' => [
307-
'synonym',
308-
'lowercase'
309-
],
306+
'filter' => array_unique(
307+
['synonym', 'lowercase'] + array_keys($filters)
308+
),
310309
],
311310
],
312311
'filter' => [
@@ -315,6 +314,10 @@ public function setIndexParams($index_name, $shards = 5, $replicas = 0, $tokeniz
315314
'format' => 'wordnet',
316315
'synonyms_path' => 'analysis/wn_s.pl',
317316
],
317+
'stemmer' => [
318+
'type' => 'stemmer',
319+
'language' => 'english',
320+
],
318321
],
319322
],
320323
'max_result_window' => 1000000,

includes/Models/Model.php

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
*/
1111
class Model{
1212

13-
/**
14-
* @var array
15-
*/
16-
protected $attributes = [];
17-
1813
/**
1914
* The ES instance.
2015
*
@@ -362,46 +357,35 @@ public function search() {
362357
}
363358

364359
/**
365-
* Get query paramaters.
360+
* Paginate the results.
361+
*
362+
* @param int $per_page
366363
*
367364
* @return array
368365
* @throws \Exception
369366
*/
370-
public function getQuery() {
371-
return $this->builder->build();
367+
public function paginate($per_page = 10) {
368+
$start = microtime(true);
369+
$results = $this->search();
370+
$total = $results['hits']['total'];
371+
$current_page = pager_default_initialize($total, $per_page);
372+
return [
373+
'results' => $results,
374+
'total' => $total,
375+
'page' => $current_page + 1,
376+
'pages' => ceil($total / $per_page),
377+
'pager' => theme('pager', ['quantity', $total]),
378+
'time' => microtime(true) - $start
379+
];
372380
}
373381

374382
/**
375-
* Get an attribute.
376-
*
377-
* @param string $name
378-
* The name of attribute.
379-
*
380-
* @return mixed
381-
* The value of the attribute if it exists.
382-
*/
383-
public function __get($name) {
384-
if (array_key_exists($name, $this->attributes)) {
385-
return $this->attributes[$name];
386-
}
387-
388-
if (method_exists(static::class, $name)) {
389-
return;
390-
}
391-
392-
return $this->{$name};
393-
}
394-
395-
/**
396-
* Check if an attribute isset.
397-
*
398-
* @param string $name
399-
* The name of the attribute.
383+
* Get query paramaters.
400384
*
401-
* @return bool
402-
* Whether the attribute has been set.
385+
* @return array
386+
* @throws \Exception
403387
*/
404-
public function __isset($name) {
405-
return isset($this->attributes[$name]);
388+
public function getQuery() {
389+
return $this->builder->build();
406390
}
407391
}

includes/Query/SimpleQueryClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public function multiMatch(array $fields, $value) {
6262
'fields' => $fields,
6363
'query' => $value,
6464
'lenient' => TRUE,
65-
//'type' => 'phrase',
6665
'analyzer' => 'synonym',
66+
//'type' => 'phrase',
6767
'fuzziness' => 'AUTO',
6868
],
6969
];

includes/tripal_elasticsearch.advanced_search.form.inc

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {
6464

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

67-
if (isset($_GET['term']) && !empty($_GET['term'])) {
68-
$form['wrapper']['results'] = [
69-
'#markup' => tripal_elasticsearch_advanced_search_results($_GET),
70-
];
71-
}
67+
//if (isset($_GET['term']) && !empty($_GET['term'])) {
68+
$form['wrapper']['results'] = [
69+
'#markup' => tripal_elasticsearch_advanced_search_results($_GET),
70+
];
71+
//}
7272

7373
return $form;
7474
}
@@ -79,26 +79,18 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {
7979
* @return string
8080
*/
8181
function tripal_elasticsearch_advanced_search_results(array $values) {
82-
$fields = ['content.*', 'title'];
83-
84-
if (isset($_GET['field']) && !empty($_GET['field'])) {
85-
$fields = ["content.{$_GET['field']}"];
82+
if (empty($values['category']) && empty($values['term'])) {
83+
return '';
8684
}
8785

8886
try {
8987
$instance = new \ES\Common\Instance();
90-
$model = new \ES\Models\Model();
91-
$model->setIndexName('entities');
92-
$model->where($fields, trim($values['term']));
93-
94-
$category = trim($values['category'] ?? '');
95-
if (!empty($category)) {
96-
$model->where('bundle_label', "\"$category\"");
88+
$results = tripal_elasticsearch_perform_advanced_search($values, 15);
89+
if($results['total'] == 0 && !empty($values['term'])) {
90+
$values['term'] = strtolower($values['term']);
91+
$results = tripal_elasticsearch_perform_advanced_search($values, 15);
9792
}
98-
99-
$model->highlight(['content.*', 'title']);
100-
$data = $model->search();
101-
$hits = $instance->formatHits($data);
93+
$hits = $instance->formatHits($results['results']);
10294

10395
if (count($hits) === 0) {
10496
return theme(
@@ -109,7 +101,16 @@ function tripal_elasticsearch_advanced_search_results(array $values) {
109101
);
110102
}
111103

112-
return tripal_elasticsearch_get_website_search_result_table($hits);
104+
$content = theme('elasticsearch_results_header', [
105+
'page' => $results['page'],
106+
'total' => $results['total'],
107+
'pages' => $results['pages'],
108+
'time' => $results['time']
109+
]);
110+
$content .= tripal_elasticsearch_get_website_search_result_table($hits, false);
111+
$content .= $results['pager'];
112+
113+
return $content;
113114
} catch (Exception $exception) {
114115
watchdog(
115116
'tripal_elasticsearch',
@@ -123,6 +124,40 @@ function tripal_elasticsearch_advanced_search_results(array $values) {
123124
}
124125
}
125126

127+
/**
128+
* @param $values
129+
*
130+
* @return array
131+
* @throws \Exception
132+
*/
133+
function tripal_elasticsearch_perform_advanced_search($values, $per_page = NULL) {
134+
$fields = ['content.*', 'title'];
135+
136+
if (isset($values['field']) && !empty($values['field'])) {
137+
$fields = ["content.{$_GET['field']}"];
138+
}
139+
140+
$model = new \ES\Models\Model();
141+
$model->setIndexName('entities');
142+
143+
if (!empty($values['term'])) {
144+
$model->where($fields, trim($values['term']));
145+
}
146+
147+
$category = trim($values['category'] ?? '');
148+
if (!empty($category)) {
149+
$model->where('bundle_label', "\"$category\"");
150+
}
151+
152+
$model->highlight(['content.*', 'title']);
153+
154+
if (is_null($per_page)) {
155+
return $model->search();
156+
}
157+
158+
return $model->paginate($per_page);
159+
}
160+
126161
/**
127162
* AJAX Callback.
128163
*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>
2+
<span>
3+
<b><?php print number_format($total) ?> results found</b>
4+
<?php if (isset($time)): ?>
5+
(<?php print number_format($time, 2) ?> seconds)
6+
<?php endif; ?>
7+
</span>
8+
<span style="float: right">Page <?php print $page ?> of <?php print $pages ?></span>
9+
</p>

tripal_elasticsearch.api.inc

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,6 @@ function tripal_elasticsearch_get_website_search_result_table($search_results, $
238238
$row = new stdClass();
239239
$row->title = $item->title;
240240
if (isset($item->nid)) {
241-
// $row = '<h3>' . l($item->title, 'node/' . $item->nid) . '</h3>';
242-
// if ($base_url) {
243-
// $row = '<h3><a href="' . $base_url . '/node/' . $item->nid . '">' . $item->title . '</a></h3>';
244-
// }
245241

246242
if ($base_url) {
247243
$row->url = "{$base_url}/node/{$item->nid}";
@@ -251,11 +247,6 @@ function tripal_elasticsearch_get_website_search_result_table($search_results, $
251247
}
252248
}
253249
else {
254-
// $row = '<h3>' . l($item->title, 'bio_data/' . $item->entity_id) . '</h3>';
255-
// if ($base_url) {
256-
// $row = '<h3><a href="' . $base_url . '/bio_data/' . $item->entity_id . '">' . $item->title . '</a></h3>';
257-
// }
258-
259250
if ($base_url) {
260251
$row->url = "{$base_url}/bio_data/{$item->entity_id}";
261252
}
@@ -285,13 +276,6 @@ function tripal_elasticsearch_get_website_search_result_table($search_results, $
285276
$rows[] = $row;
286277
}
287278

288-
// $output = theme('table', [
289-
// 'header' => [],
290-
// 'rows' => $rows,
291-
// 'attributes' => [
292-
// 'id' => 'es-search-results-table',
293-
// ],
294-
// ]);
295279
$output = theme('elasticsearch_results', [
296280
'rows' => $rows,
297281
'base_url' => $base_url

tripal_elasticsearch.module

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,17 @@ function tripal_elasticsearch_theme($existing, $type, $theme, $path) {
388388
'message' => ''
389389
]
390390
],
391+
'elasticsearch_results_header' => [
392+
'template' => 'elasticsearch_results_header',
393+
'render element' => 'content',
394+
'path' => "$path/theme/templates",
395+
'variables' => [
396+
// Current page
397+
'page' => 1,
398+
'total' => 0,
399+
'pages' => 1
400+
]
401+
],
391402
'elasticsearch_results' => [
392403
'template' => 'elasticsearch_results',
393404
// 'render element' => 'elements',

0 commit comments

Comments
 (0)