Przeglądaj źródła

#652 ElasticSearch: API

Alex Trofimov 9 lat temu
rodzic
commit
e6e1579bdc

+ 134 - 0
modules/boonex/elasticsearch/classes/BxElsApi.php

@@ -0,0 +1,134 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    HostingAPI Hosting API
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+define('BX_ELASTICSEARCH_LOG', 1);
+
+/**
+ * ElasticSearch API
+ */
+class BxElsApi extends BxDol
+{
+    protected $_sError = false;
+    protected $_iTimeout = 30;
+    protected $_sApiUrl = 'http://localhost:9200';
+
+    public function __construct()
+    {
+        $this->_sApiUrl = 'http://45.56.80.210:9200'; // TODO: use $this->_sApiUrl = getParam(''); 
+    }
+
+    public function indexData($sIndex, $sType, $iContentId, $aData) 
+    {
+        return $this->api("/$sIndex/$sType/$iContentId", $aData, 'put');
+    }
+
+    public function getErrorMsg() 
+    {
+        return $this->_sError;
+    }
+    
+    public function api($sAction, $aData = array(), $sMetod = 'get', $bJsonResponse = true) 
+    {
+        if (!$this->_sApiUrl) {
+            $this->_sError = 'Host isn\'t defined';
+            return null;
+        }
+
+        if (!$sAction) {
+            $this->_sError = 'Action isn\'t defined';
+            return null;
+        }        
+
+        $sUrl = bx_append_url_params($this->_sApiUrl . $sAction, array(
+            'format' => 'json',
+            'human' => false,
+        ));
+
+        $sHttpCode = 200;
+        $s = $this->curl($sUrl, $aData, $sMetod, $sHttpCode);
+
+        $this->log($s);
+
+        if ($sHttpCode >= 300 || $sHttpCode < 200) {
+            $this->_sError = 'Action('.$sAction.') returned status - ' . $sHttpCode;
+            return null;
+        }
+
+        // return JSON response
+        if ($s && $bJsonResponse) {
+            if (null === ($a = json_decode($s, true))) {
+                $this->_sError = 'Can not decode response';
+                return null;
+            }
+            return $a;
+        }
+    
+        return $s;
+    }
+
+    protected function curl ($sUrl, $aData = array(), $sMethod = 'get', &$sHttpCode = null, $aBasicAuth = array()) 
+    {
+        $rConnect = curl_init();
+
+        curl_setopt($rConnect, CURLOPT_USERAGENT, 'UNA ' . BX_DOL_VERSION);
+        curl_setopt($rConnect, CURLOPT_URL, $sUrl);
+        curl_setopt($rConnect, CURLOPT_HEADER, NULL === $sHttpCode ? false : true);
+        curl_setopt($rConnect, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($rConnect, CURLOPT_VERBOSE, 0);
+        curl_setopt($rConnect, CURLOPT_HEADER, 0);
+
+        curl_setopt($rConnect, CURLOPT_CONNECTTIMEOUT, $this->_iTimeout);
+        curl_setopt($rConnect, CURLOPT_TIMEOUT, $this->_iTimeout);
+
+        if (!ini_get('open_basedir'))
+            curl_setopt($rConnect, CURLOPT_FOLLOWLOCATION, 1);
+
+        if ($aBasicAuth)
+            curl_setopt($rConnect, CURLOPT_USERPWD, $aBasicAuth['user'] . ':' . $aBasicAuth['password']);
+
+        if ('get' != $sMethod)
+            curl_setopt($rConnect, CURLOPT_CUSTOMREQUEST, strtoupper($sMethod));
+
+        if ('post' == $sMethod)
+            curl_setopt($rConnect, CURLOPT_POST, true);
+
+        if ($aData) {
+            curl_setopt($rConnect, CURLOPT_POSTFIELDS, json_encode($aData));
+            curl_setopt($rConnect, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
+        }
+
+        $sResult = curl_exec($rConnect);
+
+        if (NULL !== $sHttpCode)
+            $sHttpCode = curl_getinfo($rConnect, CURLINFO_HTTP_CODE);
+
+        curl_close($rConnect);
+
+        return $sResult;
+    }
+
+    public function log ($mixed, $sProvider = '')
+    {
+        if (!defined('BX_ELASTICSEARCH_LOG') || !constant('BX_ELASTICSEARCH_LOG'))
+            return;
+        $fn = BX_DIRECTORY_PATH_ROOT . "logs/elasticsearch.log";
+        $f = @fopen ($fn, 'a');
+        if (!$f)
+            return;
+        if (is_array($mixed))
+            fwrite ($f, date(DATE_RFC822) . "\n" . print_r($mixed, true) . "\n");
+        else
+            fwrite ($f, date(DATE_RFC822) . "\t" . $mixed . "\n");
+        fclose ($f);
+    }
+}
+
+/** @} */

+ 43 - 0
modules/boonex/elasticsearch/classes/BxElsModule.php

@@ -16,6 +16,49 @@ class BxElsModule extends BxBaseModGeneralModule
         parent::__construct($aModule);
     }
 
+    public function actionDebug()
+    {
+        bx_import('Api', $this->_aModule);
+        $o = new BxElsApi();
+
+        // search all
+        // $mixed = $o->api('/testdata/_search?q=*'); 
+        // $mixed = $o->api('/testdata/_search', ['query' => ['match_all' => (object)[]]]);
+
+        // search for the term
+        // $mixed = $o->api('/testdata/_search', ['query' => ['simple_query_string' => ['query' => 'palm']]]);
+        // $mixed = $o->api('/testdata/_search?q=palm');
+
+        // search in a field
+        // $mixed = $o->api('/testdata/_search', ['query' => ['simple_query_string' => ['query' => 'palm', 'fields' => ['name']]]]);
+
+        // delete indexed doc
+        // $mixed = $o->api('/testdata/trees/23', [], 'delete'); 
+
+        // get indexed doc
+        // $mixed = $o->api('/testdata/trees/23'); 
+
+        // index a doc
+        // $mixed = $o->indexData('testdata', 'trees', 23, ['name' => 'apple tree', 'fruits' => 'apples']); 
+        // $mixed = $o->indexData('testdata', 'trees', 24, ['name' => 'palm', 'fruits' => 'coconuts']);
+
+        // get indexes
+        // $mixed = $o->api('/_cat/indices'); 
+
+        // add index
+        // $mixed = $o->api('/testdata', [], 'PUT'); 
+
+        // test
+        $mixed = $o->api('/_cat/health'); 
+
+        echo '<pre>';
+        if (null === $mixed)
+            echo $o->getErrorMsg();
+        else
+            var_dump($mixed);
+        echo '</pre>';
+    }
+    
     public function serviceAdd($sString, $iLanguageId = 0, $iOrig = 1)
     {
     }