From 59f931647a2b4a13be20ba8f2baa4ec93e334ee5 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 30 Aug 2016 08:05:00 +0200 Subject: [PATCH] Add support for modification date queries: "before:", "after:" and "date:" Resolves: #4 --- README.rst | 5 +++ bin/setup.php | 5 ++- data/elasticsearch-mapping.json | 6 +++ src/phinde/Elasticsearch.php | 66 ++++++++++++++++++++++++++------- 4 files changed, 67 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index 7977635..e34cd15 100644 --- a/README.rst +++ b/README.rst @@ -19,6 +19,11 @@ Features - ``foo OR bar`` - ``title:foo`` searches for ``foo`` only in the page title - Facets for tag, domain, language and type +- Date search: + + - ``before:2016-08-30`` - modification date before that day + - ``after:2016-08-30`` - modified after that day + - ``date::2016-08-30`` - exact modification day match - Site search - Query: ``foo bar site:example.org/dir/`` diff --git a/bin/setup.php b/bin/setup.php index 1e6c66d..ca71aed 100755 --- a/bin/setup.php +++ b/bin/setup.php @@ -1,7 +1,10 @@ #!/usr/bin/env php array( + 'modate' => array( + 'gt' => $dateAfter . '||/d', + ) + ) + ); + } + if (preg_match('#before:([^ ]+)#', $query, $matches)) { + $dateBefore = $matches[1]; + $query = trim(str_replace($matches[0], '', $query)); + $qMust[] = array( + 'range' => array( + 'modate' => array( + 'lt' => $dateBefore . '||/d', + ) + ) + ); + } + if (preg_match('#date:([^ ]+)#', $query, $matches)) { + $dateExact = $matches[1]; + $query = trim(str_replace($matches[0], '', $query)); + $qMust[] = array( + 'range' => array( + 'modate' => array( + 'gte' => $dateExact . '||/d', + 'lte' => $dateExact . '||/d', + ) + ) + ); + } + + $qMust[] = array( + 'query_string' => array( + 'default_field' => '_all', + 'default_operator' => 'AND', + 'query' => $query + ) + ); + $qMust[] = array( + 'term' => array( + 'status' => 'indexed' + ) + ); + if ($sort == 'date') { $sortCfg = array('modate' => array('order' => 'desc')); } else { @@ -98,20 +149,7 @@ class Elasticsearch ), 'query' => array( 'bool' => array( - 'must' => array( - array( - 'query_string' => array( - 'default_field' => '_all', - 'default_operator' => 'AND', - 'query' => $query - ) - ), - array( - 'term' => array( - 'status' => 'indexed' - ) - ), - ) + 'must' => $qMust ) ), 'highlight' => array( -- 2.30.2