BxElsModule.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. if(empty($sIndex))
  23. $sIndex = $this->_oConfig->getIndex();
  24. // search all
  25. $mixed = $this->_oApi->api('/' . $sIndex . (!empty($sType) ? '/' . $sType : '') . '/_search?q=*');
  26. // $mixed = $this->_oApi->api('/testdata/_search', ['query' => ['match_all' => (object)[]]]);
  27. // search for the term
  28. // $mixed = $this->_oApi->api('/testdata/_search', ['query' => ['simple_query_string' => ['query' => 'palm']]]);
  29. // $mixed = $this->_oApi->api('/testdata/_search?q=palm');
  30. // search in a field
  31. // $mixed = $this->_oApi->api('/testdata/_search', ['query' => ['simple_query_string' => ['query' => 'palm', 'fields' => ['name']]]]);
  32. // compound searchs
  33. /*
  34. $mixed = $this->_oApi->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 = $this->_oApi->api('/' . $this->_oConfig->getIndex(), array(), 'delete');
  72. // delete indexed doc
  73. //$mixed = $this->_oApi->api('/' . $this->_oConfig->getIndex() . '/trees/23', [], 'delete');
  74. // get indexed doc
  75. // $mixed = $this->_oApi->api('/testdata/trees/23');
  76. // index a doc
  77. // $mixed = $this->_oApi->indexData('testdata', 'trees', 23, ['name' => 'apple tree', 'fruits' => 'apples']);
  78. // $mixed = $this->_oApi->indexData('testdata', 'trees', 24, ['name' => 'palm', 'fruits' => 'coconuts']);
  79. // get indexes
  80. // $mixed = $this->_oApi->api('/_cat/indices');
  81. // add index
  82. // $mixed = $this->_oApi->api('/testdata', [], 'PUT');
  83. // test
  84. //$mixed = $this->_oApi->api('/_cat/health');
  85. // mapping put
  86. //$mixed = $this->_oApi->api('/' . $sIndex . '/_mapping/bx_forum', array('properties' => array('featured' => array('type' => 'long'))));
  87. // mapping get
  88. //$mixed = $this->_oApi->api('/' . $sIndex . '/_mapping' . (!empty($sType) ? '/' . $sType : ''));
  89. echo '<pre>';
  90. if (null === $mixed)
  91. echo $this->_oApi->getErrorMsg();
  92. else
  93. var_dump($mixed);
  94. echo '</pre>';
  95. }
  96. /**
  97. * @page service Service Calls
  98. * @section bx_elasticsearch ElasticSearch
  99. * @subsection bx_elasticsearch-other Other
  100. * @subsubsection bx_elasticsearch-is_configured is_configured
  101. *
  102. * @code bx_srv('bx_elasticsearch', 'is_configured', [...]); @endcode
  103. *
  104. * Checkes whether the module is correctly configured or not.
  105. *
  106. * @return boolean value determining where the module is configured or not.
  107. *
  108. * @see BxElsModule::serviceIsConfigured
  109. */
  110. /**
  111. * @ref bx_elasticsearch-is_configured "is_configured"
  112. */
  113. public function serviceIsConfigured()
  114. {
  115. $CNF = &$this->_oConfig->CNF;
  116. return isset($CNF['PARAM_API_URL']) && getParam($CNF['PARAM_API_URL']) != '';
  117. }
  118. /**
  119. * @page service Service Calls
  120. * @section bx_elasticsearch ElasticSearch
  121. * @subsection bx_elasticsearch-search Search
  122. * @subsubsection bx_elasticsearch-search_simple search_simple
  123. *
  124. * @code bx_srv('bx_elasticsearch', 'search_simple', [...]); @endcode
  125. *
  126. * Perform a simple search by term. Index and Type to search in can be specified.
  127. *
  128. * @param $sTerm string value with keyword to search for.
  129. * @param $sType (optional) if type is specified then the search will be performed in this type only.
  130. * @param $sIndex (optional) if index is specified then the search will be performed in this index only.
  131. * @return an array with search results.
  132. *
  133. * @see BxElsModule::serviceSearchSimple
  134. */
  135. /**
  136. * @ref bx_elasticsearch-search_simple "search_simple"
  137. */
  138. public function serviceSearchSimple($sTerm, $sType = '', $sIndex = '')
  139. {
  140. if(empty($sIndex))
  141. $sIndex = $this->_oConfig->getIndex();
  142. $mixedResult = $this->_oApi->searchSimple($sIndex, $sType, $sTerm);
  143. if(!$mixedResult || !is_array($mixedResult))
  144. return false;
  145. return $mixedResult['hits'];
  146. }
  147. /**
  148. * @page service Service Calls
  149. * @section bx_elasticsearch ElasticSearch
  150. * @subsection bx_elasticsearch-search Search
  151. * @subsubsection bx_elasticsearch-search_extended search_extended
  152. *
  153. * @code bx_srv('bx_elasticsearch', 'search_extended', [...]); @endcode
  154. *
  155. * Perform an extended search by provided conditions. Index and Type to search in can be specified.
  156. *
  157. * @param $aCondition an array with conditions to construct search request from.
  158. * @param $aSelection (optional) an array with custom condition.
  159. * @param $sType (optional) if type is specified then the search will be performed in this type only.
  160. * @param $sIndex (optional) if index is specified then the search will be performed in this index only.
  161. * @return an array with search results.
  162. *
  163. * Condition #1: keyword search
  164. * $aCondition = array('val' => 'test');
  165. *
  166. * Condition #2: `featured`='1'
  167. * $aCondition = array('fld' => 'featured', 'val' => 0);
  168. *
  169. * Condition #3: (`title`='test' OR `text`='test' OR (`featured`='1' AND `allow_view_to`='3'))
  170. * $aCondition = array('grp' => true, 'opr' => 'OR', 'cnds' => array(
  171. * array('fld' => 'title', 'val' => 'test'),
  172. * array('fld' => 'text', 'val' => 'test'),
  173. * array('grp' => true, 'opr' => 'AND', 'cnds' => array(
  174. * array('fld' => 'featured', 'val' => 1),
  175. * array('fld' => 'allow_view_to', 'val' => 3)
  176. * )),
  177. * array('simple_query_string' => array('query' => 'test')),
  178. * ));
  179. *
  180. * Condition #4: ((`title`='test' OR `text`='test') AND `featured`='1' AND `allow_view_to`='3')
  181. * $aCondition = array('grp' => true, 'opr' => 'AND', 'cnds' => array(
  182. * array('grp' => true, 'opr' => 'OR', 'cnds' => array(
  183. * array('fld' => 'title', 'val' => 'test'),
  184. * array('fld' => 'text', 'val' => 'test'),
  185. * )),
  186. * array('fld' => 'featured', 'val' => 0),
  187. * array('fld' => 'allow_view_to', 'val' => 3)
  188. * ));
  189. *
  190. * @see BxElsModule::serviceSearchExtended
  191. */
  192. /**
  193. * @ref bx_elasticsearch-search_extended "search_extended"
  194. */
  195. public function serviceSearchExtended($aCondition, $aSelection = array(), $sType = '', $sIndex = '')
  196. {
  197. if(empty($sIndex))
  198. $sIndex = $this->_oConfig->getIndex();
  199. $mixedResult = $this->_oApi->searchExtended($sIndex, $sType, $aCondition, $aSelection);
  200. if(!$mixedResult || !is_array($mixedResult))
  201. return false;
  202. return $mixedResult['hits'];
  203. }
  204. /**
  205. * @page service Service Calls
  206. * @section bx_elasticsearch ElasticSearch
  207. * @subsection bx_elasticsearch-manage Manage
  208. * @subsubsection bx_elasticsearch-get get
  209. *
  210. * @code bx_srv('bx_elasticsearch', 'get', [...]); @endcode
  211. *
  212. * Get item's data by content ID and type.
  213. *
  214. * @param $iContentId integer value with content ID.
  215. * @param $sType a string with content type.
  216. * @param $sIndex (optional) if index is specified then the item will be searched in this index only.
  217. * @return an array with item's data.
  218. *
  219. * @see BxElsModule::serviceGet
  220. */
  221. /**
  222. * @ref bx_elasticsearch-get "get"
  223. */
  224. public function serviceGet($iContentId, $sType, $sIndex = '')
  225. {
  226. if(empty($sIndex))
  227. $sIndex = $this->_oConfig->getIndex();
  228. return $this->_oApi->getData($sIndex, $sType, $iContentId);
  229. }
  230. /**
  231. * @page service Service Calls
  232. * @section bx_elasticsearch ElasticSearch
  233. * @subsection bx_elasticsearch-manage Manage
  234. * @subsubsection bx_elasticsearch-add add
  235. *
  236. * @code bx_srv('bx_elasticsearch', 'add', [...]); @endcode
  237. *
  238. * Add item to store in Elasticsearch.
  239. *
  240. * @param $iContentId integer value with content ID.
  241. * @param $mixedContentInfo mixed value with content info to store.
  242. * @param $sIndex (optional) if index is specified then the item will be stored in this index.
  243. * @return boolean value determining where the item was added or not.
  244. *
  245. * @see BxElsModule::serviceAdd
  246. */
  247. /**
  248. * @ref bx_elasticsearch-add "add"
  249. */
  250. public function serviceAdd($iContentId, $mixedContentInfo, $sIndex = '')
  251. {
  252. if(is_array($mixedContentInfo)) {
  253. $sMethod = 'getObjectInstanceByAlert' . bx_gen_method_name($mixedContentInfo['alert_type']);
  254. $mixedContentInfo = BxDolContentInfo::$sMethod($mixedContentInfo['unit'], $mixedContentInfo['action']);
  255. }
  256. if(!($mixedContentInfo instanceof BxDolContentInfo))
  257. return false;
  258. $CNF = &$this->_oConfig->CNF;
  259. if(empty($sIndex))
  260. $sIndex = $this->_oConfig->getIndex();
  261. $aInfo = array(
  262. $CNF['FIELD_AUTHOR'] => $mixedContentInfo->getContentAuthor($iContentId),
  263. $CNF['FIELD_ADDED'] => $mixedContentInfo->getContentDateAdded($iContentId),
  264. $CNF['FIELD_CHANGED'] => $mixedContentInfo->getContentDateChanged($iContentId),
  265. $CNF['FIELD_LINK'] => $mixedContentInfo->getContentLink($iContentId),
  266. $CNF['FIELD_TITLE'] => $mixedContentInfo->getContentTitle($iContentId),
  267. $CNF['FIELD_TEXT'] => $mixedContentInfo->getContentText($iContentId),
  268. );
  269. $aInfo = array_merge($aInfo, $mixedContentInfo->getContentInfo($iContentId));
  270. $mixedResult = $this->_oApi->indexData($sIndex, $mixedContentInfo->getName(), $iContentId, $this->_prepareToIndex($aInfo));
  271. if($mixedResult === null)
  272. return false;
  273. return $mixedResult['result'] == 'created';
  274. }
  275. /**
  276. * @page service Service Calls
  277. * @section bx_elasticsearch ElasticSearch
  278. * @subsection bx_elasticsearch-manage Manage
  279. * @subsubsection bx_elasticsearch-update update
  280. *
  281. * @code bx_srv('bx_elasticsearch', 'update', [...]); @endcode
  282. *
  283. * Update item in Elasticsearch storage.
  284. *
  285. * @param $iContentId integer value with content ID.
  286. * @param $mixedContentInfo mixed value with new content info.
  287. * @param $sIndex (optional) if index is specified then the item will be updated in this index.
  288. * @return boolean value determining where the item was updated or not.
  289. *
  290. * @see BxElsModule::serviceUpdate
  291. */
  292. /**
  293. * @ref bx_elasticsearch-update "update"
  294. */
  295. public function serviceUpdate($iContentId, $mixedContentInfo, $sIndex = '')
  296. {
  297. if(is_array($mixedContentInfo)) {
  298. $sMethod = 'getObjectInstanceByAlert' . bx_gen_method_name($mixedContentInfo['alert_type']);
  299. $mixedContentInfo = BxDolContentInfo::$sMethod($mixedContentInfo['unit'], $mixedContentInfo['action']);
  300. }
  301. if(!($mixedContentInfo instanceof BxDolContentInfo))
  302. return false;
  303. $CNF = &$this->_oConfig->CNF;
  304. if(empty($sIndex))
  305. $sIndex = $this->_oConfig->getIndex();
  306. $aInfo = array(
  307. $CNF['FIELD_AUTHOR'] => $mixedContentInfo->getContentAuthor($iContentId),
  308. $CNF['FIELD_ADDED'] => $mixedContentInfo->getContentDateAdded($iContentId),
  309. $CNF['FIELD_CHANGED'] => $mixedContentInfo->getContentDateChanged($iContentId),
  310. $CNF['FIELD_LINK'] => $mixedContentInfo->getContentLink($iContentId),
  311. $CNF['FIELD_TITLE'] => $mixedContentInfo->getContentTitle($iContentId),
  312. $CNF['FIELD_TEXT'] => $mixedContentInfo->getContentText($iContentId),
  313. );
  314. $aInfo = array_merge($aInfo, $mixedContentInfo->getContentInfo($iContentId));
  315. $mixedResult = $this->_oApi->updateData($sIndex, $mixedContentInfo->getName(), $iContentId, $this->_prepareToIndex($aInfo));
  316. if($mixedResult === null)
  317. return false;
  318. return $mixedResult['result'] == 'updated';
  319. }
  320. /**
  321. * @page service Service Calls
  322. * @section bx_elasticsearch ElasticSearch
  323. * @subsection bx_elasticsearch-manage Manage
  324. * @subsubsection bx_elasticsearch-delete delete
  325. *
  326. * @code bx_srv('bx_elasticsearch', 'delete', [...]); @endcode
  327. *
  328. * Delete item from Elasticsearch storage.
  329. *
  330. * @param $iContentId integer value with content ID.
  331. * @param $mixedContentInfo mixed value with content info which is needed during deletion.
  332. * @param $sIndex (optional) if index is specified then the item will be deleted from this index.
  333. * @return boolean value determining where the item was deleted or not.
  334. *
  335. * @see BxElsModule::serviceDelete
  336. */
  337. /**
  338. * @ref bx_elasticsearch-delete "delete"
  339. */
  340. public function serviceDelete($iContentId, $mixedContentInfo, $sIndex = '')
  341. {
  342. if(is_array($mixedContentInfo)) {
  343. $sMethod = 'getObjectInstanceByAlert' . bx_gen_method_name($mixedContentInfo['alert_type']);
  344. $mixedContentInfo = BxDolContentInfo::$sMethod($mixedContentInfo['unit'], $mixedContentInfo['action']);
  345. }
  346. if(!($mixedContentInfo instanceof BxDolContentInfo))
  347. return false;
  348. $CNF = &$this->_oConfig->CNF;
  349. if(empty($sIndex))
  350. $sIndex = $this->_oConfig->getIndex();
  351. $mixedResult = $this->_oApi->deleteData($sIndex, $mixedContentInfo->getName(), $iContentId);
  352. if($mixedResult === null)
  353. return false;
  354. return $mixedResult['result'] == 'deleted';
  355. }
  356. /**
  357. * @page service Service Calls
  358. * @section bx_elasticsearch ElasticSearch
  359. * @subsection bx_elasticsearch-manage Manage
  360. * @subsubsection bx_elasticsearch-index index
  361. *
  362. * @code bx_srv('bx_elasticsearch', 'delete', [...]); @endcode
  363. *
  364. * Index data in Elasticsearch storage.
  365. *
  366. * @param $sIndex string value with storage's index name which will be indexed.
  367. * @param $sType (optional) if type is specified then data in this type will be indexed only.
  368. * @return boolean value determining where the data was indexed or not.
  369. *
  370. * @see BxElsModule::serviceIndex
  371. */
  372. /**
  373. * @ref bx_elasticsearch-index "index"
  374. */
  375. public function serviceIndex($sIndex, $sType = '')
  376. {
  377. if(empty($sIndex))
  378. $sIndex = $this->_oConfig->getIndex();
  379. $aTypes = !empty($sType) ? array($sType) : array_keys(BxDolContentInfo::getSystems());
  380. if(empty($aTypes) || !is_array($aTypes))
  381. return false;
  382. foreach($aTypes as $sType) {
  383. $oContentInfo = BxDolContentInfo::getObjectInstance($sType);
  384. if(!$oContentInfo)
  385. continue;
  386. $aIds = $oContentInfo->getAll(array('type' => 'all_ids'));
  387. if(empty($aIds) || !is_array($aIds))
  388. continue;
  389. foreach($aIds as $iId)
  390. $this->serviceAdd($iId, $oContentInfo);
  391. }
  392. return true;
  393. }
  394. protected function _prepareToIndex($mixed)
  395. {
  396. if(!is_array($mixed))
  397. return strip_tags($mixed);
  398. foreach($mixed as $mixedKey => $mixedValue)
  399. $mixed[$mixedKey] = strip_tags($mixedValue);
  400. return $mixed;
  401. }
  402. }
  403. /** @} */