Answer the question
In order to leave comments, you need to log in
How to generate a Sphinx query in Yii2 (yiisoft/yii2-sphinx component)?
Greetings!
There is a project written in PHP without frameworks, MVC, and so on. When rewriting to Yii2, a question arose with the search through Sphinx. The question is how to set search options like setMatchMode and setSortMode and how to attach buildExcerpts?
The simplest request can be made from the documentation
$rows = (new Query)
->from('my_index')
->all();
$rowSnippetSources[] = file_get_contents('/path/to/index/files/' . $row['id'] . '.txt');
$s = new SphinxClient();
$s->setServer("127.0.0.1", 9312);
$s->setMatchMode(SPH_MATCH_EXTENDED2);
$s->setSortMode(SPH_SORT_RELEVANCE);
$s->SetFieldWeights([
'name' => 10,
'desc' => 1,
'video_text' => 1,
]);
$s->setMaxQueryTime(3000);
$result = $s->query($name, '*');
$items = [];
foreach ($result['matches'] as $id => $item) {
$items[$id] = [
'name' => $item['attrs']['name'],
'weight' => $item['weight'],
'direct' => $item['attrs']['name'] == $name
];
$desc = $s->buildExcerpts([$item['attrs']['desc']], 'my_index', $name, [
'before_match' => '<strong>',
'after_match' => '</strong>',
'around' => 2,
'limit' => 2,
]);
$items[$id]['desc'] = $desc[0] ?? false;
}
usort($items, function ($a, $b) {
if ($a['direct']) {
return -1;
} else {
return $a['weight'] < $b['weight'];
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question