3v4l.org

run code in 500+ PHP versions simultaneously
<?php error_reporting(2); use Elastic\Elasticsearch\ClientBuilder; header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: *"); try { //print_r($_POST); $debug = false; $keys = array_keys($_POST); $search_location = $_POST['l']; // 0 - nosah, 1 - beur $books = $_POST['b']; // string in format '1,2,3,4,5,6,7,8,9,10,11,12,13,14' $phase = $_POST['phase']; if (in_array('e', $keys)) { $exactPhrase = $_POST['e']; } else { $exactPhrase = '0'; } if (in_array('from', $keys)) { $from = $_POST['from']; } else { $from = 0; } if (in_array('size', $keys)) { $size = $_POST['size']; } else { $size = 100; } if (in_array('book', $keys)) { $search_book = $_POST['book']; } else { $search_book = "mishne"; } if ($debug) { $date = new DateTime(); echo 'after get data from POST: ' . $date->format('H:i:s:u'); echo "<br />"; } $client = ClientBuilder::create() ->setHosts(["http://XXXXXXXX"]) ->build(); if ($from == 0) { $searchlogparams = [ 'index' => 'searchlog', 'type' => 'search_log', 'body' => ['phrase' => $phase, 'timestamp' => date('c'), 'search_type' => $exactPhrase, 'search_location' => $search_location, 'search_books' => explode(',', $books), ], ]; // Document will be indexed to my_index/my_type/my_id $response = $client->index($searchlogparams); } if ($debug) { $date = new DateTime(); echo 'after create connection: ' . $date->format('H:i:s:u'); echo "<br />"; } $params['from'] = $from; $params['size'] = $size; if ($search_book == "mishne") { $params['index'] = $search_location == '0' ? 'rambam_nosah_index' : 'rambam_beur_index'; } else if ($search_book == "more") { $params['index'] = $search_location == '0' ? 'more_nosah_index' : 'more_beur_index'; } $params['type'] = 'text'; $params['_source'] = $search_location == '0' ? array('book', 'hativa', 'chapter') : array('book', 'hativa', 'chapter', 'link'); $params['body']['sort'] = $search_location == '0' ? [ ['book' => ['order' => 'asc']], ['hativa' => ['order' => 'asc']], ['chapter' => ['order' => 'asc']], ] : [ ['book' => ['order' => 'asc']], ['hativa' => ['order' => 'asc']], ['chapter' => ['order' => 'asc']], ['link' => ['order' => 'asc']], ]; $params['body']['highlight'] = [ 'order' => 'score', 'fields' => [ 'content' => [ 'number_of_fragments' => 100, 'fragment_size' => 50, 'options' => [ 'locale' => 'he_il', 'top_scoring' => true, ], ], ], ]; if ($books == '1,2,3,4,5,6,7,8,9,10,11,12,13,14') { if ($exactPhrase == '1') { $params['body']['query']['match_phrase'] = ['content' => ['query' => $phase]]; } else { $params['body']['query']['match'] = ['content' => $phase]; } } else { $books_array = explode(',', $books); $params['body']['query']['bool']['should'] = []; foreach ($books_array as $value) { array_push($params['body']['query']['bool']['should'], [ 'term' => ['book' => $value], ]); } $params['body']['query']['bool']['minimum_should_match'] = 1; if ($exactPhrase == '1') { $params['body']['query']['bool']['must'] = [ 'match_phrase' => ['content' => ['query' => $phase]], ]; } else { $params['body']['query']['bool']['must'] = [ 'match' => ['content' => $phase], ]; } } if ($debug) { $date = new DateTime(); echo 'after create request object: ' . $date->format('H:i:s:u'); echo "<br />"; } //print_r($params); // $params = [ // 'size' => 1006, // 'index' => ['rambam_nosah_index', 'rambam_beur_index'], // 'type' => 'text', // //'explain' => true, // '_source' => array('book', 'hativa', 'chapter'), // 'body' => [ // 'query' => [ // 'bool' => [ // // 'filter' => [ // // ['term' => [ 'book' => '14' ]], // // ['term' => [ 'chapter' => '6' ]] // // ], // 'should' => [ // ['term' => [ 'book' => 14 ]], // ['term' => [ 'book' => 6 ]] // ], // 'minimum_should_match' => 1, // 'must' => [ // 'match' => ['content' => 'אברהם'], // ], // //'should' => [ // // //'match_phrase_prefix' => [ 'content' => 'טהרה' ] // // 'match_phrase' => [ 'content' => 'טהור' ] // // //'match' => [ 'content' => 'שבות' ] // //], // //'must_not' => [ // // 'match' => [ 'content' => 'שבת' ] // //] // ], // ], // 'sort' => [ // ['book' => ['order' => 'asc']], // ['hativa' => ['order' => 'asc']], // ['chapter' => ['order' => 'asc']], // ], // 'highlight' => [ // //'pre_tags' => ['<tag1>'], // //'post_tags' => ['</tag1>'], // //'tags_schema' => 'styled', // 'order' => 'score', // 'fields' => [ // 'content' => [ // //'force_source' => true, // 'number_of_fragments' => 200, // 'fragment_size' => 100, // //'no_match_size' => 150, // //'fragmenter' => 'scan', // scan OR sentence OR none // //'type' => 'experimental', // 'options' => [ // 'locale' => 'he_il', // 'top_scoring' => true, // ], // ], // ], // ], // ], // ]; //print_r($params); $results = $client->search($params); if ($debug) { $date = new DateTime(); echo 'after search: ' . $date->format('H:i:s:u'); echo "<br />"; } // echo "<pre>"; // print_r($results); // echo "</pre>"; // echo "<pre>"; // var_dump($results); // echo "</pre>"; $Maches = []; $id = 0; foreach ($results['hits']['hits'] as $index => $hit) { $Maches[$id] = [ 'l' => $hit['sort'], 'c' => $hit['highlight']['content'], ]; $id++; } if ($debug) { $date = new DateTime(); echo 'after create result object: ' . $date->format('H:i:s:u'); echo "<br />"; } $resultArray['paging'] = [ 'total' => $results['hits']['total']['value'], 'from' => $from, 'size' => $size, ]; $resultArray['maches'] = $Maches; //echo "<pre>"; print_r(json_encode($resultArray, JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_FORCE_OBJECT)); //print_r(gzcompress(json_encode($Maches, JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_FORCE_OBJECT), 6)); //echo "</pre>"; if ($debug) { $date = new DateTime(); echo 'after print result object: ' . $date->format('H:i:s:u'); echo "<br />"; } } catch (Exception $e) { // echo 'Caught exception: ', $e->getMessage(), "\n"; }

preferences:
23.08 ms | 483 KiB | 4 Q