Support "nick:cweiske" search syntax as alias for "author.name"
[phinde.git] / src / phinde / Elasticsearch.php
index 735b64f..96a769b 100644 (file)
@@ -68,8 +68,23 @@ class Elasticsearch
         $r->send();
     }
 
-    public function search($query, $filters, $site, $page, $perPage)
+    public function search($query, $filters, $site, $page, $perPage, $sort)
     {
+        if (preg_match('#nick:([^ ]*)#', $query, $matches)) {
+            $authorName = $matches[1];
+            $query = str_replace(
+                'nick:' . $authorName,
+                'author.name:' . $authorName,
+                $query
+            );
+        }
+
+        if ($sort == 'date') {
+            $sortCfg = array('modate' => array('order' => 'desc'));
+        } else {
+            $sortCfg = array();
+        }
+
         $r = new Elasticsearch_Request(
             $this->baseUrl . 'document/_search',
             \HTTP_Request2::METHOD_GET
@@ -87,6 +102,7 @@ class Elasticsearch
                         array(
                             'query_string' => array(
                                 'default_field' => '_all',
+                                'default_operator' => 'AND',
                                 'query' => $query
                             )
                         ),
@@ -101,6 +117,7 @@ class Elasticsearch
             'highlight' => array(
                 'pre_tags' => array('<em class="hl">'),
                 'order' => 'score',
+                'encoder' => 'html',
                 'fields' => array(
                     'title' => array(
                         'require_field_match' => false,
@@ -140,9 +157,7 @@ class Elasticsearch
             ),
             'from' => $page * $perPage,
             'size' => $perPage,
-            'sort' => array(
-                //array('modate' => array('order' => 'desc'))
-            )
+            'sort' => $sortCfg,
         );
         foreach ($filters as $type => $value) {
             $doc['query']['bool']['must'][] = array(