Support "nick:cweiske" search syntax as alias for "author.name"
[phinde.git] / src / phinde / Elasticsearch.php
index 4bc4637440e9a103f39a5e086bde7789612e1b07..96a769bc18319386eeec8ceef6003980f80220df 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());