Esempio n. 1
0
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>
  <title>Recipe Search</title>
  <link rel="stylesheet" href="/css/bootstrap.min.css" />
</head>
<body>
Esempio n. 2
0
 // First, setup full text search bits
 $fullTextClauses = [];
 if ($_REQUEST['title']) {
     $fullTextClauses[] = ['match' => ['title' => $_REQUEST['title']]];
 }
 if ($_REQUEST['description']) {
     $fullTextClauses[] = ['match' => ['description' => $_REQUEST['description']]];
 }
 if ($_REQUEST['ingredients']) {
     $fullTextClauses[] = ['match' => ['ingredients' => $_REQUEST['ingredients']]];
 }
 if ($_REQUEST['directions']) {
     $fullTextClauses[] = ['match' => ['directions' => $_REQUEST['directions']]];
 }
 if ($_REQUEST['tags']) {
     $tags = Util::recipeTagsToArray($_REQUEST['tags']);
     $fullTextClauses[] = ['terms' => ['tags' => $tags, 'minimum_should_match' => count($tags)]];
 }
 if (count($fullTextClauses) > 0) {
     $query = ['bool' => ['must' => $fullTextClauses]];
 } else {
     $query = ['match_all' => (object) []];
 }
 // Then setup exact match bits
 $filterClauses = [];
 if ($_REQUEST['prep_time_min_low'] || $_REQUEST['prep_time_min_high']) {
     $rangeFilter = [];
     if ($_REQUEST['prep_time_min_low']) {
         $rangeFilter['gte'] = (int) $_REQUEST['prep_time_min_low'];
     }
     if ($_REQUEST['prep_time_min_high']) {