aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2016-08-30 08:05:00 +0200
committerChristian Weiske <cweiske@cweiske.de>2016-08-30 08:05:00 +0200
commit59f931647a2b4a13be20ba8f2baa4ec93e334ee5 (patch)
treecacf6c1e152772e74d616aeb753a94d56dc2ec7a
parent210a7ec82c46ed6e410f80be4b1149f5295b1306 (diff)
downloadphinde-59f931647a2b4a13be20ba8f2baa4ec93e334ee5.tar.gz
phinde-59f931647a2b4a13be20ba8f2baa4ec93e334ee5.zip
Add support for modification date queries: "before:", "after:" and "date:"
Resolves: #4
-rw-r--r--README.rst5
-rwxr-xr-xbin/setup.php5
-rw-r--r--data/elasticsearch-mapping.json6
-rw-r--r--src/phinde/Elasticsearch.php66
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
<?php
namespace phinde;
-//configure the elasticsearch index
+/**
+ * Configure the elasticsearch index.
+ * Throws away all data.
+ */
require_once __DIR__ . '/../src/init.php';
//delete old index
diff --git a/data/elasticsearch-mapping.json b/data/elasticsearch-mapping.json
index d1e83ec..617c69f 100644
--- a/data/elasticsearch-mapping.json
+++ b/data/elasticsearch-mapping.json
@@ -76,6 +76,12 @@
"tags": {
"type": "string",
"boost": 1.5
+ },
+ "crdate": {
+ "type": "date"
+ },
+ "modate": {
+ "type": "date"
}
}
}
diff --git a/src/phinde/Elasticsearch.php b/src/phinde/Elasticsearch.php
index 96a769b..4035861 100644
--- a/src/phinde/Elasticsearch.php
+++ b/src/phinde/Elasticsearch.php
@@ -79,6 +79,57 @@ class Elasticsearch
);
}
+ $qMust = array();//query parts for the MUST section
+
+ //modification date filters
+ if (preg_match('#after:([^ ]+)#', $query, $matches)) {
+ $dateAfter = $matches[1];
+ $query = trim(str_replace($matches[0], '', $query));
+ $qMust[] = array(
+ 'range' => 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(