From 87670eb07d1b0e82e0a8b4c1f9b9d20e3cafdb42 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 10 Feb 2016 22:02:11 +0100 Subject: [PATCH] add date sorting --- data/templates/search/sidebar.htm | 9 +++++++++ src/phinde/Elasticsearch.php | 12 ++++++++---- 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: +
+ Relevance + Date +
+
+
+{% 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, ) ); ?> -- 2.30.2