├── LICENSE ├── README.md └── application ├── config └── elasticsearch.php └── libraries └── elasticsearch.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Håkan Nylén 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | elasticsearch-codeigniter-library 2 | ================================= 3 | 4 | A small library to make search queries and create and add indexes. 5 | 6 | 7 | ## How to index data with the library 8 | You create data in an array to pass it to elasticsearch. You probably want to specify the Key for the document and show the result. Like this: 9 | 10 | $id = 1337; 11 | $data = array("name"=>"nisse", "age"=>"14", "sex"=>"male"); 12 | var_dump($this->elasticsearch->add("people", $id, $data)); 13 | 14 | This will save the array to the elasticsearch. "people" is the collection, the index where you want to save it. 15 | 16 | # CRUD Operations 17 | 18 | ## CREATE 19 | $id = 1337; 20 | $data = array("name"=>"nisse", "age"=>"14", "sex"=>"male"); 21 | $return = $this->elasticsearch->add("people", $id, $data); 22 | 23 | ## READ 24 | $id = 1337; 25 | $this->elasticsearch->get("people", $id); 26 | 27 | ## UPDATE 28 | $id = 1337; 29 | $data = array( 30 | "id" => $id, 31 | "name"=>"nisse", 32 | "age"=>"14", 33 | "sex"=>"male" 34 | ); 35 | $return = $this->elasticsearch->add("people", $id, $data); 36 | 37 | ## DELETE 38 | $id = 1337; 39 | $this->elasticsearch->delete("people", $id); 40 | -------------------------------------------------------------------------------- /application/config/elasticsearch.php: -------------------------------------------------------------------------------- 1 | config -> load("elasticsearch"); 20 | $this -> server = $ci -> config -> item('es_server'); 21 | $this -> index = $ci -> config -> item('index'); 22 | } 23 | /** 24 | * Handling the call for every function with curl 25 | * 26 | * @param type $path 27 | * @param type $method 28 | * @param type $data 29 | * 30 | * @return type 31 | * @throws Exception 32 | */ 33 | 34 | private function call($path, $method = 'GET', $data = null) 35 | { 36 | if (!$this -> index) { 37 | throw new Exception('$this->index needs a value'); 38 | } 39 | 40 | $url = $this -> server . '/' . $this -> index . '/' . $path; 41 | 42 | $headers = array('Accept: application/json', 'Content-Type: application/json', ); 43 | 44 | $ch = curl_init(); 45 | curl_setopt($ch, CURLOPT_URL, $url); 46 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 47 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 48 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 49 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 50 | 51 | switch($method) { 52 | case 'GET' : 53 | break; 54 | case 'POST' : 55 | curl_setopt($ch, CURLOPT_POST, true); 56 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 57 | break; 58 | case 'PUT' : 59 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 60 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 61 | break; 62 | case 'DELETE' : 63 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 64 | break; 65 | } 66 | 67 | $response = curl_exec($ch); 68 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 69 | 70 | return json_decode($response, true); 71 | } 72 | 73 | 74 | /** 75 | * create a index with mapping or not 76 | * 77 | * @param json $map 78 | */ 79 | 80 | public function create($map = false) 81 | { 82 | if (!$map) { 83 | $this -> call(null, 'PUT'); 84 | } else { 85 | $this -> call(null, 'PUT', $map); 86 | } 87 | } 88 | 89 | /** 90 | * get status 91 | * 92 | * @return array 93 | */ 94 | 95 | public function status() 96 | { 97 | return $this -> call('_status'); 98 | } 99 | 100 | /** 101 | * count how many indexes it exists 102 | * 103 | * @param string $type 104 | * 105 | * @return array 106 | */ 107 | 108 | public function count($type) 109 | { 110 | return $this -> call($type . '/_count?' . http_build_query(array(null => '{matchAll:{}}'))); 111 | } 112 | 113 | /** 114 | * set the mapping for the index 115 | * 116 | * @param string $type 117 | * @param json $data 118 | * 119 | * @return array 120 | */ 121 | 122 | public function map($type, $data) 123 | { 124 | return $this -> call($type . '/_mapping', 'PUT', $data); 125 | } 126 | 127 | /** 128 | * set the mapping for the index 129 | * 130 | * @param type $type 131 | * @param type $id 132 | * @param type $data 133 | * 134 | * @return type 135 | */ 136 | 137 | public function add($type, $id, $data) 138 | { 139 | return $this -> call($type . '/' . $id, 'PUT', $data); 140 | } 141 | 142 | /** 143 | * delete a index 144 | * 145 | * @param type $type 146 | * @param type $id 147 | * 148 | * @return type 149 | */ 150 | 151 | public function delete($type, $id) 152 | { 153 | return $this -> call($type . '/' . $id, 'DELETE'); 154 | } 155 | 156 | /** 157 | * make a simple search query 158 | * 159 | * @param type $type 160 | * @param type $q 161 | * 162 | * @return type 163 | */ 164 | 165 | public function query($type, $q) 166 | { 167 | return $this -> call($type . '/_search?' . http_build_query(array('q' => $q))); 168 | } 169 | 170 | /** 171 | * make a advanced search query with json data to send 172 | * 173 | * @param type $type 174 | * @param type $query 175 | * 176 | * @return type 177 | */ 178 | 179 | public function advancedquery($type, $query) 180 | { 181 | return $this -> call($type . '/_search', 'POST', $query); 182 | } 183 | 184 | /** 185 | * make a search query with result sized set 186 | * 187 | * @param string $type what kind of type of index you want to search 188 | * @param string $query the query as a string 189 | * @param integer $size The size of the results 190 | * 191 | * @return array 192 | */ 193 | 194 | public function query_wresultSize($type, $query, $size = 999) 195 | { 196 | return $this -> call($type . '/_search?' . http_build_query(array('q' => $q, 'size' => $size))); 197 | } 198 | 199 | /** 200 | * get one index via the id 201 | * 202 | * @param string $type The index type 203 | * @param integer $id the indentifier for a index 204 | * 205 | * @return type 206 | */ 207 | 208 | public function get($type, $id) 209 | { 210 | return $this -> call($type . '/' . $id, 'GET'); 211 | } 212 | 213 | /** 214 | * Query the whole server 215 | * 216 | * @param type $query 217 | * 218 | * @return type 219 | */ 220 | 221 | public function query_all($query) 222 | { 223 | return $this -> call('_search?' . http_build_query(array('q' => $query))); 224 | } 225 | 226 | /** 227 | * get similar indexes for one index specified by id - send data to add filters or more 228 | * 229 | * @param string $type 230 | * @param integer $id 231 | * @param string $fields 232 | * @param string $data 233 | * 234 | * @return array 235 | */ 236 | 237 | public function morelikethis($type, $id, $fields = false, $data = false) 238 | { 239 | if ($data != false && !$fields) { 240 | return $this -> call($type . '/' . $id . '/_mlt', 'GET', $data); 241 | } else if ($data != false && $fields != false) { 242 | return $this -> call($type . '/' . $id . '/_mlt?' . $fields, 'POST', $data); 243 | } else if (!$fields) { 244 | return $this -> call($type . '/' . $id . '/_mlt'); 245 | } else { 246 | return $this -> call($type . '/' . $id . '/_mlt?' . $fields); 247 | } 248 | } 249 | 250 | /** 251 | * make a search query with result sized set 252 | * 253 | * @param type $query 254 | * @param type $size 255 | * 256 | * @return type 257 | */ 258 | public function query_all_wresultSize($query, $size = 999) 259 | { 260 | return $this -> call('_search?' . http_build_query(array('q' => $query, 'size' => $size))); 261 | } 262 | 263 | /** 264 | * make a suggest query based on similar looking terms 265 | * 266 | * @param type $query 267 | * 268 | * @return array 269 | */ 270 | public function suggest($query) 271 | { 272 | return $this -> call('_suggest', 'POST', $query); 273 | } 274 | 275 | } 276 | --------------------------------------------------------------------------------