|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Elastica\Query; |
| 4 | + |
| 5 | +/** |
| 6 | + * Image query |
| 7 | + * |
| 8 | + * @category Xodoa |
| 9 | + * @package Elastica |
| 10 | + * @author Jacques Moati <[email protected]> |
| 11 | + * @link https://github.com/kzwang/elasticsearch-image |
| 12 | + */ |
| 13 | +class Image extends AbstractQuery |
| 14 | +{ |
| 15 | + public function __construct(array $image = array()) |
| 16 | + { |
| 17 | + $this->setParams($image); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Sets a param for the given field |
| 22 | + * |
| 23 | + * @param string $field |
| 24 | + * @param string $key |
| 25 | + * @param string $value |
| 26 | + * |
| 27 | + * @return $this |
| 28 | + */ |
| 29 | + public function setFieldParam($field, $key, $value) |
| 30 | + { |
| 31 | + if (!isset($this->_params[$field])) { |
| 32 | + $this->_params[$field] = array(); |
| 33 | + } |
| 34 | + |
| 35 | + $this->_params[$field][$key] = $value; |
| 36 | + |
| 37 | + return $this; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Set field boost value |
| 42 | + * |
| 43 | + * If not set, defaults to 1.0. |
| 44 | + * |
| 45 | + * @param string $field |
| 46 | + * @param float $boost |
| 47 | + * |
| 48 | + * @return $this |
| 49 | + */ |
| 50 | + public function setFieldBoost($field, $boost = 1.0) |
| 51 | + { |
| 52 | + return $this->setFieldParam($field, 'boost', (float)$boost); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Set field feature value |
| 57 | + * |
| 58 | + * If not set, defaults CEDD. |
| 59 | + * |
| 60 | + * @param string $field |
| 61 | + * @param string $feature |
| 62 | + * |
| 63 | + * @return $this |
| 64 | + */ |
| 65 | + public function setFieldFeature($field, $feature = "CEDD") |
| 66 | + { |
| 67 | + return $this->setFieldParam($field, 'feature', $feature); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Set field hash value |
| 72 | + * |
| 73 | + * If not set, defaults BIT_SAMPLING. |
| 74 | + * |
| 75 | + * @param string $field |
| 76 | + * @param string $hash |
| 77 | + * |
| 78 | + * @return $this |
| 79 | + */ |
| 80 | + public function setFieldHash($field, $hash = "BIT_SAMPLING") |
| 81 | + { |
| 82 | + return $this->setFieldParam($field, 'hash', $hash); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set field image value |
| 87 | + * |
| 88 | + * @param string $field |
| 89 | + * @param string $path File will be base64_encode |
| 90 | + * |
| 91 | + * @return $this |
| 92 | + * @throws \Exception |
| 93 | + */ |
| 94 | + public function setFieldImage($field, $path) |
| 95 | + { |
| 96 | + if (!file_exists($path) || !is_readable($path)) { |
| 97 | + throw new \Exception(sprintf("File %s can't be open", $path)); |
| 98 | + } |
| 99 | + |
| 100 | + return $this->setFieldParam($field, 'image', base64_encode(file_get_contents($path))); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Set field index value |
| 105 | + * |
| 106 | + * @param string $field |
| 107 | + * @param string $index |
| 108 | + * |
| 109 | + * @return $this |
| 110 | + */ |
| 111 | + public function setFieldIndex($field, $index) |
| 112 | + { |
| 113 | + return $this->setFieldParam($field, 'index', $index); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Set field type value |
| 118 | + * |
| 119 | + * @param string $field |
| 120 | + * @param string $type |
| 121 | + * |
| 122 | + * @return $this |
| 123 | + */ |
| 124 | + public function setFieldType($field, $type) |
| 125 | + { |
| 126 | + return $this->setFieldParam($field, 'type', $type); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Set field id value |
| 131 | + * |
| 132 | + * @param string $field |
| 133 | + * @param string $id |
| 134 | + * |
| 135 | + * @return $this |
| 136 | + */ |
| 137 | + public function setFieldId($field, $id) |
| 138 | + { |
| 139 | + return $this->setFieldParam($field, 'id', $id); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Set field path value |
| 144 | + * |
| 145 | + * @param string $field |
| 146 | + * @param string $path |
| 147 | + * |
| 148 | + * @return $this |
| 149 | + */ |
| 150 | + public function setFieldPath($field, $path) |
| 151 | + { |
| 152 | + return $this->setFieldParam($field, 'path', $path); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Define quickly a reference image already in your elasticsearch database |
| 157 | + * |
| 158 | + * If not set, path will be the same as $field. |
| 159 | + * |
| 160 | + * @param string $field |
| 161 | + * @param string $index |
| 162 | + * @param string $type |
| 163 | + * @param string $id |
| 164 | + * @param string $path |
| 165 | + * |
| 166 | + * @return $this |
| 167 | + */ |
| 168 | + public function setImageByReference($field, $index, $type, $id, $path = null) |
| 169 | + { |
| 170 | + if (null === $path) { |
| 171 | + $path = $field; |
| 172 | + } |
| 173 | + |
| 174 | + $this |
| 175 | + ->setFieldIndex($field, $index) |
| 176 | + ->setFieldType($field, $type) |
| 177 | + ->setFieldId($field, $id); |
| 178 | + |
| 179 | + return $this->setFieldPath($field, $path); |
| 180 | + } |
| 181 | +} |
0 commit comments