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 7977635eb0a7d415281fe9ed4da28cee410459eb..e34cd15225ea71d9ac32877f4b7e9086720ef7b0 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 1e6c66db73ba558fd96eb530086f771f3a66db75..ca71aeda44448949cf78866d735e2ac6e812a2fe 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 d1e83ec4cf4eb4a1bb6e695b834a901e9cb2927e..617c69f69f81492bb26027c34194d442a38a35f1 100644 (file)
                 "tags": {
                     "type": "string",
                     "boost": 1.5
+                },
+                "crdate": {
+                    "type": "date"
+                },
+                "modate": {
+                    "type": "date"
                 }
             }
         }
index 96a769bc18319386eeec8ceef6003980f80220df..40358611edb1cb3dc69e929cd13e6fe141869991 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(