Search code examples
phpmysqlsphinx

Execute category search Sphinx php


Good time of day!

There is such config Sphinx

    source txtcontent :  ru_config
{ 
  sql_query = SELECT `id` as `txt_id`, 1 as index_id, `type_id`,`content_type_id`, `title`, `annonce`, `content` FROM `TxtContent` WHERE `status` = 1 AND `content_type_id` != 14
  sql_attr_uint   = index_id
  sql_attr_uint   = type_id
}

The entire table is indexed, and is stored in one large search index. When it comes to find what is in it then all works OK

But today the task was to search for categories The categories described in the field and have a type_id of type int How in php using SphinxAPI to perform such a search? Standard search looks like this.

$sphinxClient = new SphinxClient();
$sphinxClient->SetServer("127.0.0.1", 3312 );
$sphinxClient->SetLimits( 0, 700,700 );
$sphinxClient->SetSortMode(SPH_SORT_RELEVANCE);
$sphinxClient->SetArrayResult( true );
$result = $sphinxClient->Query( $this->query, 'txtcontent provider item');

I tried to add

$sphinxClient->SetFilter('type_id','1');

To search only where type_id = 1 but it didn't help.

Actually how can I search for a specific category? option to find everything in php to let go of the result excess is not considered (otherwise, the search will then be saturada existing limit) how to do it "properly" via the API without placing each topic in a separate search index?


Solution

  • setFilter takes an Array of values. And they need to be numeric (type_id is a numeric attribute)

    $sphinxClient->SetFilter('type_id',array(1));
    

    The sphinxapi class actully uses assertions to detect invalid data like this, which I guess you have disabled (otherwise would of seen them!).