Skip to content

Instantly share code, notes, and snippets.

@elchele
Last active July 5, 2016 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchele/e8c9e65afa63f15d9644 to your computer and use it in GitHub Desktop.
Save elchele/e8c9e65afa63f15d9644 to your computer and use it in GitHub Desktop.
PHP script for simple querying of Elasticsearch using Sugar default server settings
<?php
/* Author: Angel Magaña - cheleguanaco@cheleguanaco.com
* File: <sugar_root>/es_test.php
*
* Executes simple Elasticsearch query using
* Elasticsearch params from Sugar config files
* Usage: http://<sugar_url>/es_test.php?term=<your_search_term>
*
*/
//Set params
require_once('config.php');
require_once('config_override.php');
$index = $sugar_config['unique_key'] . '_shared';
$host = $sugar_config['full_text_engine']['Elastic']['host'];
$port = $sugar_config['full_text_engine']['Elastic']['port'];
$term = $_GET['term'];
$url = 'http://' . $host . ':' . $port . '/' . $index . '/_search?q=' . $term . '&size=100';
//cURL Request
$cu = curl_init();
curl_setopt($cu, CURLOPT_URL, $url);
curl_setopt($cu, CURLOPT_HEADER, 0);
curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($cu);
curl_close($cu);
$result = json_decode($result);
$all = $result->hits->hits;
foreach($all as $one)
{
$record = (array) $one->_source;
echo 'Module: ' . $record['module'] . '<br/>Name: ' . $record['name'] . '<p>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment