diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2016-02-10 22:02:11 +0100 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2016-02-10 22:02:11 +0100 |
| commit | 87670eb07d1b0e82e0a8b4c1f9b9d20e3cafdb42 (patch) | |
| tree | 13352a7206dfedd73f5e1eceb2595bc5e2cf0cb3 | |
| parent | 5a5d2545fd262fa06e4ddc49cc675779b21f86c4 (diff) | |
| download | phinde-87670eb07d1b0e82e0a8b4c1f9b9d20e3cafdb42.tar.gz phinde-87670eb07d1b0e82e0a8b4c1f9b9d20e3cafdb42.zip | |
add date sorting
| -rw-r--r-- | data/templates/search/sidebar.htm | 9 | ||||
| -rw-r--r-- | src/phinde/Elasticsearch.php | 12 | ||||
| -rw-r--r-- | www/index.php | 20 |
3 files changed, 35 insertions, 6 deletions
diff --git a/data/templates/search/sidebar.htm b/data/templates/search/sidebar.htm index 9e36312..a48b6e8 100644 --- a/data/templates/search/sidebar.htm +++ b/data/templates/search/sidebar.htm @@ -1,3 +1,12 @@ +{% if hitcount > 1 %} + Sort by: +<div class="btn-group"> + <a class="btn{%if sort==""%} disabled btn-primary{%endif%}" href="{{urlSortRelevance}}">Relevance</a> + <a class="btn{%if sort=="date"%} disabled btn-primary{%endif%}" href="{{urlSortDate}}">Date</a> +</div> +<br/> +<br/> +{% endif %} {% set filters %} {% include 'search/sidebar-filter.htm' with {'type': 'tags', 'title': 'Tag'} %} {% include 'search/sidebar-filter.htm' with {'type': 'domain', 'title': 'Domain'} %} diff --git a/src/phinde/Elasticsearch.php b/src/phinde/Elasticsearch.php index 316f3da..310b63b 100644 --- a/src/phinde/Elasticsearch.php +++ b/src/phinde/Elasticsearch.php @@ -68,8 +68,14 @@ class Elasticsearch $r->send(); } - public function search($query, $filters, $site, $page, $perPage) + public function search($query, $filters, $site, $page, $perPage, $sort) { + if ($sort == 'date') { + $sortCfg = array('modate' => array('order' => 'desc')); + } else { + $sortCfg = array(); + } + $r = new Elasticsearch_Request( $this->baseUrl . 'document/_search', \HTTP_Request2::METHOD_GET @@ -142,9 +148,7 @@ class Elasticsearch ), 'from' => $page * $perPage, 'size' => $perPage, - 'sort' => array( - //array('modate' => array('order' => 'desc')) - ) + 'sort' => $sortCfg, ); foreach ($filters as $type => $value) { $doc['query']['bool']['must'][] = array( diff --git a/www/index.php b/www/index.php index 6f4ec8f..54d5da8 100644 --- a/www/index.php +++ b/www/index.php @@ -16,6 +16,7 @@ if (isset($_GET['page'])) { //PEAR Pager begins at 1 $page = (int)$_GET['page'] - 1; } + $perPage = 10;//$GLOBALS['phinde']['perPage']; $site = null; $siteParam = false; @@ -34,6 +35,13 @@ if (preg_match('#site:([^ ]*)#', $query, $matches)) { $cleanQuery = $query; } +if (isset($_GET['sort']) && $_GET['sort'] == 'date') { + $sort = 'date'; + $baseLink .= '&sort=date'; +} else { + $sort = ''; +} + $filters = array(); if (isset($_GET['filter'])) { $allowedFilter = array('domain', 'language', 'tags', 'term'); @@ -80,7 +88,7 @@ if (preg_match('#site:([^ ]*)#', $query, $matches)) { $timeBegin = microtime(true); $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']); -$res = $es->search($cleanQuery, $filters, $site, $page, $perPage); +$res = $es->search($cleanQuery, $filters, $site, $page, $perPage, $sort); $timeEnd = microtime(true); $pager = new Html_Pager( @@ -124,6 +132,11 @@ if ($site !== null) { $urlNoSite = null; } +$urlSortRelevance = buildLink( + str_replace('&sort=date', '', $baseLink), $filters, null, null +); +$urlSortDate = $urlSortRelevance . '&sort=date'; + render( 'search', array( @@ -137,7 +150,10 @@ render( 'hits' => $res->hits->hits, 'aggregations' => $res->aggregations, 'activeFilters' => $activeFilters, - 'pager' => $pager + 'pager' => $pager, + 'sort' => $sort, + 'urlSortRelevance' => $urlSortRelevance, + 'urlSortDate' => $urlSortDate, ) ); ?> |
