public function index() { if (!$this->input->is_cli_request()) { show_error('Acesso não autorizado', 403); } $client_params = array('hosts' => $this->config->item('elasticsearch_hosts')); $client = new Elasticsearch\Client($client_params); try { $client->indices()->delete(array('index' => 'varadouro')); } catch (Exception $e) { echo "Indice não existe, será criado\n"; } $client->indices()->create(array('index' => 'varadouro', 'body' => array('mappings' => array('evento' => array('properties' => array('titulo' => array('type' => 'string'), 'slug' => array('type' => 'string', 'index' => 'not_analyzed'), 'descricao' => array('type' => 'string'), 'data' => array('type' => 'string', 'index' => 'not_analyzed'))), 'noticia' => array('properties' => array('data' => array('type' => 'string', 'index' => 'not_analyzed'), 'titulo' => array('type' => 'string'), 'slug' => array('type' => 'string', 'index' => 'not_analyzed'), 'conteudo' => array('type' => 'string'))), 'espaco_cultural' => array('properties' => array('nome_espaco' => array('type' => 'string'), 'slug' => array('type' => 'string', 'index' => 'not_analyzed'), 'atividades_culturais' => array('type' => 'string'))), 'agente_cultural' => array('properties' => array('nome_responsavel' => array('type' => 'string'), 'slug' => array('type' => 'string', 'index' => 'not_analyzed'), 'atividades_culturais' => array('type' => 'string'))))))); echo "Indexando eventos...\n"; $start = 0; $eventos = $this->eventos_m->get_all(128, $start); while ($eventos) { foreach ($eventos as $evento) { echo "Indexando \"{$evento->id} - {$evento->titulo}\"\n"; $client->index(array('body' => array('titulo' => $evento->titulo, 'slug' => $evento->slug, 'descricao' => strip_tags_better($evento->descricao), 'data' => $evento->data), 'index' => 'varadouro', 'type' => 'evento', 'id' => $evento->id)); } $start += 128; $eventos = $this->eventos_m->get_all(128, $start); } echo "Indexando noticias...\n"; $start = 0; $noticias = $this->noticias_m->get_all(128, $start); while ($noticias) { foreach ($noticias as $noticia) { echo "Indexando \"{$noticia->id} - {$noticia->titulo}\"\n"; $client->index(array('body' => array('data' => $noticia->data, 'titulo' => $noticia->titulo, 'slug' => $noticia->slug, 'conteudo' => strip_tags_better($noticia->conteudo)), 'index' => 'varadouro', 'type' => 'noticia', 'id' => $noticia->id)); } $start += 128; $noticias = $this->noticias_m->get_all(128, $start); } echo "Indexando espaços culturais...\n"; $start = 0; $espacos = $this->espacos_culturais_m->get_all(128, $start); while ($espacos) { foreach ($espacos as $espaco) { echo "Indexando \"{$espaco->id} - {$espaco->nome_espaco}\"\n"; $client->index(array('body' => array('nome_espaco' => $espaco->nome_espaco, 'slug' => $espaco->slug, 'atividades_culturais' => strip_tags_better($espaco->atividades_culturais)), 'index' => 'varadouro', 'type' => 'espaco_cultural', 'id' => $espaco->id)); } $start += 128; $espacos = $this->espacos_culturais_m->get_all(128, $start); } echo "Indexando agentes culturais...\n"; $start = 0; $agentes = $this->agentes_culturais_m->get_all(128, $start); while ($agentes) { foreach ($agentes as $agente) { echo "Indexando \"{$agente->id} - {$agente->nome_responsavel}\"\n"; $client->index(array('body' => array('nome_responsavel' => $agente->nome_responsavel, 'slug' => $agente->slug, 'atividades_culturais' => strip_tags_better($agente->atividades_culturais)), 'index' => 'varadouro', 'type' => 'agente_cultural', 'id' => $agente->id)); } $start += 128; $agentes = $this->agentes_culturais_m->get_all(128, $start); } }
public static function putMapping() { $config = config('session.elastic', static::$defaultConfig); $ttl = $this->getTTL(); $client = new \Elasticsearch\Client(['hosts' => [$config['url']]]); $mappingParams = ['index' => $config['index'], 'type' => $config['type'], 'body' => ['properties' => ['created' => ['type' => 'date'], 'updated' => ['type' => 'date'], 'data' => ['type' => 'string', 'index' => 'no']], '_ttl' => $ttl ? ['enabled' => true, 'default' => $ttl ?: '30m'] : ['enabled' => false]]]; $client->indices()->putMapping($mappingParams); }
/** * Reverse the migrations. * * @return void */ public function down() { $prefix = \Config::get('reports.prefix'); if ($prefix) { $prefix .= '_'; } $client = new Elasticsearch\Client(); $client->indices()->delete(['index' => $prefix . 'reporter']); }
public function main() { set_time_limit(0); $logs = TableRegistry::get('LogLogins'); $params = array(); $params['hosts'] = array('192.168.26.112:9200'); $client = new \Elasticsearch\Client($params); # create a index $params = ['index' => 'mobgame', 'body' => ['settings' => ['number_of_shards' => 3, 'number_of_replicas' => 1], 'mappings' => ['log_logins' => ['properties' => ['user_id' => ['type' => 'integer'], 'created' => ['type' => 'date', 'format' => 'date_time'], 'modified' => ['type' => 'date', 'format' => 'date_time'], 'os' => ['type' => 'string', 'index' => 'not_analyzed'], 'resolution' => ['type' => 'string'], 'sdk_ver' => ['type' => 'string', 'index' => 'not_analyzed'], 'game_id' => ['type' => 'integer'], 'g_ver' => ['type' => 'string'], 'device' => ['type' => 'string', 'index' => 'not_analyzed'], 'network' => ['type' => 'string', 'index' => 'not_analyzed'], 'ip' => ['type' => 'string', 'index' => 'not_analyzed'], 'distributor' => ['type' => 'string', 'index' => 'not_analyzed']]]]]]; try { $client->indices()->create($params); $params['index'] = 'mobgame'; $params['type'] = 'log_logins'; $ret = $client->indices()->getMapping(array('index' => 'mobgame', 'type' => 'log_logins')); } catch (Exception $e) { echo $e->getMessage(); } $i = 0; $offset = 2; do { $this->out('Fetching data... '); $this->out('Offset: ' . $offset * $i); $query = $logs->find()->where(['id >=' => $i * $offset, 'id <' => ($i + 1) * $offset]); $docs = array(); if ($query->count() == 0) { break; } foreach ($query as $row) { $data = $row->toArray(); unset($data['id']); $data['created'] = $data['created']->toISO8601String(); $data['modified'] = $data['modified']->toISO8601String(); $docs['body'] = $data; $docs['index'] = 'mobgame'; $docs['type'] = 'log_logins'; debug($docs); $client->index($docs); $i++; die; } } while (true); }
public function actionCreate() { // Configure the index... $params = array(); // The index is "pages": $params['index'] = $this->_index; // Configure the analysis: $params['body']['settings']['analysis'] = array('filter' => array('my_filter' => array('type' => 'ngram', 'min_gram' => 3, 'max_gram' => 10)), 'analyzer' => array('my_analyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('lowercase', 'my_filter')))); // Map the fields: $params['body']['mappings']['page'] = array('_source' => array('enabled' => true), 'properties' => array('id' => array('type' => 'integer'), 'title' => array('type' => 'string', 'analyzer' => 'my_analyzer'), 'author' => array('type' => 'string', 'analyzer' => 'my_analyzer'), 'content' => array('type' => 'string', 'analyzer' => 'my_analyzer'))); // Create the ES client: $client = new Elasticsearch\Client(); // Delete the index, if need be: if (isset($_GET['delete'])) { $client->indices()->delete(array('index' => 'pages')); } // Create the index: $client->indices()->create($params); $this->render('create'); }
/** * @param Config\Config $config * * @return bool * @author Fabian Köstring */ private function elasticsearchCreateType($class, Config\Config $config) { try { $properties = []; foreach ($config->get('mapping') as $elasticsearchProperty => $classAttribute) { $propertyColumnAnnotation = $this->getPropertyColumnAnnotation($class, $classAttribute); $properties[$elasticsearchProperty] = ['type' => $propertyColumnAnnotation->type]; } $params = ['index' => $config->get('index'), 'body' => ['mappings' => [$config->get('type') => ['_source' => ['enabled' => true], 'properties' => $properties]]]]; //echo "<pre>"; //print_r($params); //echo "</pre>"; //die(); $response = $this->elasticsearchClient->indices()->create($params); return true; } catch (\Exception $e) { // @todo - Hier müssen genauere Fehlermeldungen rausgehauen werden. Was fehlt denn? Return false die('elasticsearchCreateType is failing. Maybe missing Parameter.'); } return false; }
/** * @return \Elasticsearch\Namespaces\IndicesNamespace */ protected function _getIndices() { return $this->_client->indices(); }
<?php require __DIR__ . '/../vendor/autoload.php'; use RecipeSearch\Constants; // Connect to local Elasticsearch node $esPort = getenv('APP_ES_PORT') ?: 9200; $client = new Elasticsearch\Client(['hosts' => ['localhost:' . $esPort]]); // Delete index to clear out existing data $deleteParams = []; $deleteParams['index'] = Constants::ES_INDEX; if ($client->indices()->exists($deleteParams)) { $client->indices()->delete($deleteParams); } // Setup bulk index request for seed data $params = []; $params['index'] = Constants::ES_INDEX; $params['type'] = Constants::ES_TYPE; $batchLines = []; $iter = new DirectoryIterator(__DIR__ . "/recipes"); foreach ($iter as $item) { if (!$item->isDot() && $item->isFile() && $item->isReadable()) { $filepath = $item->getPathname(); $basename = $item->getBasename(".json"); $batchLines[] = '{ "index": { "_id": "' . $basename . '" } }'; $batchLines[] = json_encode(json_decode(file_get_contents($filepath))); } } $params['body'] = implode("\n", $batchLines); // Bulk load seed data $ret = $client->bulk($params); if ($ret['errors']) {
} break; case 'DELETE_DOC': $params = array(); $params['index'] = 'acotel'; $params['type'] = 'eiplus_dev_status'; $params['id'] = '5521983059028'; if ($client->exists($params)) { echo "DELETING VALUE...<BR/>\n"; $retDelete = $client->delete($params); } else { echo "INEXISTENT VALUE...<BR/>\n"; } break; case 'DELETE_IDX': $deleteParams['index'] = 'acotel'; $client->indices()->delete($deleteParams); break; default: die('INVALID PARAMETER: Type GET,SAVE OR DELETE'); } } catch (Exception $e) { switch ($e->getCode()) { case 0: echo "========ELASTIC_SEARCH DOWN========="; break; default: echo "EXCEPTION LAUNCHED: [{$e->getCode()}] - {$e->getMessage()}"; break; } }
<?php /** * es goods mapping */ header("Content-Type: text/html; charset=utf-8"); require 'composer/vendor/autoload.php'; $client = new Elasticsearch\Client(); $indexParams['index'] = 'ec_hmv'; // Index Settings $indexParams['body']['settings']['number_of_shards'] = 1; $indexParams['body']['settings']['number_of_replicas'] = 0; // Example Index Mapping $myTypeMapping = array('_source' => array('enabled' => true), 'properties' => array('name' => array('type' => 'string', 'store' => 'yes', 'analyzer' => 'standard'), 'title' => array('type' => 'string', 'store' => 'yes', 'analyzer' => 'standard'), 'price' => array('type' => 'float'), 'img_url' => array('type' => 'string'), 'format' => array('type' => 'integer'), 'cate_id' => array('type' => 'integer'), 'keyword' => array('type' => 'string', 'store' => 'yes', 'analyzer' => 'standard'), 'released' => array('type' => 'integer'), 'availability' => array('type' => 'integer'), 'isdown' => array('type' => 'integer'), 'preorder' => array('type' => 'integer'), 'comment' => array('type' => 'integer'), 'sales' => array('type' => 'integer'), 'content' => array('type' => 'string', 'store' => 'yes', 'analyzer' => 'standard'))); $indexParams['body']['mappings']['ec_goods'] = $myTypeMapping; // Create the index $client->indices()->create($indexParams);