Base.Class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php // vim:set ts=4 sw=4 et:
  2. namespace Mall\Framework\SearchClient\Transport;
  3. /**
  4. * This file is part of the ElasticSearch PHP client
  5. *
  6. * (c) Raymond Julin <raymond.julin@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. abstract class Base
  12. {
  13. /**
  14. * What host to connect to for server
  15. * @var string
  16. */
  17. protected $host = "";
  18. /**
  19. * Port to connect on
  20. * @var int
  21. */
  22. protected $port = 9200;
  23. /**
  24. * ElasticSearch index
  25. * @var string
  26. */
  27. protected $index;
  28. /**
  29. * ElasticSearch document type
  30. * @var string
  31. */
  32. protected $type;
  33. /**
  34. * Default constructor, just set host and port
  35. * @param string $host
  36. * @param int $port
  37. */
  38. public function __construct($host, $port)
  39. {
  40. $this->host = $host;
  41. $this->port = $port;
  42. }
  43. /**
  44. * Method for indexing a new document
  45. *
  46. * @param array|object $document
  47. * @param mixed $id
  48. * @param array $options
  49. */
  50. abstract public function index($document, $id = false, array $options = array());
  51. /**
  52. * Perform a request against the given path/method/payload combination
  53. * Example:
  54. * $es->request('/_status');
  55. *
  56. * @param string|array $path
  57. * @param string $method
  58. * @param array|bool $payload
  59. * @return
  60. */
  61. abstract public function request($path, $method = "GET", $payload = false, $buildPath = true);
  62. /**
  63. * Delete a document by its id
  64. * @param mixed $id
  65. * @param array $options
  66. */
  67. abstract public function delete($id = false, array $options = array());
  68. /**
  69. * Perform a search based on query
  70. * @param array|string $query
  71. * @param array|string $options
  72. */
  73. abstract public function search($query, array $options = array());
  74. abstract public function scrollSearch($query, $scroll = '10s', $size = 2000, array $options = array());
  75. abstract public function batchUpdateFieldVaule($data, $query);
  76. abstract public function createBase($index, $mappings);
  77. abstract public function deleteBase($index);
  78. /**
  79. * Search
  80. *
  81. * @return array
  82. * @param mixed $query String or array to use as criteria for delete
  83. * @param array $options Parameters to pass to delete action
  84. * @throws \Mall\Framework\Search
  85. */
  86. public function deleteByQuery($query, array $options = array())
  87. {
  88. throw new \Mall\Framework\SearchClient\Exception(__FUNCTION__ . ' not implemented for ' . __CLASS__);
  89. }
  90. /**
  91. * Set what index to act against
  92. * @param string $index
  93. */
  94. public function setIndex($index)
  95. {
  96. $this->index = $index;
  97. }
  98. /**
  99. * Set what document types to act against
  100. * @param string $type
  101. */
  102. public function setType($type)
  103. {
  104. $this->type = $type;
  105. }
  106. /**
  107. * Build a callable url
  108. *
  109. * @return string
  110. * @param array|bool $path
  111. * @param array $options Query parameter options to pass
  112. */
  113. protected function buildUrl($path = false, array $options = array())
  114. {
  115. $isAbsolute = (is_array($path) ? isset($path[0][0])?$path[0][0] : '' : $path[0]) === '/';
  116. $url = $isAbsolute ? '' : "/" . $this->index;
  117. if ($path && is_array($path) && count($path) > 0)
  118. $url .= "/" . implode("/", array_filter($path));
  119. if (substr($url, -1) == "/")
  120. $url = substr($url, 0, -1);
  121. if (count($options) > 0)
  122. $url .= "?" . http_build_query($options, '', '&');
  123. return $url;
  124. }
  125. }