You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ElasticSearchPlugin.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. if (!defined('GNUSOCIAL')) {
  3. exit(1);
  4. }
  5. require('lib/ElasticSearch.php');
  6. class ElasticSearchPlugin extends Plugin
  7. {
  8. const VERSION = '0.0.1';
  9. function onGetSearchEngine(Memcached_DataObject $target, $table, &$search_engine)
  10. {
  11. $engine = $this->createEngine($target);
  12. // TODO: Error handling
  13. // Consider falling back to built-in search on error
  14. // (i.e.: returning `true`)
  15. $search_engine = $engine;
  16. return false;
  17. }
  18. function onEndNoticeSaveWeb($action, $notice)
  19. {
  20. $this->handleNoticeSave($notice);
  21. return true;
  22. }
  23. function onEndNoticeSave($notice)
  24. {
  25. $this->handleNoticeSave($notice);
  26. return true;
  27. }
  28. function handleNoticeSave($notice)
  29. {
  30. $engine = $this->createEngine(new Notice());
  31. if ($notice->getVerb(true) === 'delete') {
  32. $engine->delete($notice);
  33. } else {
  34. $engine->index($notice);
  35. }
  36. }
  37. function onRouterInitialized($m)
  38. {
  39. // Hijack the regular search page
  40. $m->connect('search/notice', array('action' => 'enoticesearch'));
  41. }
  42. function createEngine($target)
  43. {
  44. $index_name = $this->getIndexname();
  45. $hosts = common_config('elasticsearch', 'hosts');
  46. if ($hosts === false) {
  47. $hosts = [ '127.0.0.1:9200' ];
  48. }
  49. return new ElasticSearch($target, null, $index_name, $hosts);
  50. }
  51. function getIndexName()
  52. {
  53. $index_name = common_config('elasticsearch', 'index_name');
  54. if ($index_name === false) {
  55. $index_name = 'gnusocial';
  56. }
  57. return $index_name;
  58. }
  59. function onPluginVersion(array &$versions)
  60. {
  61. $versions[] = array('name' => 'Elasticsearch backend',
  62. 'version' => self::VERSION,
  63. 'author' => 'chimo',
  64. 'homepage' => 'https://github.com/chimo/gs-elasticsearch',
  65. 'description' =>
  66. // TRANS: Plugin description.
  67. _m('ElasticSearch engine'));
  68. return true;
  69. }
  70. }