Skip to content

Instantly share code, notes, and snippets.

@thangman22
Created June 11, 2014 08:58
Show Gist options
  • Save thangman22/080e4db10bc9c993a56a to your computer and use it in GitHub Desktop.
Save thangman22/080e4db10bc9c993a56a to your computer and use it in GitHub Desktop.
Chang Elastic
<?php
function _getPlayerFromElastic($field, $keywords) {
$returnItem = array();
$keywordImplode = implode(" OR ", $keywords);
$query = '{"query":{"bool":{"must":[{"query_string":{"default_field":"' . $field . '","query":"' . $keywordImplode . '"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"facets":{}}';
$url = 'http://162.222.178.137:9200/message/_search';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $query);
$response = curl_exec($curl);
$objResponse = json_decode($response, TRUE);
return $objResponse;
}
function getSearchResultFromKeyword($keywords) {
$facebookResponse = _getPlayerFromElastic("facebook.message", $keywords);
$sumOfTotalMessage += $facebookResponse['hits']['total'];
echo "Facebook ".$sumOfTotalMessage."\n";
foreach ($facebookResponse['hits']['hits'] as $item) {
$returnItem[$item['_id']] = $item['_source'];
}
$TwitterResponse = _getPlayerFromElastic("twitter.text", $keywords);
$sumOfTotalMessage += $TwitterResponse['hits']['total'];
echo "Twitter ".$sumOfTotalMessage."\n";
foreach ($TwitterResponse['hits']['hits'] as $item) {
$returnItem[$item['_id']] = $item['_source'];
}
$InstagramResponse = _getPlayerFromElastic("instagram.caption.text", $keywords);
$sumOfTotalMessage += $InstagramResponse['hits']['total'];
echo "Instagram ".$sumOfTotalMessage."\n";
foreach ($InstagramResponse['hits']['hits'] as $item) {
$returnItem[$item['_id']] = $item['_source'];
}
$PantipTopicResponse = _getPlayerFromElastic("pantip.topic", $keywords);
$sumOfTotalMessage += $PantipTopicResponse['hits']['total'];
echo "Pantip topic ".$sumOfTotalMessage."\n";
foreach ($PantipTopicResponse['hits']['hits'] as $item) {
$returnItem[$item['_id']] = $item['_source'];
}
$PantipDetailResponse = _getPlayerFromElastic("pantip.detail", $keywords);
$sumOfTotalMessage += $PantipDetailResponse['hits']['total'];
echo "Pantip detail ".$sumOfTotalMessage."\n";
foreach ($PantipDetailResponse['hits']['hits'] as $item) {
$returnItem[$item['_id']] = $item['_source'];
}
$sockerSuckTopicResponse = _getPlayerFromElastic("soccersuck.topic", $keywords);
$sumOfTotalMessage += $sockerSuckTopicResponse['hits']['total'];
echo "Soccersuck topic ".$sumOfTotalMessage."\n";
foreach ($sockerSuckTopicResponse['hits']['hits'] as $item) {
$returnItem[$item['_id']] = $item['_source'];
}
$sockerSuckDetailResponse = _getPlayerFromElastic("soccersuck.detail", $keywords);
$sumOfTotalMessage += $sockerSuckDetailResponse['hits']['total'];
echo "Soccersuck detail ".$sumOfTotalMessage."\n";
foreach ($sockerSuckDetailResponse['hits']['hits'] as $item) {
$returnItem[$item['_id']] = $item['_source'];
}
return array("result"=>$returnItem,"total"=>$sumOfTotalMessage);
}
var_dump(getSearchResultFromKeyword(array("a")));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment