Support "nick:cweiske" search syntax as alias for "author.name"
[phinde.git] / src / phinde / Elasticsearch.php
index 4bc4637..96a769b 100644 (file)
@@ -68,8 +68,23 @@ class Elasticsearch
         $r->send();
     }
 
-    public function search($query, $filters, $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
                             )
                         ),
@@ -98,6 +114,25 @@ class Elasticsearch
                     )
                 )
             ),
+            'highlight' => array(
+                'pre_tags' => array('<em class="hl">'),
+                'order' => 'score',
+                'encoder' => 'html',
+                'fields' => array(
+                    'title' => array(
+                        'require_field_match' => false,
+                        'number_of_fragments' => 0,
+                    ),
+                    'url' => array(
+                        'require_field_match' => false,
+                        'number_of_fragments' => 0,
+                    ),
+                    'text' => array(
+                        'require_field_match' => false,
+                        'number_of_fragments' => 1,
+                    ),
+                )
+            ),
             'aggregations' => array(
                 'tags' => array(
                     'terms' => array(
@@ -122,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(
@@ -133,11 +166,20 @@ class Elasticsearch
                 )
             );
         }
+        if ($site != '') {
+            $doc['query']['bool']['must'][] = array(
+                'prefix' => array(
+                    'schemalessUrl' => array(
+                        'value' => $site
+                    )
+                )
+            );
+        }
 
         //unset($doc['_source']);
 
         //ini_set('xdebug.var_display_max_depth', 10);
-        //return json_decode(json_encode($doc));
+        //echo json_encode($doc);die();
         $r->setBody(json_encode($doc));
         $res = $r->send();
         return json_decode($res->getBody());