status page
[phinde.git] / src / phinde / Elasticsearch.php
index 2887beb..069cf1f 100644 (file)
@@ -10,10 +10,15 @@ class Elasticsearch
         $this->baseUrl = $baseUrl;
     }
 
+    public static function getDocId($url)
+    {
+        return hash('sha256', $url);
+    }
+
     public function isKnown($url)
     {
         $r = new Elasticsearch_Request(
-            $this->baseUrl . 'document/' . rawurlencode($url),
+            $this->baseUrl . 'document/' . static::getDocId($url),
             \HTTP_Request2::METHOD_HEAD
         );
         $r->allow404 = true;
@@ -24,7 +29,7 @@ class Elasticsearch
     public function get($url)
     {
         $r = new Elasticsearch_Request(
-            $this->baseUrl . 'document/' . rawurlencode($url),
+            $this->baseUrl . 'document/' . static::getDocId($url),
             \HTTP_Request2::METHOD_GET
         );
         $r->allow404 = true;
@@ -39,17 +44,30 @@ class Elasticsearch
     public function markQueued($url)
     {
         $r = new Elasticsearch_Request(
-            $this->baseUrl . 'document/' . rawurlencode($url),
+            $this->baseUrl . 'document/' . static::getDocId($url),
             \HTTP_Request2::METHOD_PUT
         );
-        $doc = array(
-            'status' => 'queued',
-            'url' => $url
+        $doc = (object) array(
+            'url' => $url,
+            'status' => (object) array(
+                'processed' => null,
+                'findable'  => false,
+            )
         );
         $r->setBody(json_encode($doc));
         $r->send();
     }
 
+    public function countDocuments()
+    {
+        $r = new Elasticsearch_Request(
+            $this->baseUrl . 'document/_count',
+            \HTTP_Request2::METHOD_GET
+        );
+        $res = $r->send();
+        return json_decode($res->getBody())->count;
+    }
+
     public function search($query, $filters, $site, $page, $perPage, $sort)
     {
         if (preg_match_all('#nick:([^ ]*)#', $query, $matches)) {
@@ -109,12 +127,12 @@ class Elasticsearch
         );
         $qMust[] = array(
             'term' => array(
-                'status' => 'indexed'
+                'status.findable' => true
             )
         );
 
         if ($sort == 'date') {
-            $sortCfg = array('modate' => array('order' => 'desc'));
+            $sortCfg = array('status.modate' => array('order' => 'desc'));
         } else {
             $sortCfg = array();
         }
@@ -133,7 +151,7 @@ class Elasticsearch
                 'url',
                 'title',
                 'author',
-                'modate',
+                'status.modate',
             ),
             'query' => array(
                 'bool' => array(