Add support for modification date queries: "before:", "after:" and "date:"
authorChristian Weiske <cweiske@cweiske.de>
Tue, 30 Aug 2016 06:05:00 +0000 (08:05 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 30 Aug 2016 06:05:00 +0000 (08:05 +0200)
Resolves: #4

README.rst
bin/setup.php
data/elasticsearch-mapping.json
src/phinde/Elasticsearch.php

index 7977635..e34cd15 100644 (file)
@@ -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/``
index 1e6c66d..ca71aed 100755 (executable)
@@ -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
index d1e83ec..617c69f 100644 (file)
                 "tags": {
                     "type": "string",
                     "boost": 1.5
+                },
+                "crdate": {
+                    "type": "date"
+                },
+                "modate": {
+                    "type": "date"
                 }
             }
         }
index 96a769b..4035861 100644 (file)
@@ -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(