$request = $connector->search('meat');
$request->resultsOptions()
->sortBy(
\Infinity\API\ResultsOptions::TYPE_ATTRIBUTE,
\Infinity\API\ResultsOptions::ORDER_ASC,
'Name'
)
->skip(3)
->take(2)
->addDistinctFacet(
'Category',
'Category2'
);
/* Actually perform the search query */
$response = $connector->query($request);
if (!$response->getMakesSense()) {
echo 'We did not understand your query' . PHP_EOL;
}
$spellingSuggestions = $response->getSpellingSuggestions();
if (!empty($spellingSuggestions)) {
$queries = array_map(
function ($suggestion) {
return $suggestion->getQuery();
},
$spellingSuggestions
);
$suggestions = join(', ', $queries);
echo 'Did you mean: ' . $suggestions . '?' . PHP_EOL;
}
/* Get the total result count, which may help with pagination */
$count = $response->getRaw()->getResults()->getCount();
$current_count = sizeof($response->getResults());
echo 'Found in total ' . $count . ' results. '
. 'Showing ' . $current_count . ':' . PHP_EOL;
/* Print all results in this response */
foreach ($response->getResults() as $entity) {
$id = $entity->getId();
//we know that this attribute exists
$title = $entity->getAttribute('Title');
//this attribute may not exist, check if it does before using it
if($entity->hasAttribute('Does not exist'))
echo $entity->getAttribute('Does not exist');
echo $id . ': ' . $title . PHP_EOL;
}
/* Print all related results in this response */
echo PHP_EOL . 'You might also like these:' . PHP_EOL;
foreach ($response->getRelatedResults() as $entity) {
$id = $entity->getId();
$title = $entity->getAttribute('Title');
echo $id . ': ' . $title . PHP_EOL;
}