Search code examples
phpsphinxsphinxql

How to search by URL using Sphinx?


I have index in Sphinx. One of field in index is URL. And it contains for example next values:

(1) http://u24.ru/news/38417/v-centre-chelyabinska-muzhchina-popytalsya-pereplyt-reku?rss
(2) https://meduza.io/feature/2017/07/10/islamskoe-gosudarstvo-vybili-iz-mosula-chto-budet-dalshe
(3) https://stolica-s.su/sport/athletics/89078

How I try to search by url (part of code):

$query = $this->manager->makeQuery()
    ->select(['id'])
    ->from(['content']);

$url = str_replace(
    ['=', '&', '?', '-', '%'],
    [' ', ' ', ' ', ' ', ' '],
    'http://www.ruscur.ru/themes/0/00/70/7020.shtml?news/0/10/43/104359'
);
$url = $query->escapeMatch($url);
$query->match('url', $url);

var_dump($query->execute()->fetchAllAssoc()); // empty

Is any way to search with Sphinx without using fulltext searching?


Solution

  • You can make a hash of URL and store it, then compare by hash:

    $query = $this->manager->makeQuery()
        ->select(['id'])
        ->from(['content'])
        ->where('hash', '=', Hasher::hash($url));
    

    Hint for query builder: http://foolcode.github.io/SphinxQL-Query-Builder/