BxElsModule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup ElasticSearch ElasticSearch
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxElsModule extends BxBaseModGeneralModule
  12. {
  13. protected $_oApi;
  14. function __construct(&$aModule)
  15. {
  16. parent::__construct($aModule);
  17. bx_import('Api', $aModule);
  18. $this->_oApi = new BxElsApi($this);
  19. }
  20. public function actionDebug($sType = '', $sIndex = '')
  21. {
  22. bx_import('Api', $this->_aModule);
  23. $o = new BxElsApi();
  24. if(empty($sIndex))
  25. $sIndex = $this->_oConfig->getIndex();
  26. // search all
  27. $mixed = $o->api('/' . $sIndex . (!empty($sType) ? '/' . $sType : '') . '/_search?q=*');
  28. // $mixed = $o->api('/testdata/_search', ['query' => ['match_all' => (object)[]]]);
  29. // search for the term
  30. // $mixed = $o->api('/testdata/_search', ['query' => ['simple_query_string' => ['query' => 'palm']]]);
  31. // $mixed = $o->api('/testdata/_search?q=palm');
  32. // search in a field
  33. // $mixed = $o->api('/testdata/_search', ['query' => ['simple_query_string' => ['query' => 'palm', 'fields' => ['name']]]]);
  34. // compound searchs
  35. /*
  36. $mixed = $o->api('/testdata/_search', array(
  37. 'query' => array(
  38. 'bool' => array(
  39. 'filter' => array(
  40. array('term' => array('featured' => '0')),
  41. array('term' => array('allow_view_to' => '3')),
  42. array(
  43. 'dis_max' => array(
  44. 'queries' => array(
  45. array('term' => array('title' => 'uupdd')),
  46. array('term' => array('text' => 'uupdd'))
  47. )
  48. ),
  49. )
  50. ),
  51. ))
  52. ));
  53. $mixed = $this->api('/testdata/_search', array(
  54. 'query' => array(
  55. 'dis_max' => array(
  56. 'queries' => array(
  57. array('simple_query_string' => array('query' => 'uupd')),
  58. array('term' => array('title' => 'uupdd')),
  59. array('term' => array('text' => 'uupdd')),
  60. array(
  61. 'bool' => array(
  62. 'filter' => array(
  63. array('term' => array('featured' => '0')),
  64. array('term' => array('allow_view_to' => '3')),
  65. )
  66. ),
  67. )
  68. ),
  69. ))
  70. ));
  71. */
  72. // delete index
  73. //$mixed = $o->api('/' . $this->_oConfig->getIndex(), array(), 'delete');
  74. // delete indexed doc
  75. //$mixed = $o->api('/' . $this->_oConfig->getIndex() . '/trees/23', [], 'delete');
  76. // get indexed doc
  77. // $mixed = $o->api('/testdata/trees/23');
  78. // index a doc
  79. // $mixed = $o->indexData('testdata', 'trees', 23, ['name' => 'apple tree', 'fruits' => 'apples']);
  80. // $mixed = $o->indexData('testdata', 'trees', 24, ['name' => 'palm', 'fruits' => 'coconuts']);
  81. // get indexes
  82. // $mixed = $o->api('/_cat/indices');
  83. // add index
  84. // $mixed = $o->api('/testdata', [], 'PUT');
  85. // test
  86. //$mixed = $o->api('/_cat/health');
  87. // mapping put
  88. //$mixed = $o->api('/' . $sIndex . '/_mapping/bx_forum', array('properties' => array('featured' => array('type' => 'long'))));
  89. // mapping get
  90. //$mixed = $o->api('/' . $sIndex . '/_mapping' . (!empty($sType) ? '/' . $sType : ''));
  91. echo '<pre>';
  92. if (null === $mixed)
  93. echo $o->getErrorMsg();
  94. else
  95. var_dump($mixed);
  96. echo '</pre>';
  97. }
  98. public function serviceIsConfigured()
  99. {
  100. $CNF = &$this->_oConfig->CNF;
  101. return isset($CNF['PARAM_API_URL']) && getParam($CNF['PARAM_API_URL']) != '';
  102. }
  103. public function serviceSearchSimple($sTerm, $sType = '', $sIndex = '')
  104. {
  105. if(empty($sIndex))
  106. $sIndex = $this->_oConfig->getIndex();
  107. $mixedResult = $this->_oApi->searchSimple($sIndex, $sType, $sTerm);
  108. if(!$mixedResult || !is_array($mixedResult))
  109. return false;
  110. return $mixedResult['hits'];
  111. }
  112. /*
  113. * Condition #1: keyword search
  114. * $aCondition = array('val' => 'test');
  115. *
  116. * Condition #2: `featured`='1'
  117. * $aCondition = array('fld' => 'featured', 'val' => 0);
  118. *
  119. * Condition #3: (`title`='test' OR `text`='test' OR (`featured`='1' AND `allow_view_to`='3'))
  120. * $aCondition = array('grp' => true, 'opr' => 'OR', 'cnds' => array(
  121. * array('fld' => 'title', 'val' => 'test'),
  122. * array('fld' => 'text', 'val' => 'test'),
  123. * array('grp' => true, 'opr' => 'AND', 'cnds' => array(
  124. * array('fld' => 'featured', 'val' => 1),
  125. * array('fld' => 'allow_view_to', 'val' => 3)
  126. * )),
  127. * array('simple_query_string' => array('query' => 'test')),
  128. * ));
  129. *
  130. * Condition #4: ((`title`='test' OR `text`='test') AND `featured`='1' AND `allow_view_to`='3')
  131. * $aCondition = array('grp' => true, 'opr' => 'AND', 'cnds' => array(
  132. * array('grp' => true, 'opr' => 'OR', 'cnds' => array(
  133. * array('fld' => 'title', 'val' => 'test'),
  134. * array('fld' => 'text', 'val' => 'test'),
  135. * )),
  136. * array('fld' => 'featured', 'val' => 0),
  137. * array('fld' => 'allow_view_to', 'val' => 3)
  138. * ));
  139. *
  140. */
  141. public function serviceSearchExtended($aCondition, $aSelection = array(), $sType = '', $sIndex = '')
  142. {
  143. if(empty($sIndex))
  144. $sIndex = $this->_oConfig->getIndex();
  145. $mixedResult = $this->_oApi->searchExtended($sIndex, $sType, $aCondition, $aSelection);
  146. if(!$mixedResult || !is_array($mixedResult))
  147. return false;
  148. return $mixedResult['hits'];
  149. }
  150. public function serviceGet($iContentId, $sType, $sIndex = '')
  151. {
  152. if(empty($sIndex))
  153. $sIndex = $this->_oConfig->getIndex();
  154. return $this->_oApi->getData($sIndex, $sType, $iContentId);
  155. }
  156. public function serviceAdd($iContentId, $mixedContentInfo, $sIndex = '')
  157. {
  158. if(is_array($mixedContentInfo)) {
  159. $sMethod = 'getObjectInstanceByAlert' . bx_gen_method_name($mixedContentInfo['alert_type']);
  160. $mixedContentInfo = BxDolContentInfo::$sMethod($mixedContentInfo['unit'], $mixedContentInfo['action']);
  161. }
  162. if(!($mixedContentInfo instanceof BxDolContentInfo))
  163. return false;
  164. $CNF = &$this->_oConfig->CNF;
  165. if(empty($sIndex))
  166. $sIndex = $this->_oConfig->getIndex();
  167. $aInfo = array(
  168. $CNF['FIELD_AUTHOR'] => $mixedContentInfo->getContentAuthor($iContentId),
  169. $CNF['FIELD_ADDED'] => $mixedContentInfo->getContentDateAdded($iContentId),
  170. $CNF['FIELD_CHANGED'] => $mixedContentInfo->getContentDateChanged($iContentId),
  171. $CNF['FIELD_LINK'] => $mixedContentInfo->getContentLink($iContentId),
  172. $CNF['FIELD_TITLE'] => $mixedContentInfo->getContentTitle($iContentId),
  173. $CNF['FIELD_TEXT'] => $mixedContentInfo->getContentText($iContentId),
  174. );
  175. $aInfo = array_merge($aInfo, $mixedContentInfo->getContentInfo($iContentId));
  176. $mixedResult = $this->_oApi->indexData($sIndex, $mixedContentInfo->getName(), $iContentId, $this->_prepareToIndex($aInfo));
  177. if($mixedResult === null)
  178. return false;
  179. return $mixedResult['result'] == 'created';
  180. }
  181. public function serviceUpdate($iContentId, $mixedContentInfo, $sIndex = '')
  182. {
  183. if(is_array($mixedContentInfo)) {
  184. $sMethod = 'getObjectInstanceByAlert' . bx_gen_method_name($mixedContentInfo['alert_type']);
  185. $mixedContentInfo = BxDolContentInfo::$sMethod($mixedContentInfo['unit'], $mixedContentInfo['action']);
  186. }
  187. if(!($mixedContentInfo instanceof BxDolContentInfo))
  188. return false;
  189. $CNF = &$this->_oConfig->CNF;
  190. if(empty($sIndex))
  191. $sIndex = $this->_oConfig->getIndex();
  192. $aInfo = array(
  193. $CNF['FIELD_AUTHOR'] => $mixedContentInfo->getContentAuthor($iContentId),
  194. $CNF['FIELD_ADDED'] => $mixedContentInfo->getContentDateAdded($iContentId),
  195. $CNF['FIELD_CHANGED'] => $mixedContentInfo->getContentDateChanged($iContentId),
  196. $CNF['FIELD_LINK'] => $mixedContentInfo->getContentLink($iContentId),
  197. $CNF['FIELD_TITLE'] => $mixedContentInfo->getContentTitle($iContentId),
  198. $CNF['FIELD_TEXT'] => $mixedContentInfo->getContentText($iContentId),
  199. );
  200. $aInfo = array_merge($aInfo, $mixedContentInfo->getContentInfo($iContentId));
  201. $mixedResult = $this->_oApi->updateData($sIndex, $mixedContentInfo->getName(), $iContentId, $this->_prepareToIndex($aInfo));
  202. if($mixedResult === null)
  203. return false;
  204. return $mixedResult['result'] == 'updated';
  205. }
  206. public function serviceDelete($iContentId, $mixedContentInfo, $sIndex = '')
  207. {
  208. if(is_array($mixedContentInfo)) {
  209. $sMethod = 'getObjectInstanceByAlert' . bx_gen_method_name($mixedContentInfo['alert_type']);
  210. $mixedContentInfo = BxDolContentInfo::$sMethod($mixedContentInfo['unit'], $mixedContentInfo['action']);
  211. }
  212. if(!($mixedContentInfo instanceof BxDolContentInfo))
  213. return false;
  214. $CNF = &$this->_oConfig->CNF;
  215. if(empty($sIndex))
  216. $sIndex = $this->_oConfig->getIndex();
  217. bx_import('Api', $this->_aModule);
  218. $oApi = new BxElsApi();
  219. $mixedResult = $oApi->deleteData($sIndex, $mixedContentInfo->getName(), $iContentId);
  220. if($mixedResult === null)
  221. return false;
  222. return $mixedResult['result'] == 'deleted';
  223. }
  224. public function serviceIndex($sIndex, $sType = '')
  225. {
  226. if(empty($sIndex))
  227. $sIndex = $this->_oConfig->getIndex();
  228. $aTypes = !empty($sType) ? array($sType) : array_keys(BxDolContentInfo::getSystems());
  229. if(empty($aTypes) || !is_array($aTypes))
  230. return false;
  231. bx_import('Api', $this->_aModule);
  232. $oApi = new BxElsApi();
  233. foreach($aTypes as $sType) {
  234. $oContentInfo = BxDolContentInfo::getObjectInstance($sType);
  235. if(!$oContentInfo)
  236. continue;
  237. $aIds = $oContentInfo->getAll(array('type' => 'all_ids'));
  238. if(empty($aIds) || !is_array($aIds))
  239. continue;
  240. foreach($aIds as $iId)
  241. $this->serviceAdd($iId, $oContentInfo);
  242. }
  243. }
  244. protected function _prepareToIndex($mixed)
  245. {
  246. if(!is_array($mixed))
  247. return strip_tags($mixed);
  248. foreach($mixed as $mixedKey => $mixedValue)
  249. $mixed[$mixedKey] = strip_tags($mixedValue);
  250. return $mixed;
  251. }
  252. }
  253. /** @} */