X-Git-Url: https://git.cweiske.de/phinde.git/blobdiff_plain/226508cd8d3e8c147ad314a0de483e08be71c254..7aba8a17bae437bfba2cb6c6496f750d014f3636:/src/phinde/Elasticsearch.php diff --git a/src/phinde/Elasticsearch.php b/src/phinde/Elasticsearch.php index 4bc4637..c437036 100644 --- a/src/phinde/Elasticsearch.php +++ b/src/phinde/Elasticsearch.php @@ -68,8 +68,79 @@ 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 + ); + } + + $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 { + $sortCfg = array(); + } + + $contentMatchSize = 100; + if ($GLOBALS['phinde']['showFullContent']) { + $contentMatchSize = 999999; + } + $r = new Elasticsearch_Request( $this->baseUrl . 'document/_search', \HTTP_Request2::METHOD_GET @@ -83,19 +154,28 @@ class Elasticsearch ), 'query' => array( 'bool' => array( - 'must' => array( - array( - 'query_string' => array( - 'default_field' => '_all', - 'query' => $query - ) - ), - array( - 'term' => array( - 'status' => 'indexed' - ) - ), - ) + 'must' => $qMust + ) + ), + 'highlight' => array( + 'pre_tags' => array(''), + '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, + 'fragment_size' => $contentMatchSize, + 'no_match_size' => $contentMatchSize, + ), ) ), 'aggregations' => array( @@ -122,9 +202,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 +211,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());