<?php require __DIR__ . '/../vendor/autoload.php'; use RecipeSearch\Constants; use RecipeSearch\Util; // Add recipe if one was submitted if (count($_POST) > 0) { // Connect to Elasticsearch (1-node cluster) $esPort = getenv('APP_ES_PORT') ?: 9200; $client = new Elasticsearch\Client(['hosts' => ['localhost:' . $esPort]]); // Convert recipe title to ID $id = Util::recipeTitleToId($_POST['title']); // Check if recipe with this ID already exists $exists = $client->exists(['id' => $id, 'index' => Constants::ES_INDEX, 'type' => Constants::ES_TYPE]); if ($exists) { $message = 'A recipe with this title already exists. You can view it ' . '<a href="/view.php?id=' . $id . '">here</a> or rename your recipe.'; } else { // Index the recipe in Elasticsearch $recipe = $_POST; $recipe['tags'] = Util::recipeTagsToArray($_POST['tags']); $document = ['id' => $id, 'index' => Constants::ES_INDEX, 'type' => Constants::ES_TYPE, 'body' => $recipe]; $client->index($document); // Redirect user to recipe view page $message = 'Recipe added!'; header('Location: /view.php?id=' . $id . '&message=' . $message); exit; } } ?> <html> <head>
// 'https://localhost', // SSL to localhost // 'https://192.168.1.3:9200', // SSL to IP + Port // 'http://*****:*****@localhost:9200', // HTTP Basic Auth // 'https://*****:*****@localhost:9200', // SSL + HTTP Basic Auth //); $params['hosts'] = array('localhost:9200'); $client = new Elasticsearch\Client($params); $t = $_GET['t']; try { switch (strtoupper($t)) { case 'GET': $params = array(); $params['index'] = 'acotel'; $params['type'] = 'eiplus_dev_status'; $params['id'] = '5521983059028'; if ($client->exists($params)) { $retDoc = $client->get($params); print_r($retDoc['_source']); } else { echo "INEXISTENT VALUE"; } break; case 'SEARCH': $searchParams['index'] = 'acotel'; $searchParams['type'] = 'eiplus_dev_status'; $searchParams['body']['query']['match']['msisdn'] = '5521983059028'; $queryResponse = $client->search($searchParams); print_r($queryResponse); //echo $queryResponse['hits']['hits'][0]['_id']; // Outputs 'abc' break; case 'SAVE':