BxElsModule.php 10 KB

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