本文整理汇总了PHP中SearchResult::getHit方法的典型用法代码示例。如果您正苦于以下问题:PHP SearchResult::getHit方法的具体用法?PHP SearchResult::getHit怎么用?PHP SearchResult::getHit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SearchResult
的用法示例。
在下文中一共展示了SearchResult::getHit方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: cleanupElasticsearch
/**
* Find documents in Elasticsearch which don't exist in Elgg anymore
*
* @return void
*/
protected static function cleanupElasticsearch()
{
$client = elasticsearch_get_client();
if (empty($client)) {
return;
}
// this could take a while
set_time_limit(0);
// prepare a search for all documents
$search_params = ['index' => $client->getIndex(), 'search_type' => 'scan', 'scroll' => '2m', 'body' => ['query' => ['match_all' => []]]];
try {
$scroll_setup = $client->search($search_params);
} catch (\Exception $e) {
return;
}
// now scroll through all results
$scroll_params = ['scroll_id' => elgg_extract('_scroll_id', $scroll_setup), 'scroll' => '2m'];
// ignore Elgg access
$ia = elgg_set_ignore_access(true);
try {
while ($result = $client->scroll($scroll_params)) {
$search_result = new SearchResult($result, $search_params);
$elasticsearch_guids = $search_result->toGuids();
if (empty($elasticsearch_guids)) {
break;
}
$elgg_guids = elgg_get_entities(['guids' => $elasticsearch_guids, 'limit' => false, 'callback' => function ($row) {
return (int) $row->guid;
}]);
$guids_not_in_elgg = array_diff($elasticsearch_guids, $elgg_guids);
if (empty($guids_not_in_elgg)) {
continue;
}
// remove all left over documents
foreach ($guids_not_in_elgg as $guid) {
// need to get the hist from Elasticsearch to get the type, since it's not in Elgg anymore
$hit = $search_result->getHit($guid);
elasticsearch_add_document_for_deletion($guid, ['_index' => $client->getIndex(), '_type' => elgg_extract('_type', $hit), '_id' => $guid]);
}
}
} catch (\Exception $e) {
// probably reached the end of the scroll
// elgg_log('Elasticsearch cleanup: ' . $e->getMessage(), 'ERROR');
}
// restore access
elgg_set_ignore_access($ia);
// clear scroll
try {
$client->clearScroll($scroll_params);
} catch (\Exception $e) {
// unable to clean, could be because we came to the end of the scroll
}
}
示例2: inspect
/**
* Inspect a GUID in Elasticsearch
*
* @param int $guid the GUID to inspect
*
* @return false|array
*/
public function inspect($guid)
{
$guid = (int) $guid;
if ($guid < 1) {
return false;
}
$search_params = ['index' => $this->getIndex(), 'body' => ['query' => ['filtered' => ['filter' => ['term' => ['guid' => $guid]]]]]];
try {
$search_result = $this->search($search_params);
$s = new SearchResult($search_result, $search_params);
$hit = $s->getHit($guid);
if (empty($hit)) {
return false;
}
return elgg_extract('_source', $hit);
} catch (\Exception $e) {
// somethig went wrong
}
return false;
}