From 7b4425b096fa8c18d0db9fd9b1ae96d63ee8af55 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 1 Feb 2016 20:18:59 +0100 Subject: first kinda working version --- src/Elasticsearch.php | 138 ++++++++++++++++++++++++++++++++++++++++++ src/Elasticsearch/Request.php | 35 +++++++++++ src/Html/Pager.php | 66 ++++++++++++++++++++ src/functions.php | 11 ++++ 4 files changed, 250 insertions(+) create mode 100644 src/Elasticsearch.php create mode 100644 src/Elasticsearch/Request.php create mode 100644 src/Html/Pager.php create mode 100644 src/functions.php (limited to 'src') diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php new file mode 100644 index 0000000..b3f3067 --- /dev/null +++ b/src/Elasticsearch.php @@ -0,0 +1,138 @@ +baseUrl = $baseUrl; + } + + /** + * @link https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html + */ + public function isKnown($url) + { + $r = new Elasticsearch_Request( + $this->baseUrl . 'document/_search/exists', + \HTTP_Request2::METHOD_GET + ); + $r->allow404 = true; + $r->setBody( + json_encode( + array( + 'query' => array( + 'filtered' => array( + 'filter' => array( + 'term' => array( + 'url' => $url + ) + ) + ) + ) + ) + ) + ); + $res = json_decode($r->send()->getBody()); + return $res->exists; + } + + public function get($url) + { + $r = new Elasticsearch_Request( + $this->baseUrl . 'document/' . rawurlencode($url), + \HTTP_Request2::METHOD_GET + ); + $r->allow404 = true; + $res = $r->send(); + if ($res->getStatus() != 200) { + return null; + } + $d = json_decode($res->getBody()); + return $d->_source; + } + + public function markQueued($url) + { + $r = new Elasticsearch_Request( + $this->baseUrl . 'document/' . rawurlencode($url), + \HTTP_Request2::METHOD_PUT + ); + $doc = array( + 'status' => 'queued', + 'url' => $url + ); + $r->setBody(json_encode($doc)); + $r->send(); + } + + public function search($query, $page, $perPage) + { + $r = new Elasticsearch_Request( + $this->baseUrl . 'document/_search', + \HTTP_Request2::METHOD_GET + ); + $doc = array( + '_source' => array( + 'url', + 'title', + 'author', + 'modate', + ), + 'query' => array( + 'bool' => array( + 'must' => array( + array( + 'query_string' => array( + 'default_field' => '_all', + 'query' => $query + ) + ), + array( + 'term' => array( + 'status' => 'indexed' + ) + ) + ) + ) + ), + 'aggregations' => array( + 'tags' => array( + 'terms' => array( + 'field' => 'tags' + ) + ), + 'language' => array( + 'terms' => array( + 'field' => 'language' + ) + ), + 'domain' => array( + 'terms' => array( + 'field' => 'domain' + ) + ), + 'type' => array( + 'terms' => array( + 'field' => 'type' + ) + ) + ), + 'from' => $page * $perPage, + 'size' => $perPage, + 'sort' => array( + //array('modate' => array('order' => 'desc')) + ) + ); + //unset($doc['_source']); + + //ini_set('xdebug.var_display_max_depth', 10); + //return json_decode(json_encode($doc)); + $r->setBody(json_encode($doc)); + $res = $r->send(); + return json_decode($res->getBody()); + } +} +?> diff --git a/src/Elasticsearch/Request.php b/src/Elasticsearch/Request.php new file mode 100644 index 0000000..7bb6add --- /dev/null +++ b/src/Elasticsearch/Request.php @@ -0,0 +1,35 @@ +getStatus() / 100); + if ($mainCode === 2) { + return $res; + } + + if ($this->allow404 && $res->getStatus() == 404) { + return $res; + } + $js = json_decode($res->getBody()); + if (isset($js->error)) { + $error = json_encode($js->error); + } else { + $error = $res->getBody(); + } + + throw new \Exception( + 'Error in elasticsearch communication at ' + . $this->getMethod() . ' ' . (string) $this->getUrl() + . ' (status code ' . $res->getStatus() . '): ' + . $error + ); + } +} + +?> diff --git a/src/Html/Pager.php b/src/Html/Pager.php new file mode 100644 index 0000000..a14a53d --- /dev/null +++ b/src/Html/Pager.php @@ -0,0 +1,66 @@ +pager = \Pager::factory( + array( + 'mode' => 'Sliding', + 'perPage' => $perPage, + 'delta' => 2, + 'totalItems' => $itemCount, + 'currentPage' => $currentPage, + 'urlVar' => 'page', + 'append' => $append, + 'path' => '', + 'fileName' => $filename, + 'separator' => '###', + 'spacesBeforeSeparator' => 0, + 'spacesAfterSeparator' => 0, + 'curPageSpanPre' => '', + 'curPageSpanPost' => '', + 'firstPagePre' => '', + 'firstPageText' => 'first', + 'firstPagePost' => '', + 'lastPagePre' => '', + 'lastPageText' => 'last', + 'lastPagePost' => '', + 'prevImg' => '« prev', + 'nextImg' => 'next »', + ) + ); + } + + + public function getLinks() + { + $arLinks = $this->pager->getLinks(); + $arLinks['pages'] = explode('###', $arLinks['pages']); + return $arLinks; + } + + public function numPages() + { + return $this->pager->numPages(); + } +} + +?> diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 0000000..fd91360 --- /dev/null +++ b/src/functions.php @@ -0,0 +1,11 @@ + -- cgit v1.2.3