Răsfoiți Sursa

#2200 ElasticSearch: index reorganization to support latest elasticsearch versions

Alex Trofimov 7 ani în urmă
părinte
comite
51af2e261e

+ 23 - 11
modules/boonex/elasticsearch/classes/BxElsApi.php

@@ -16,6 +16,8 @@ define('BX_ELASTICSEARCH_LOG', 1);
  */
 class BxElsApi extends BxDol
 {
+    static $TYPE = 'doc';
+    
 	protected $_oModule;
 
     protected $_sError = false;
@@ -35,19 +37,29 @@ class BxElsApi extends BxDol
 
     public function searchSimple($sIndex, $sType, $sTerm) 
     {
-        $sQuery = "/$sIndex" . (!empty($sType) ? "/$sType" : "") . "/_search";
-        return $this->api($sQuery, array('query' => array('simple_query_string' => array('query' => $sTerm))));
+        if ($sType) {
+            $sQuery = "/{$sIndex}@{$sType}/" . self::$TYPE . "/_search";
+            return $this->api($sQuery, array('query' => array('simple_query_string' => array('query' => $sTerm))));
+        } 
+        else {
+            $this->log('ERROR: searchSimple - search without specifying particular module(type) isn\'t supported');
+        }
     }
 
     public function searchExtended($sIndex, $sType, $aCondition, $aSelection = array()) 
     {
-        $sQuery = "/$sIndex" . (!empty($sType) ? "/$sType" : "") . "/_search";
+        if ($sType) {
+            $sQuery = "/{$sIndex}@{$sType}/" . self::$TYPE . "/_search";
 
-        $aQuery = array('query' => $this->_prepareConditionsGroup($aCondition));
-        if(!empty($aSelection))
-            $aQuery = array_merge($aQuery, $aSelection);
+            $aQuery = array('query' => $this->_prepareConditionsGroup($aCondition));
+            if(!empty($aSelection))
+                $aQuery = array_merge($aQuery, $aSelection);
 
-        return $this->api($sQuery, $aQuery);
+            return $this->api($sQuery, $aQuery);
+        }
+        else {
+            $this->log('ERROR: searchExtended - search without specifying particular module(type) isn\'t supported');
+        }
     }
 
     protected function _prepareConditionsGroup($aGrp)
@@ -119,22 +131,22 @@ class BxElsApi extends BxDol
 
     public function getData($sIndex, $sType, $iContentId) 
     {
-        return $this->api("/$sIndex/$sType/$iContentId");
+        return $this->api("/{$sIndex}@{$sType}/" . self::$TYPE . "/$iContentId");
     }
 
     public function indexData($sIndex, $sType, $iContentId, $aData) 
     {
-        return $this->api("/$sIndex/$sType/$iContentId", $aData, 'put');
+        return $this->api("/{$sIndex}@{$sType}/" . self::$TYPE . "/$iContentId", $aData, 'put');
     }
 
     public function updateData($sIndex, $sType, $iContentId, $aData)
     {
-        return $this->api("/$sIndex/$sType/$iContentId", $aData, 'post');
+        return $this->api("/{$sIndex}@{$sType}/" . self::$TYPE . "/$iContentId", $aData, 'post');
     }
 
     public function deleteData($sIndex, $sType, $iContentId)
     {
-        return $this->api("/$sIndex/$sType/$iContentId", array(), 'delete');
+        return $this->api("/{$sIndex}@{$sType}/" . self::$TYPE . "/$iContentId", array(), 'delete');
     }
 
     public function getErrorMsg() 

+ 19 - 2
modules/boonex/elasticsearch/classes/BxElsModule.php

@@ -21,13 +21,30 @@ class BxElsModule extends BxBaseModGeneralModule
         $this->_oApi = new BxElsApi($this);
     }
 
-    public function actionDebug($sType = '', $sIndex = '')
+    public function actionDeleteOldIndex()
+    {
+        if (!isAdmin()) {
+            BxDolTemplate::getInstance()->displayAccessDenied();
+            exit;
+        }
+
+        $mixed = $this->_oApi->api('/' . $this->_oConfig->getIndex(), array(), 'delete');
+
+        echo '<pre>';
+        if (null === $mixed)
+            echo $this->_oApi->getErrorMsg();
+        else
+            var_dump($mixed);
+        echo '</pre>';
+    }
+
+    public function actionDebug($sIndex = '')
     {
         if(empty($sIndex))
             $sIndex = $this->_oConfig->getIndex();
 
         // search all
-        $mixed = $this->_oApi->api('/' . $sIndex . (!empty($sType) ? '/' . $sType : '') . '/_search?q=*');
+        $mixed = $this->_oApi->api('/' . $sIndex . '@bx_posts/doc/_search?q=*');
         // $mixed = $this->_oApi->api('/testdata/_search', ['query' => ['match_all' => (object)[]]]);
 
         // search for the term

+ 10 - 2
modules/boonex/elasticsearch/classes/BxElsSearch.php

@@ -59,8 +59,16 @@ class BxElsSearch extends BxTemplSearch
                 continue;
 
             $sContent = '';
-            foreach ($aResults['hits'] as $aResult)
-                $sContent .= BxDolContentInfo::getObjectInstance($aResult['_type'])->getContentSearchResultUnit($aResult['_id'], $sUnitTemplate);
+            foreach ($aResults['hits'] as $aResult) {
+                if (!isset($aResult['_index']))
+                    continue;
+                list ($sHost, $sObject) = explode('@', $aResult['_index']);
+                if (empty($sObject))
+                    continue;
+                if (!($oContentInfo = BxDolContentInfo::getObjectInstance($sObject)))
+                    continue;
+                $sContent .= $oContentInfo->getContentSearchResultUnit($aResult['_id'], $sUnitTemplate);
+            }
 
             $sResult .= $this->_oModule->_oTemplate->parseHtmlByName('search_results_block.html', array(
                 'id' => $aValue['id'],